// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/renderer_host/pepper/monitor_finder_mac.h"

#import <Cocoa/Cocoa.h>

#include "base/bind.h"
#include "base/task/post_task.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"

MonitorFinder::MonitorFinder(int process_id, int render_frame_id)
    : process_id_(process_id),
      render_frame_id_(render_frame_id),
      request_sent_(false),
      display_id_(kCGNullDirectDisplay) {}

MonitorFinder::~MonitorFinder() {}

int64_t MonitorFinder::GetMonitor() {
  {
    // The plugin may call this method several times, so avoid spamming the UI
    // thread with requests by only allowing one outstanding request at a time.
    base::AutoLock lock(mutex_);
    if (request_sent_)
      return display_id_;
    request_sent_ = true;
  }

  base::PostTaskWithTraits(
      FROM_HERE, {content::BrowserThread::UI},
      base::BindOnce(&MonitorFinder::FetchMonitorFromWidget, this));
  return display_id_;
}

// static
bool MonitorFinder::IsMonitorBuiltIn(int64_t display_id) {
  return CGDisplayIsBuiltin(display_id);
}

void MonitorFinder::FetchMonitorFromWidget() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  content::RenderFrameHost* rfh =
      content::RenderFrameHost::FromID(process_id_, render_frame_id_);
  if (!rfh)
    return;

  gfx::NativeView native_view = rfh->GetNativeView();
  NSWindow* window = [native_view.GetNativeNSView() window];
  NSScreen* screen = [window screen];
  CGDirectDisplayID display_id =
      [[[screen deviceDescription] objectForKey:@"NSScreenNumber"] intValue];

  base::AutoLock lock(mutex_);
  request_sent_ = false;
  display_id_ = display_id;
}
