OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/extension_view_host.h" | 5 #include "chrome/browser/extensions/extension_view_host.h" |
6 | 6 |
7 #include "base/strings/string_piece.h" | 7 #include "base/strings/string_piece.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 9 #include "chrome/browser/extensions/extension_view.h" |
9 #include "chrome/browser/extensions/window_controller.h" | 10 #include "chrome/browser/extensions/window_controller.h" |
10 #include "chrome/browser/file_select_helper.h" | 11 #include "chrome/browser/file_select_helper.h" |
11 #include "chrome/browser/platform_util.h" | 12 #include "chrome/browser/platform_util.h" |
12 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/browser_dialogs.h" | 14 #include "chrome/browser/ui/browser_dialogs.h" |
14 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 15 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
15 #include "content/public/browser/notification_source.h" | 16 #include "content/public/browser/notification_source.h" |
16 #include "content/public/browser/render_view_host.h" | 17 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "extensions/browser/extension_system.h" | 19 #include "extensions/browser/extension_system.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 ExtensionViewHost::~ExtensionViewHost() { | 70 ExtensionViewHost::~ExtensionViewHost() { |
70 // The hosting WebContents will be deleted in the base class, so unregister | 71 // The hosting WebContents will be deleted in the base class, so unregister |
71 // this object before it deletes the attached WebContentsModalDialogManager. | 72 // this object before it deletes the attached WebContentsModalDialogManager. |
72 WebContentsModalDialogManager* manager = | 73 WebContentsModalDialogManager* manager = |
73 WebContentsModalDialogManager::FromWebContents(host_contents()); | 74 WebContentsModalDialogManager::FromWebContents(host_contents()); |
74 if (manager) | 75 if (manager) |
75 manager->SetDelegate(NULL); | 76 manager->SetDelegate(NULL); |
76 } | 77 } |
77 | 78 |
78 void ExtensionViewHost::CreateView(Browser* browser) { | 79 void ExtensionViewHost::CreateView(Browser* browser) { |
79 #if defined(TOOLKIT_VIEWS) | 80 view_ = CreateExtensionView(this, browser); |
80 view_.reset(new ExtensionViewViews(this, browser)); | |
81 // We own |view_|, so don't auto delete when it's removed from the view | |
82 // hierarchy. | |
83 view_->set_owned_by_client(); | |
84 #elif defined(OS_MACOSX) | |
85 view_.reset(new ExtensionViewMac(this, browser)); | |
86 view_->Init(); | 81 view_->Init(); |
87 #else | |
88 // TODO(port) | |
89 NOTREACHED(); | |
90 #endif | |
91 } | 82 } |
92 | 83 |
93 void ExtensionViewHost::SetAssociatedWebContents(WebContents* web_contents) { | 84 void ExtensionViewHost::SetAssociatedWebContents(WebContents* web_contents) { |
94 associated_web_contents_ = web_contents; | 85 associated_web_contents_ = web_contents; |
95 if (associated_web_contents_) { | 86 if (associated_web_contents_) { |
96 // Observe the new WebContents for deletion. | 87 // Observe the new WebContents for deletion. |
97 associated_web_contents_observer_.reset( | 88 associated_web_contents_observer_.reset( |
98 new AssociatedWebContentsObserver(this, associated_web_contents_)); | 89 new AssociatedWebContentsObserver(this, associated_web_contents_)); |
99 } else { | 90 } else { |
100 associated_web_contents_observer_.reset(); | 91 associated_web_contents_observer_.reset(); |
101 } | 92 } |
102 } | 93 } |
103 | 94 |
104 void ExtensionViewHost::UnhandledKeyboardEvent( | 95 void ExtensionViewHost::UnhandledKeyboardEvent( |
105 WebContents* source, | 96 WebContents* source, |
106 const content::NativeWebKeyboardEvent& event) { | 97 const content::NativeWebKeyboardEvent& event) { |
107 Browser* browser = view_->browser(); | 98 view_->HandleKeyboardEvent(source, event); |
108 if (browser) { | |
109 // Handle lower priority browser shortcuts such as Ctrl-f. | |
110 return browser->HandleKeyboardEvent(source, event); | |
111 } else { | |
112 #if defined(TOOLKIT_VIEWS) | |
113 // In case there's no Browser (e.g. for dialogs), pass it to | |
114 // ExtensionViewViews to handle accelerators. The view's FocusManager does | |
115 // not know anything about Browser accelerators, but might know others such | |
116 // as Ash's. | |
117 view_->HandleKeyboardEvent(event); | |
118 #endif | |
119 } | |
120 } | 99 } |
121 | 100 |
122 // ExtensionHost overrides: | 101 // ExtensionHost overrides: |
123 | 102 |
124 void ExtensionViewHost::OnDidStopLoading() { | 103 void ExtensionViewHost::OnDidStopLoading() { |
125 DCHECK(did_stop_loading()); | 104 DCHECK(did_stop_loading()); |
126 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX) | |
127 view_->DidStopLoading(); | 105 view_->DidStopLoading(); |
128 #endif | |
129 } | 106 } |
130 | 107 |
131 void ExtensionViewHost::OnDocumentAvailable() { | 108 void ExtensionViewHost::OnDocumentAvailable() { |
132 if (extension_host_type() == VIEW_TYPE_EXTENSION_INFOBAR) { | 109 if (extension_host_type() == VIEW_TYPE_EXTENSION_INFOBAR) { |
133 // No style sheet for other types, at the moment. | 110 // No style sheet for other types, at the moment. |
134 InsertInfobarCSS(); | 111 InsertInfobarCSS(); |
135 } | 112 } |
136 } | 113 } |
137 | 114 |
138 void ExtensionViewHost::LoadInitialURL() { | 115 void ExtensionViewHost::LoadInitialURL() { |
(...skipping 30 matching lines...) Expand all Loading... |
169 switch (params.disposition) { | 146 switch (params.disposition) { |
170 case SINGLETON_TAB: | 147 case SINGLETON_TAB: |
171 case NEW_FOREGROUND_TAB: | 148 case NEW_FOREGROUND_TAB: |
172 case NEW_BACKGROUND_TAB: | 149 case NEW_BACKGROUND_TAB: |
173 case NEW_POPUP: | 150 case NEW_POPUP: |
174 case NEW_WINDOW: | 151 case NEW_WINDOW: |
175 case SAVE_TO_DISK: | 152 case SAVE_TO_DISK: |
176 case OFF_THE_RECORD: { | 153 case OFF_THE_RECORD: { |
177 // Only allow these from hosts that are bound to a browser (e.g. popups). | 154 // Only allow these from hosts that are bound to a browser (e.g. popups). |
178 // Otherwise they are not driven by a user gesture. | 155 // Otherwise they are not driven by a user gesture. |
179 Browser* browser = view_->browser(); | 156 Browser* browser = view_->GetBrowser(); |
180 return browser ? browser->OpenURL(params) : NULL; | 157 return browser ? browser->OpenURL(params) : NULL; |
181 } | 158 } |
182 default: | 159 default: |
183 return NULL; | 160 return NULL; |
184 } | 161 } |
185 } | 162 } |
186 | 163 |
187 bool ExtensionViewHost::PreHandleKeyboardEvent( | 164 bool ExtensionViewHost::PreHandleKeyboardEvent( |
188 WebContents* source, | 165 WebContents* source, |
189 const NativeWebKeyboardEvent& event, | 166 const NativeWebKeyboardEvent& event, |
190 bool* is_keyboard_shortcut) { | 167 bool* is_keyboard_shortcut) { |
191 if (extension_host_type() == VIEW_TYPE_EXTENSION_POPUP && | 168 if (extension_host_type() == VIEW_TYPE_EXTENSION_POPUP && |
192 event.type == NativeWebKeyboardEvent::RawKeyDown && | 169 event.type == NativeWebKeyboardEvent::RawKeyDown && |
193 event.windowsKeyCode == ui::VKEY_ESCAPE) { | 170 event.windowsKeyCode == ui::VKEY_ESCAPE) { |
194 DCHECK(is_keyboard_shortcut != NULL); | 171 DCHECK(is_keyboard_shortcut != NULL); |
195 *is_keyboard_shortcut = true; | 172 *is_keyboard_shortcut = true; |
196 return false; | 173 return false; |
197 } | 174 } |
198 | 175 |
199 // Handle higher priority browser shortcuts such as Ctrl-w. | 176 // Handle higher priority browser shortcuts such as Ctrl-w. |
200 Browser* browser = view_->browser(); | 177 Browser* browser = view_->GetBrowser(); |
201 if (browser) | 178 if (browser) |
202 return browser->PreHandleKeyboardEvent(source, event, is_keyboard_shortcut); | 179 return browser->PreHandleKeyboardEvent(source, event, is_keyboard_shortcut); |
203 | 180 |
204 *is_keyboard_shortcut = false; | 181 *is_keyboard_shortcut = false; |
205 return false; | 182 return false; |
206 } | 183 } |
207 | 184 |
208 void ExtensionViewHost::HandleKeyboardEvent( | 185 void ExtensionViewHost::HandleKeyboardEvent( |
209 WebContents* source, | 186 WebContents* source, |
210 const NativeWebKeyboardEvent& event) { | 187 const NativeWebKeyboardEvent& event) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 web_modal::WebContentsModalDialogHost* | 249 web_modal::WebContentsModalDialogHost* |
273 ExtensionViewHost::GetWebContentsModalDialogHost() { | 250 ExtensionViewHost::GetWebContentsModalDialogHost() { |
274 return this; | 251 return this; |
275 } | 252 } |
276 | 253 |
277 bool ExtensionViewHost::IsWebContentsVisible(WebContents* web_contents) { | 254 bool ExtensionViewHost::IsWebContentsVisible(WebContents* web_contents) { |
278 return platform_util::IsVisible(web_contents->GetNativeView()); | 255 return platform_util::IsVisible(web_contents->GetNativeView()); |
279 } | 256 } |
280 | 257 |
281 gfx::NativeView ExtensionViewHost::GetHostView() const { | 258 gfx::NativeView ExtensionViewHost::GetHostView() const { |
282 return view_->native_view(); | 259 return view_->GetNativeView(); |
283 } | 260 } |
284 | 261 |
285 gfx::Point ExtensionViewHost::GetDialogPosition(const gfx::Size& size) { | 262 gfx::Point ExtensionViewHost::GetDialogPosition(const gfx::Size& size) { |
286 if (!GetVisibleWebContents()) | 263 if (!GetVisibleWebContents()) |
287 return gfx::Point(); | 264 return gfx::Point(); |
288 gfx::Rect bounds = GetVisibleWebContents()->GetViewBounds(); | 265 gfx::Rect bounds = GetVisibleWebContents()->GetViewBounds(); |
289 return gfx::Point( | 266 return gfx::Point( |
290 std::max(0, (bounds.width() - size.width()) / 2), | 267 std::max(0, (bounds.width() - size.width()) / 2), |
291 std::max(0, (bounds.height() - size.height()) / 2)); | 268 std::max(0, (bounds.height() - size.height()) / 2)); |
292 } | 269 } |
293 | 270 |
294 gfx::Size ExtensionViewHost::GetMaximumDialogSize() { | 271 gfx::Size ExtensionViewHost::GetMaximumDialogSize() { |
295 if (!GetVisibleWebContents()) | 272 if (!GetVisibleWebContents()) |
296 return gfx::Size(); | 273 return gfx::Size(); |
297 return GetVisibleWebContents()->GetViewBounds().size(); | 274 return GetVisibleWebContents()->GetViewBounds().size(); |
298 } | 275 } |
299 | 276 |
300 void ExtensionViewHost::AddObserver( | 277 void ExtensionViewHost::AddObserver( |
301 web_modal::ModalDialogHostObserver* observer) { | 278 web_modal::ModalDialogHostObserver* observer) { |
302 } | 279 } |
303 | 280 |
304 void ExtensionViewHost::RemoveObserver( | 281 void ExtensionViewHost::RemoveObserver( |
305 web_modal::ModalDialogHostObserver* observer) { | 282 web_modal::ModalDialogHostObserver* observer) { |
306 } | 283 } |
307 | 284 |
308 WindowController* ExtensionViewHost::GetExtensionWindowController() const { | 285 WindowController* ExtensionViewHost::GetExtensionWindowController() const { |
309 return view_->browser() ? view_->browser()->extension_window_controller() | 286 Browser* browser = view_->GetBrowser(); |
310 : NULL; | 287 return browser ? browser->extension_window_controller() : NULL; |
311 } | 288 } |
312 | 289 |
313 WebContents* ExtensionViewHost::GetAssociatedWebContents() const { | 290 WebContents* ExtensionViewHost::GetAssociatedWebContents() const { |
314 return associated_web_contents_; | 291 return associated_web_contents_; |
315 } | 292 } |
316 | 293 |
317 WebContents* ExtensionViewHost::GetVisibleWebContents() const { | 294 WebContents* ExtensionViewHost::GetVisibleWebContents() const { |
318 if (associated_web_contents_) | 295 if (associated_web_contents_) |
319 return associated_web_contents_; | 296 return associated_web_contents_; |
320 if (extension_host_type() == VIEW_TYPE_EXTENSION_POPUP) | 297 if (extension_host_type() == VIEW_TYPE_EXTENSION_POPUP) |
(...skipping 15 matching lines...) Expand all Loading... |
336 | 313 |
337 void ExtensionViewHost::InsertInfobarCSS() { | 314 void ExtensionViewHost::InsertInfobarCSS() { |
338 static const base::StringPiece css( | 315 static const base::StringPiece css( |
339 ResourceBundle::GetSharedInstance().GetRawDataResource( | 316 ResourceBundle::GetSharedInstance().GetRawDataResource( |
340 IDR_EXTENSIONS_INFOBAR_CSS)); | 317 IDR_EXTENSIONS_INFOBAR_CSS)); |
341 | 318 |
342 host_contents()->InsertCSS(css.as_string()); | 319 host_contents()->InsertCSS(css.as_string()); |
343 } | 320 } |
344 | 321 |
345 } // namespace extensions | 322 } // namespace extensions |
OLD | NEW |