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/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/extensions/extension_host.h" | 8 #include "chrome/browser/extensions/extension_host.h" |
9 #include "chrome/browser/extensions/extension_process_manager.h" | 9 #include "chrome/browser/extensions/extension_process_manager.h" |
10 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/views/constrained_window_views.h" | 12 #include "chrome/browser/ui/views/constrained_window_views.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 "content/public/browser/notification_details.h" | 14 #include "content/public/browser/notification_details.h" |
15 #include "content/public/browser/notification_source.h" | 15 #include "content/public/browser/notification_source.h" |
16 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/render_widget_host_view.h" | 17 #include "content/public/browser/render_widget_host_view.h" |
18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
19 #include "content/public/browser/web_contents_view.h" | 19 #include "content/public/browser/web_contents_view.h" |
20 #include "ui/base/base_window.h" | 20 #include "ui/base/base_window.h" |
21 #include "ui/gfx/screen.h" | 21 #include "ui/gfx/screen.h" |
22 #include "ui/views/background.h" | 22 #include "ui/views/background.h" |
23 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
25 | 25 |
26 using content::WebContents; | 26 using content::WebContents; |
27 | 27 |
28 ExtensionDialog::ExtensionDialog(extensions::ExtensionHost* host, | 28 ExtensionDialog::ExtensionDialog(extensions::ExtensionHost* host, |
29 ExtensionDialogObserver* observer) | 29 ExtensionDialogObserver* observer) |
30 : window_(NULL), | 30 : extension_host_(host), |
31 extension_host_(host), | |
32 observer_(observer) { | 31 observer_(observer) { |
33 AddRef(); // Balanced in DeleteDelegate(); | 32 AddRef(); // Balanced in DeleteDelegate(); |
34 | 33 |
35 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 34 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
36 content::Source<Profile>(host->profile())); | 35 content::Source<Profile>(host->profile())); |
37 // Listen for the containing view calling window.close(); | 36 // Listen for the containing view calling window.close(); |
38 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 37 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
39 content::Source<Profile>(host->profile())); | 38 content::Source<Profile>(host->profile())); |
40 // Listen for a crash or other termination of the extension process. | 39 // Listen for a crash or other termination of the extension process. |
41 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, | 40 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 DCHECK(manager); | 91 DCHECK(manager); |
93 if (!manager) | 92 if (!manager) |
94 return NULL; | 93 return NULL; |
95 return manager->CreateDialogHost(url); | 94 return manager->CreateDialogHost(url); |
96 } | 95 } |
97 | 96 |
98 void ExtensionDialog::InitWindow(ui::BaseWindow* base_window, | 97 void ExtensionDialog::InitWindow(ui::BaseWindow* base_window, |
99 int width, | 98 int width, |
100 int height) { | 99 int height) { |
101 gfx::NativeWindow parent = base_window->GetNativeWindow(); | 100 gfx::NativeWindow parent = base_window->GetNativeWindow(); |
102 window_ = CreateBrowserModalDialogViews(this, parent); | 101 views::Widget* window = CreateBrowserModalDialogViews(this, parent); |
103 | 102 |
104 // Center the window over the browser. | 103 // Center the window over the browser. |
105 gfx::Point center = base_window->GetBounds().CenterPoint(); | 104 gfx::Point center = base_window->GetBounds().CenterPoint(); |
106 int x = center.x() - width / 2; | 105 int x = center.x() - width / 2; |
107 int y = center.y() - height / 2; | 106 int y = center.y() - height / 2; |
108 // Ensure the top left and top right of the window are on screen, with | 107 // Ensure the top left and top right of the window are on screen, with |
109 // priority given to the top left. | 108 // priority given to the top left. |
110 gfx::Rect screen_rect = gfx::Screen::GetScreenFor(parent)-> | 109 gfx::Rect screen_rect = gfx::Screen::GetScreenFor(parent)-> |
111 GetDisplayNearestPoint(center).bounds(); | 110 GetDisplayNearestPoint(center).bounds(); |
112 gfx::Rect bounds_rect = gfx::Rect(x, y, width, height); | 111 gfx::Rect bounds_rect = gfx::Rect(x, y, width, height); |
113 bounds_rect.AdjustToFit(screen_rect); | 112 bounds_rect.AdjustToFit(screen_rect); |
114 window_->SetBounds(bounds_rect); | 113 window->SetBounds(bounds_rect); |
115 | 114 |
116 window_->Show(); | 115 window->Show(); |
117 // TODO(jamescook): Remove redundant call to Activate()? | 116 // TODO(jamescook): Remove redundant call to Activate()? |
118 window_->Activate(); | 117 window->Activate(); |
119 } | 118 } |
120 | 119 |
121 void ExtensionDialog::ObserverDestroyed() { | 120 void ExtensionDialog::ObserverDestroyed() { |
122 observer_ = NULL; | 121 observer_ = NULL; |
123 } | 122 } |
124 | 123 |
125 void ExtensionDialog::Close() { | |
126 if (!window_) | |
127 return; | |
128 | |
129 window_->Close(); | |
130 window_ = NULL; | |
131 } | |
132 | |
133 void ExtensionDialog::MaybeFocusRenderView() { | 124 void ExtensionDialog::MaybeFocusRenderView() { |
134 views::FocusManager* focus_manager = GetWidget()->GetFocusManager(); | 125 views::FocusManager* focus_manager = GetWidget()->GetFocusManager(); |
135 DCHECK(focus_manager != NULL); | 126 DCHECK(focus_manager != NULL); |
136 | 127 |
137 // Already there's a focused view, so no need to switch the focus. | 128 // Already there's a focused view, so no need to switch the focus. |
138 if (focus_manager->GetFocusedView()) | 129 if (focus_manager->GetFocusedView()) |
139 return; | 130 return; |
140 | 131 |
141 content::RenderWidgetHostView* view = host()->render_view_host()->GetView(); | 132 content::RenderWidgetHostView* view = host()->render_view_host()->GetView(); |
142 if (!view) | 133 if (!view) |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 extension_host_->view()->set_background(NULL); | 209 extension_host_->view()->set_background(NULL); |
219 // The render view is created during the LoadURL(), so we should | 210 // The render view is created during the LoadURL(), so we should |
220 // set the focus to the view if nobody else takes the focus. | 211 // set the focus to the view if nobody else takes the focus. |
221 if (content::Details<extensions::ExtensionHost>(host()) == details) | 212 if (content::Details<extensions::ExtensionHost>(host()) == details) |
222 MaybeFocusRenderView(); | 213 MaybeFocusRenderView(); |
223 break; | 214 break; |
224 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: | 215 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: |
225 // If we aren't the host of the popup, then disregard the notification. | 216 // If we aren't the host of the popup, then disregard the notification. |
226 if (content::Details<extensions::ExtensionHost>(host()) != details) | 217 if (content::Details<extensions::ExtensionHost>(host()) != details) |
227 return; | 218 return; |
228 Close(); | 219 GetWidget()->Close(); |
229 break; | 220 break; |
230 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: | 221 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: |
231 if (content::Details<extensions::ExtensionHost>(host()) != details) | 222 if (content::Details<extensions::ExtensionHost>(host()) != details) |
232 return; | 223 return; |
233 if (observer_) | 224 if (observer_) |
234 observer_->ExtensionTerminated(this); | 225 observer_->ExtensionTerminated(this); |
235 break; | 226 break; |
236 default: | 227 default: |
237 NOTREACHED() << L"Received unexpected notification"; | 228 NOTREACHED() << L"Received unexpected notification"; |
238 break; | 229 break; |
239 } | 230 } |
240 } | 231 } |
OLD | NEW |