summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/pepper/monitor_finder_mac.mm
blob: 23427165c5aa8e5ed31e47e51782e5cabba973a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 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 "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"

namespace chrome {

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;
  }

  content::BrowserThread::PostTask(
      content::BrowserThread::UI,
      FROM_HERE,
      base::Bind(&MonitorFinder::FetchMonitorFromWidget, this));
  return display_id_;
}

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

void MonitorFinder::FetchMonitorFromWidget() {
  DCHECK(content::BrowserThread::CurrentlyOn(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 window];
  NSScreen* screen = [window screen];
  CGDirectDisplayID display_id =
      [[[screen deviceDescription] objectForKey:@"NSScreenNumber"] intValue];

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

}  // namespace chrome