OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/extensions/extension_dialog.h" | 5 #include "chrome/browser/ui/views/extensions/extension_dialog.h" |
6 | 6 |
7 #include "chrome/browser/extensions/extension_host.h" | 7 #include "chrome/browser/extensions/extension_host.h" |
8 #include "chrome/browser/extensions/extension_process_manager.h" | 8 #include "chrome/browser/extensions/extension_process_manager.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/browser_window.h" | 11 #include "chrome/browser/ui/browser_window.h" |
12 #include "chrome/browser/ui/dialog_style.h" | 12 #include "chrome/browser/ui/dialog_style.h" |
13 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" | 13 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" |
14 #include "chrome/browser/ui/views/window.h" // CreateViewsWindow | 14 #include "chrome/browser/ui/views/window.h" // CreateViewsWindow |
15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
16 #include "content/browser/renderer_host/render_view_host.h" | 16 #include "content/browser/renderer_host/render_view_host.h" |
17 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
18 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
19 #include "content/public/browser/render_widget_host_view.h" | 19 #include "content/public/browser/render_widget_host_view.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 22 #include "ui/gfx/screen.h" |
22 #include "ui/views/background.h" | 23 #include "ui/views/background.h" |
23 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
24 | 25 |
25 #if defined(USE_AURA) | 26 #if defined(USE_AURA) |
| 27 #include "ash/shell.h" |
26 #include "ui/aura/root_window.h" | 28 #include "ui/aura/root_window.h" |
27 #include "ui/aura/window.h" | |
28 #endif | 29 #endif |
29 | 30 |
30 using content::WebContents; | 31 using content::WebContents; |
31 | 32 |
32 ExtensionDialog::ExtensionDialog(ExtensionHost* host, | 33 ExtensionDialog::ExtensionDialog(ExtensionHost* host, |
33 ExtensionDialogObserver* observer) | 34 ExtensionDialogObserver* observer) |
34 : window_(NULL), | 35 : window_(NULL), |
35 extension_host_(host), | 36 extension_host_(host), |
36 observer_(observer) { | 37 observer_(observer) { |
37 AddRef(); // Balanced in DeleteDelegate(); | 38 AddRef(); // Balanced in DeleteDelegate(); |
(...skipping 13 matching lines...) Expand all Loading... |
51 | 52 |
52 // static | 53 // static |
53 ExtensionDialog* ExtensionDialog::Show( | 54 ExtensionDialog* ExtensionDialog::Show( |
54 const GURL& url, | 55 const GURL& url, |
55 Browser* browser, | 56 Browser* browser, |
56 WebContents* web_contents, | 57 WebContents* web_contents, |
57 int width, | 58 int width, |
58 int height, | 59 int height, |
59 const string16& title, | 60 const string16& title, |
60 ExtensionDialogObserver* observer) { | 61 ExtensionDialogObserver* observer) { |
61 return ExtensionDialog::ShowInternal(url, browser, web_contents, width, | 62 ExtensionHost* host = CreateExtensionHost(url, browser, NULL); |
62 height, false, title, observer); | 63 if (!host) |
| 64 return NULL; |
| 65 host->set_associated_web_contents(web_contents); |
| 66 |
| 67 return ExtensionDialog::ShowInternal(url, browser, host, width, height, |
| 68 false, title, observer); |
63 } | 69 } |
64 | 70 |
65 #if defined(USE_AURA) | 71 #if defined(USE_AURA) |
66 // static | 72 // static |
67 ExtensionDialog* ExtensionDialog::ShowFullscreen( | 73 ExtensionDialog* ExtensionDialog::ShowFullscreen( |
68 const GURL& url, | 74 const GURL& url, |
69 Browser* browser, | 75 Profile* profile, |
70 WebContents* web_contents, | |
71 const string16& title, | 76 const string16& title, |
72 ExtensionDialogObserver* observer) { | 77 ExtensionDialogObserver* observer) { |
73 return ExtensionDialog::ShowInternal(url, browser, web_contents, 0, 0, | 78 ExtensionHost* host = CreateExtensionHost(url, NULL, profile); |
| 79 if (!host) |
| 80 return NULL; |
| 81 |
| 82 return ExtensionDialog::ShowInternal(url, NULL, host, 0, 0, |
74 true, title, observer); | 83 true, title, observer); |
75 } | 84 } |
76 #endif | 85 #endif |
77 | 86 |
78 // static | 87 // static |
79 ExtensionDialog* ExtensionDialog::ShowInternal(const GURL& url, | 88 ExtensionDialog* ExtensionDialog::ShowInternal(const GURL& url, |
80 Browser* browser, | 89 Browser* browser, |
81 content::WebContents* web_contents, | 90 ExtensionHost* host, |
82 int width, | 91 int width, |
83 int height, | 92 int height, |
84 bool fullscreen, | 93 bool fullscreen, |
85 const string16& title, | 94 const string16& title, |
86 ExtensionDialogObserver* observer) { | 95 ExtensionDialogObserver* observer) { |
87 CHECK(browser); | 96 CHECK(fullscreen || browser); |
88 ExtensionHost* host = CreateExtensionHost(url, browser); | |
89 if (!host) | |
90 return NULL; | |
91 host->set_associated_web_contents(web_contents); | |
92 | |
93 ExtensionDialog* dialog = new ExtensionDialog(host, observer); | 97 ExtensionDialog* dialog = new ExtensionDialog(host, observer); |
94 dialog->set_title(title); | 98 dialog->set_title(title); |
| 99 |
95 if (fullscreen) | 100 if (fullscreen) |
96 dialog->InitWindowFullscreen(browser); | 101 dialog->InitWindowFullscreen(); |
97 else | 102 else |
98 dialog->InitWindow(browser, width, height); | 103 dialog->InitWindow(browser, width, height); |
99 | 104 |
100 // Show a white background while the extension loads. This is prettier than | 105 // Show a white background while the extension loads. This is prettier than |
101 // flashing a black unfilled window frame. | 106 // flashing a black unfilled window frame. |
102 host->view()->set_background( | 107 host->view()->set_background( |
103 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); | 108 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); |
104 host->view()->SetVisible(true); | 109 host->view()->SetVisible(true); |
105 | 110 |
106 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. | 111 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. |
107 host->host_contents()->Focus(); | 112 host->host_contents()->Focus(); |
108 return dialog; | 113 return dialog; |
109 } | 114 } |
110 | 115 |
111 // static | 116 // static |
112 ExtensionHost* ExtensionDialog::CreateExtensionHost(const GURL& url, | 117 ExtensionHost* ExtensionDialog::CreateExtensionHost(const GURL& url, |
113 Browser* browser) { | 118 Browser* browser, |
114 ExtensionProcessManager* manager = | 119 Profile* profile) { |
115 browser->profile()->GetExtensionProcessManager(); | 120 // Prefer picking the extension manager from the profile if given. |
| 121 ExtensionProcessManager* manager = NULL; |
| 122 if (profile) |
| 123 manager = profile->GetExtensionProcessManager(); |
| 124 else |
| 125 manager = browser->profile()->GetExtensionProcessManager(); |
| 126 |
116 DCHECK(manager); | 127 DCHECK(manager); |
117 if (!manager) | 128 if (!manager) |
118 return NULL; | 129 return NULL; |
119 return manager->CreateDialogHost(url, browser); | 130 if (browser) |
| 131 return manager->CreateDialogHost(url, browser); |
| 132 else |
| 133 return manager->CreatePopupHost(url, NULL); |
120 } | 134 } |
121 | 135 |
122 #if defined(USE_AURA) | 136 #if defined(USE_AURA) |
123 void ExtensionDialog::InitWindowFullscreen(Browser* browser) { | 137 void ExtensionDialog::InitWindowFullscreen() { |
124 gfx::NativeWindow parent = browser->window()->GetNativeHandle(); | 138 aura::RootWindow* root_window = ash::Shell::GetRootWindow(); |
| 139 gfx::Rect screen_rect = |
| 140 gfx::Screen::GetMonitorAreaNearestWindow(root_window); |
125 | 141 |
126 // Create the window as a child of the root window. | 142 // We want to be the fullscreen topmost child of the root window. |
127 window_ = browser::CreateFramelessViewsWindow( | 143 window_ = browser::CreateFramelessViewsWindow(root_window, this); |
128 parent->GetRootWindow(), this); | 144 window_->StackAtTop(); |
129 // Make sure we're always on top by putting ourselves at the top | 145 window_->SetBounds(screen_rect); |
130 // of the z-order of the child windows of the root window. | 146 window_->Show(); |
131 parent->GetRootWindow()->StackChildAtTop(window_->GetNativeWindow()); | |
132 | 147 |
133 int width = parent->GetRootWindow()->GetHostSize().width(); | |
134 int height = parent->GetRootWindow()->GetHostSize().height(); | |
135 window_->SetBounds(gfx::Rect(0, 0, width, height)); | |
136 | |
137 window_->Show(); | |
138 // TODO(jamescook): Remove redundant call to Activate()? | 148 // TODO(jamescook): Remove redundant call to Activate()? |
139 window_->Activate(); | 149 window_->Activate(); |
140 } | 150 } |
141 #else | 151 #else |
142 void ExtensionDialog::InitWindowFullscreen(Browser* browser) { | 152 void ExtensionDialog::InitWindowFullscreen() { |
143 NOTIMPLEMENTED(); | 153 NOTIMPLEMENTED(); |
144 } | 154 } |
145 #endif | 155 #endif |
146 | 156 |
147 | 157 |
148 void ExtensionDialog::InitWindow(Browser* browser, int width, int height) { | 158 void ExtensionDialog::InitWindow(Browser* browser, int width, int height) { |
149 gfx::NativeWindow parent = browser->window()->GetNativeHandle(); | 159 gfx::NativeWindow parent = browser->window()->GetNativeHandle(); |
150 #if defined(OS_CHROMEOS) | 160 #if defined(OS_CHROMEOS) |
151 DialogStyle style = STYLE_FLUSH; | 161 DialogStyle style = STYLE_FLUSH; |
152 #else | 162 #else |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 if (content::Details<ExtensionHost>(host()) != details) | 250 if (content::Details<ExtensionHost>(host()) != details) |
241 return; | 251 return; |
242 if (observer_) | 252 if (observer_) |
243 observer_->ExtensionTerminated(this); | 253 observer_->ExtensionTerminated(this); |
244 break; | 254 break; |
245 default: | 255 default: |
246 NOTREACHED() << L"Received unexpected notification"; | 256 NOTREACHED() << L"Received unexpected notification"; |
247 break; | 257 break; |
248 } | 258 } |
249 } | 259 } |
OLD | NEW |