OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/macros.h" | |
7 #include "base/memory/weak_ptr.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/threading/thread_task_runner_handle.h" | |
10 #import "chrome/browser/app_controller_mac.h" | |
11 #include "chrome/browser/chrome_notification_types.h" | |
12 #include "chrome/browser/fullscreen.h" | |
13 #include "chrome/browser/ui/browser.h" | |
14 #include "chrome/browser/ui/browser_window.h" | |
15 #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h" | |
16 #include "chrome/browser/ui/panels/display_settings_provider.h" | |
17 #include "content/public/browser/notification_observer.h" | |
18 #include "content/public/browser/notification_registrar.h" | |
19 #include "content/public/browser/notification_service.h" | |
20 #include "content/public/browser/notification_source.h" | |
21 #include "ui/base/work_area_watcher_observer.h" | |
22 | |
23 namespace { | |
24 | |
25 // The time, in milliseconds, that a fullscreen check will be started after | |
26 // the active workspace change is notified. This value is from experiment. | |
27 const int kCheckFullScreenDelayTimeMs = 200; | |
28 | |
29 class DisplaySettingsProviderCocoa : public DisplaySettingsProvider, | |
30 public ui::WorkAreaWatcherObserver, | |
31 public content::NotificationObserver { | |
32 public: | |
33 DisplaySettingsProviderCocoa(); | |
34 ~DisplaySettingsProviderCocoa() override; | |
35 | |
36 void ActiveSpaceChanged(); | |
37 | |
38 protected: | |
39 // Overridden from DisplaySettingsProvider: | |
40 bool NeedsPeriodicFullScreenCheck() const override; | |
41 bool IsFullScreen() override; | |
42 | |
43 // Overridden from ui::WorkAreaWatcherObserver: | |
44 void WorkAreaChanged() override; | |
45 | |
46 // Overridden from content::NotificationObserver: | |
47 void Observe(int type, | |
48 const content::NotificationSource& source, | |
49 const content::NotificationDetails& details) override; | |
50 | |
51 private: | |
52 void ActiveWorkSpaceChanged(); | |
53 | |
54 content::NotificationRegistrar registrar_; | |
55 id active_space_change_; | |
56 | |
57 // Owned by MessageLoop after posting. | |
58 base::WeakPtrFactory<DisplaySettingsProviderCocoa> weak_factory_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(DisplaySettingsProviderCocoa); | |
61 }; | |
62 | |
63 DisplaySettingsProviderCocoa::DisplaySettingsProviderCocoa() | |
64 : active_space_change_(nil), | |
65 weak_factory_(this) { | |
66 AppController* appController = static_cast<AppController*>([NSApp delegate]); | |
67 [appController addObserverForWorkAreaChange:this]; | |
68 | |
69 registrar_.Add( | |
70 this, | |
71 chrome::NOTIFICATION_FULLSCREEN_CHANGED, | |
72 content::NotificationService::AllSources()); | |
73 | |
74 active_space_change_ = [[[NSWorkspace sharedWorkspace] notificationCenter] | |
75 addObserverForName:NSWorkspaceActiveSpaceDidChangeNotification | |
76 object:nil | |
77 queue:[NSOperationQueue mainQueue] | |
78 usingBlock:^(NSNotification* notification) { | |
79 ActiveWorkSpaceChanged(); | |
80 }]; | |
81 } | |
82 | |
83 DisplaySettingsProviderCocoa::~DisplaySettingsProviderCocoa() { | |
84 AppController* appController = static_cast<AppController*>([NSApp delegate]); | |
85 [appController removeObserverForWorkAreaChange:this]; | |
86 | |
87 [[[NSWorkspace sharedWorkspace] notificationCenter] | |
88 removeObserver:active_space_change_]; | |
89 } | |
90 | |
91 bool DisplaySettingsProviderCocoa::NeedsPeriodicFullScreenCheck() const { | |
92 // Lion system introduces fullscreen support. When a window of an application | |
93 // enters fullscreen mode, the system will automatically hide all other | |
94 // windows, even including topmost windows that come from other applications. | |
95 // So we don't need to do anything when any other application enters | |
96 // fullscreen mode. We still need to handle the case when chrome enters | |
97 // fullscreen mode and our topmost windows will not get hided by the system. | |
98 return false; | |
99 } | |
100 | |
101 bool DisplaySettingsProviderCocoa::IsFullScreen() { | |
102 Browser* browser = chrome::GetLastActiveBrowser(); | |
103 if (!browser) | |
104 return false; | |
105 BrowserWindow* browser_window = browser->window(); | |
106 if (!browser_window->IsFullscreen()) | |
107 return false; | |
108 | |
109 // If the user switches to another space where the fullscreen browser window | |
110 // does not live, we do not call it fullscreen. | |
111 NSWindow* native_window = browser_window->GetNativeWindow(); | |
112 return [native_window isOnActiveSpace]; | |
113 } | |
114 | |
115 void DisplaySettingsProviderCocoa::WorkAreaChanged() { | |
116 OnDisplaySettingsChanged(); | |
117 } | |
118 | |
119 void DisplaySettingsProviderCocoa::Observe( | |
120 int type, | |
121 const content::NotificationSource& source, | |
122 const content::NotificationDetails& details) { | |
123 DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type); | |
124 // When we receive the fullscreen notification, the Chrome Window has not been | |
125 // put on the active space yet and thus IsFullScreen will return false. | |
126 // Since the fullscreen result is already known here, we can pass it dierctly | |
127 // to CheckFullScreenMode. | |
128 bool is_fullscreen = *(content::Details<bool>(details)).ptr(); | |
129 CheckFullScreenMode( | |
130 is_fullscreen ? ASSUME_FULLSCREEN_ON : ASSUME_FULLSCREEN_OFF); | |
131 } | |
132 | |
133 void DisplaySettingsProviderCocoa::ActiveWorkSpaceChanged() { | |
134 // The active workspace notification might be received earlier than the | |
135 // browser window knows that it is not in active space. | |
136 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
137 FROM_HERE, | |
138 base::Bind(&DisplaySettingsProviderCocoa::CheckFullScreenMode, | |
139 weak_factory_.GetWeakPtr(), PERFORM_FULLSCREEN_CHECK), | |
140 base::TimeDelta::FromMilliseconds(kCheckFullScreenDelayTimeMs)); | |
141 } | |
142 | |
143 } // namespace | |
144 | |
145 // static | |
146 DisplaySettingsProvider* DisplaySettingsProvider::Create() { | |
147 return new DisplaySettingsProviderCocoa(); | |
148 } | |
OLD | NEW |