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/base_window.h" |
11 #include "chrome/browser/ui/browser_window.h" | |
12 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" | 11 #include "chrome/browser/ui/views/extensions/extension_dialog_observer.h" |
13 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
14 #include "content/public/browser/notification_details.h" | 13 #include "content/public/browser/notification_details.h" |
15 #include "content/public/browser/notification_source.h" | 14 #include "content/public/browser/notification_source.h" |
16 #include "content/public/browser/render_view_host.h" | 15 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/render_widget_host_view.h" | 16 #include "content/public/browser/render_widget_host_view.h" |
18 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
19 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
20 #include "ui/gfx/screen.h" | 19 #include "ui/gfx/screen.h" |
21 #include "ui/views/background.h" | 20 #include "ui/views/background.h" |
(...skipping 22 matching lines...) Expand all Loading... |
44 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, | 43 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, |
45 content::Source<Profile>(host->profile())); | 44 content::Source<Profile>(host->profile())); |
46 } | 45 } |
47 | 46 |
48 ExtensionDialog::~ExtensionDialog() { | 47 ExtensionDialog::~ExtensionDialog() { |
49 } | 48 } |
50 | 49 |
51 // static | 50 // static |
52 ExtensionDialog* ExtensionDialog::Show( | 51 ExtensionDialog* ExtensionDialog::Show( |
53 const GURL& url, | 52 const GURL& url, |
54 Browser* browser, | 53 BaseWindow* base_window, |
| 54 Profile* profile, |
55 WebContents* web_contents, | 55 WebContents* web_contents, |
56 int width, | 56 int width, |
57 int height, | 57 int height, |
58 const string16& title, | 58 const string16& title, |
59 ExtensionDialogObserver* observer) { | 59 ExtensionDialogObserver* observer) { |
60 ExtensionHost* host = CreateExtensionHost(url, browser, NULL); | 60 ExtensionHost* host = CreateExtensionHost(url, profile); |
61 if (!host) | 61 if (!host) |
62 return NULL; | 62 return NULL; |
63 host->SetAssociatedWebContents(web_contents); | 63 host->SetAssociatedWebContents(web_contents); |
64 | 64 |
65 return ExtensionDialog::ShowInternal(url, browser, host, width, height, | 65 return ExtensionDialog::ShowInternal(url, base_window, host, width, height, |
66 false, title, observer); | 66 false, title, observer); |
67 } | 67 } |
68 | 68 |
69 #if defined(USE_AURA) | 69 #if defined(USE_AURA) |
70 // static | 70 // static |
71 ExtensionDialog* ExtensionDialog::ShowFullscreen( | 71 ExtensionDialog* ExtensionDialog::ShowFullscreen( |
72 const GURL& url, | 72 const GURL& url, |
73 Profile* profile, | 73 Profile* profile, |
74 const string16& title, | 74 const string16& title, |
75 ExtensionDialogObserver* observer) { | 75 ExtensionDialogObserver* observer) { |
76 ExtensionHost* host = CreateExtensionHost(url, NULL, profile); | 76 ExtensionHost* host = CreateExtensionHost(url, profile); |
77 if (!host) | 77 if (!host) |
78 return NULL; | 78 return NULL; |
79 | 79 |
80 return ExtensionDialog::ShowInternal(url, NULL, host, 0, 0, | 80 return ExtensionDialog::ShowInternal(url, NULL, host, 0, 0, |
81 true, title, observer); | 81 true, title, observer); |
82 } | 82 } |
83 #endif | 83 #endif |
84 | 84 |
85 // static | 85 // static |
86 ExtensionDialog* ExtensionDialog::ShowInternal(const GURL& url, | 86 ExtensionDialog* ExtensionDialog::ShowInternal(const GURL& url, |
87 Browser* browser, | 87 BaseWindow* base_window, |
88 ExtensionHost* host, | 88 ExtensionHost* host, |
89 int width, | 89 int width, |
90 int height, | 90 int height, |
91 bool fullscreen, | 91 bool fullscreen, |
92 const string16& title, | 92 const string16& title, |
93 ExtensionDialogObserver* observer) { | 93 ExtensionDialogObserver* observer) { |
94 CHECK(fullscreen || browser); | 94 CHECK(fullscreen || base_window); |
95 ExtensionDialog* dialog = new ExtensionDialog(host, observer); | 95 ExtensionDialog* dialog = new ExtensionDialog(host, observer); |
96 dialog->set_title(title); | 96 dialog->set_title(title); |
97 | 97 |
98 if (fullscreen) | 98 if (fullscreen) |
99 dialog->InitWindowFullscreen(); | 99 dialog->InitWindowFullscreen(); |
100 else | 100 else |
101 dialog->InitWindow(browser, width, height); | 101 dialog->InitWindow(base_window, width, height); |
102 | 102 |
103 // Show a white background while the extension loads. This is prettier than | 103 // Show a white background while the extension loads. This is prettier than |
104 // flashing a black unfilled window frame. | 104 // flashing a black unfilled window frame. |
105 host->view()->set_background( | 105 host->view()->set_background( |
106 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); | 106 views::Background::CreateSolidBackground(0xFF, 0xFF, 0xFF)); |
107 host->view()->SetVisible(true); | 107 host->view()->SetVisible(true); |
108 | 108 |
109 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. | 109 // Ensure the DOM JavaScript can respond immediately to keyboard shortcuts. |
110 host->host_contents()->Focus(); | 110 host->host_contents()->Focus(); |
111 return dialog; | 111 return dialog; |
112 } | 112 } |
113 | 113 |
114 // static | 114 // static |
115 ExtensionHost* ExtensionDialog::CreateExtensionHost(const GURL& url, | 115 ExtensionHost* ExtensionDialog::CreateExtensionHost(const GURL& url, |
116 Browser* browser, | |
117 Profile* profile) { | 116 Profile* profile) { |
118 // Prefer picking the extension manager from the profile if given. | 117 DCHECK(profile); |
119 ExtensionProcessManager* manager = NULL; | 118 ExtensionProcessManager* manager = profile->GetExtensionProcessManager(); |
120 if (profile) | |
121 manager = profile->GetExtensionProcessManager(); | |
122 else | |
123 manager = browser->profile()->GetExtensionProcessManager(); | |
124 | 119 |
125 DCHECK(manager); | 120 DCHECK(manager); |
126 if (!manager) | 121 if (!manager) |
127 return NULL; | 122 return NULL; |
128 return manager->CreateDialogHost(url, browser); | 123 return manager->CreateDialogHost(url); |
129 } | 124 } |
130 | 125 |
131 #if defined(USE_AURA) | 126 #if defined(USE_AURA) |
132 void ExtensionDialog::InitWindowFullscreen() { | 127 void ExtensionDialog::InitWindowFullscreen() { |
133 aura::RootWindow* root_window = ash::Shell::GetPrimaryRootWindow(); | 128 aura::RootWindow* root_window = ash::Shell::GetPrimaryRootWindow(); |
134 gfx::Rect screen_rect = | 129 gfx::Rect screen_rect = |
135 gfx::Screen::GetDisplayNearestWindow(root_window).bounds(); | 130 gfx::Screen::GetDisplayNearestWindow(root_window).bounds(); |
136 | 131 |
137 // We want to be the fullscreen topmost child of the root window. | 132 // We want to be the fullscreen topmost child of the root window. |
138 window_ = new views::Widget; | 133 window_ = new views::Widget; |
139 views::Widget::InitParams params( | 134 views::Widget::InitParams params( |
140 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 135 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
141 params.delegate = this; | 136 params.delegate = this; |
142 params.parent = root_window; | 137 params.parent = root_window; |
143 window_->Init(params); | 138 window_->Init(params); |
144 window_->StackAtTop(); | 139 window_->StackAtTop(); |
145 window_->SetBounds(screen_rect); | 140 window_->SetBounds(screen_rect); |
146 window_->Show(); | 141 window_->Show(); |
147 | 142 |
148 // TODO(jamescook): Remove redundant call to Activate()? | 143 // TODO(jamescook): Remove redundant call to Activate()? |
149 window_->Activate(); | 144 window_->Activate(); |
150 } | 145 } |
151 #else | 146 #else |
152 void ExtensionDialog::InitWindowFullscreen() { | 147 void ExtensionDialog::InitWindowFullscreen() { |
153 NOTIMPLEMENTED(); | 148 NOTIMPLEMENTED(); |
154 } | 149 } |
155 #endif | 150 #endif |
156 | 151 |
157 | 152 |
158 void ExtensionDialog::InitWindow(Browser* browser, int width, int height) { | 153 void ExtensionDialog::InitWindow(BaseWindow* base_window, |
159 gfx::NativeWindow parent = browser->window()->GetNativeWindow(); | 154 int width, |
| 155 int height) { |
| 156 gfx::NativeWindow parent = base_window->GetNativeWindow(); |
160 window_ = views::Widget::CreateWindowWithParent(this, parent); | 157 window_ = views::Widget::CreateWindowWithParent(this, parent); |
161 | 158 |
162 // Center the window over the browser. | 159 // Center the window over the browser. |
163 gfx::Point center = browser->window()->GetBounds().CenterPoint(); | 160 gfx::Point center = base_window->GetBounds().CenterPoint(); |
164 int x = center.x() - width / 2; | 161 int x = center.x() - width / 2; |
165 int y = center.y() - height / 2; | 162 int y = center.y() - height / 2; |
166 window_->SetBounds(gfx::Rect(x, y, width, height)); | 163 // Ensure the top left and top right of the window are on screen, with |
| 164 // priority given to the top left. |
| 165 gfx::Rect screen_rect = gfx::Screen::GetDisplayNearestPoint(center).bounds(); |
| 166 gfx::Rect bounds_rect = gfx::Rect(x, y, width, height); |
| 167 bounds_rect = bounds_rect.AdjustToFit(screen_rect); |
| 168 window_->SetBounds(bounds_rect); |
167 | 169 |
168 window_->Show(); | 170 window_->Show(); |
169 // TODO(jamescook): Remove redundant call to Activate()? | 171 // TODO(jamescook): Remove redundant call to Activate()? |
170 window_->Activate(); | 172 window_->Activate(); |
171 } | 173 } |
172 | 174 |
173 void ExtensionDialog::ObserverDestroyed() { | 175 void ExtensionDialog::ObserverDestroyed() { |
174 observer_ = NULL; | 176 observer_ = NULL; |
175 } | 177 } |
176 | 178 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 if (content::Details<ExtensionHost>(host()) != details) | 264 if (content::Details<ExtensionHost>(host()) != details) |
263 return; | 265 return; |
264 if (observer_) | 266 if (observer_) |
265 observer_->ExtensionTerminated(this); | 267 observer_->ExtensionTerminated(this); |
266 break; | 268 break; |
267 default: | 269 default: |
268 NOTREACHED() << L"Received unexpected notification"; | 270 NOTREACHED() << L"Received unexpected notification"; |
269 break; | 271 break; |
270 } | 272 } |
271 } | 273 } |
OLD | NEW |