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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/extension_view_host.h"
7 #include "base/strings/string_piece.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/extension_view.h"
10 #include "chrome/browser/extensions/window_controller.h"
11 #include "chrome/browser/file_select_helper.h"
12 #include "chrome/browser/platform_util.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_dialogs.h"
15 #include "components/web_modal/web_contents_modal_dialog_manager.h"
16 #include "content/public/browser/notification_source.h"
17 #include "content/public/browser/render_view_host.h"
18 #include "content/public/browser/web_contents.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/browser/runtime_data.h"
21 #include "extensions/common/extension_messages.h"
22 #include "grit/browser_resources.h"
23 #include "third_party/WebKit/public/web/WebInputEvent.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/events/keycodes/keyboard_codes.h"
27 using content::NativeWebKeyboardEvent
;
28 using content::OpenURLParams
;
29 using content::RenderViewHost
;
30 using content::WebContents
;
31 using content::WebContentsObserver
;
32 using web_modal::WebContentsModalDialogManager
;
34 namespace extensions
{
36 // Notifies an ExtensionViewHost when a WebContents is destroyed.
37 class ExtensionViewHost::AssociatedWebContentsObserver
38 : public WebContentsObserver
{
40 AssociatedWebContentsObserver(ExtensionViewHost
* host
,
41 WebContents
* web_contents
)
42 : WebContentsObserver(web_contents
), host_(host
) {}
43 ~AssociatedWebContentsObserver() override
{}
45 // content::WebContentsObserver:
46 void WebContentsDestroyed() override
{
47 // Deleting |this| from here is safe.
48 host_
->SetAssociatedWebContents(NULL
);
52 ExtensionViewHost
* host_
;
54 DISALLOW_COPY_AND_ASSIGN(AssociatedWebContentsObserver
);
57 ExtensionViewHost::ExtensionViewHost(
58 const Extension
* extension
,
59 content::SiteInstance
* site_instance
,
62 : ExtensionHost(extension
, site_instance
, url
, host_type
),
63 associated_web_contents_(NULL
) {
64 // Not used for panels, see PanelHost.
65 DCHECK(host_type
== VIEW_TYPE_EXTENSION_DIALOG
||
66 host_type
== VIEW_TYPE_EXTENSION_POPUP
);
69 ExtensionViewHost::~ExtensionViewHost() {
70 // The hosting WebContents will be deleted in the base class, so unregister
71 // this object before it deletes the attached WebContentsModalDialogManager.
72 WebContentsModalDialogManager
* manager
=
73 WebContentsModalDialogManager::FromWebContents(host_contents());
75 manager
->SetDelegate(NULL
);
78 void ExtensionViewHost::CreateView(Browser
* browser
) {
79 view_
= CreateExtensionView(this, browser
);
82 void ExtensionViewHost::SetAssociatedWebContents(WebContents
* web_contents
) {
83 associated_web_contents_
= web_contents
;
84 if (associated_web_contents_
) {
85 // Observe the new WebContents for deletion.
86 associated_web_contents_observer_
.reset(
87 new AssociatedWebContentsObserver(this, associated_web_contents_
));
89 associated_web_contents_observer_
.reset();
93 void ExtensionViewHost::UnhandledKeyboardEvent(
95 const content::NativeWebKeyboardEvent
& event
) {
96 view_
->HandleKeyboardEvent(source
, event
);
99 // ExtensionHost overrides:
101 void ExtensionViewHost::OnDidStopLoading() {
102 DCHECK(did_stop_loading());
103 view_
->DidStopLoading();
106 void ExtensionViewHost::LoadInitialURL() {
107 if (!ExtensionSystem::Get(browser_context())->
108 runtime_data()->IsBackgroundPageReady(extension())) {
109 // Make sure the background page loads before any others.
111 extensions::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY
,
112 content::Source
<Extension
>(extension()));
116 // Popups may spawn modal dialogs, which need positioning information.
117 if (extension_host_type() == VIEW_TYPE_EXTENSION_POPUP
) {
118 WebContentsModalDialogManager::CreateForWebContents(host_contents());
119 WebContentsModalDialogManager::FromWebContents(
120 host_contents())->SetDelegate(this);
121 if (!popup_manager_
.get())
122 popup_manager_
.reset(new web_modal::PopupManager(this));
123 popup_manager_
->RegisterWith(host_contents());
126 ExtensionHost::LoadInitialURL();
129 bool ExtensionViewHost::IsBackgroundPage() const {
134 // content::WebContentsDelegate overrides:
136 WebContents
* ExtensionViewHost::OpenURLFromTab(
138 const OpenURLParams
& params
) {
139 // Whitelist the dispositions we will allow to be opened.
140 switch (params
.disposition
) {
142 case NEW_FOREGROUND_TAB
:
143 case NEW_BACKGROUND_TAB
:
147 case OFF_THE_RECORD
: {
148 // Only allow these from hosts that are bound to a browser (e.g. popups).
149 // Otherwise they are not driven by a user gesture.
150 Browser
* browser
= view_
->GetBrowser();
151 return browser
? browser
->OpenURL(params
) : NULL
;
158 bool ExtensionViewHost::PreHandleKeyboardEvent(
160 const NativeWebKeyboardEvent
& event
,
161 bool* is_keyboard_shortcut
) {
162 if (extension_host_type() == VIEW_TYPE_EXTENSION_POPUP
&&
163 event
.type
== NativeWebKeyboardEvent::RawKeyDown
&&
164 event
.windowsKeyCode
== ui::VKEY_ESCAPE
) {
165 DCHECK(is_keyboard_shortcut
!= NULL
);
166 *is_keyboard_shortcut
= true;
170 // Handle higher priority browser shortcuts such as Ctrl-w.
171 Browser
* browser
= view_
->GetBrowser();
173 return browser
->PreHandleKeyboardEvent(source
, event
, is_keyboard_shortcut
);
175 *is_keyboard_shortcut
= false;
179 void ExtensionViewHost::HandleKeyboardEvent(
181 const NativeWebKeyboardEvent
& event
) {
182 if (extension_host_type() == VIEW_TYPE_EXTENSION_POPUP
) {
183 if (event
.type
== NativeWebKeyboardEvent::RawKeyDown
&&
184 event
.windowsKeyCode
== ui::VKEY_ESCAPE
) {
189 UnhandledKeyboardEvent(source
, event
);
192 bool ExtensionViewHost::PreHandleGestureEvent(
193 content::WebContents
* source
,
194 const blink::WebGestureEvent
& event
) {
195 // Disable pinch zooming.
196 return event
.type
== blink::WebGestureEvent::GesturePinchBegin
||
197 event
.type
== blink::WebGestureEvent::GesturePinchUpdate
||
198 event
.type
== blink::WebGestureEvent::GesturePinchEnd
;
201 content::ColorChooser
* ExtensionViewHost::OpenColorChooser(
202 WebContents
* web_contents
,
203 SkColor initial_color
,
204 const std::vector
<content::ColorSuggestion
>& suggestions
) {
205 // Similar to the file chooser below, opening a color chooser requires a
206 // visible <input> element to click on. Therefore this code only exists for
207 // extensions with a view.
208 return chrome::ShowColorChooser(web_contents
, initial_color
);
211 void ExtensionViewHost::RunFileChooser(
213 const content::FileChooserParams
& params
) {
214 // For security reasons opening a file picker requires a visible <input>
215 // element to click on, so this code only exists for extensions with a view.
216 FileSelectHelper::RunFileChooser(tab
, params
);
220 void ExtensionViewHost::ResizeDueToAutoResize(WebContents
* source
,
221 const gfx::Size
& new_size
) {
222 view_
->ResizeDueToAutoResize(new_size
);
225 // content::WebContentsObserver overrides:
227 void ExtensionViewHost::RenderViewCreated(RenderViewHost
* render_view_host
) {
228 ExtensionHost::RenderViewCreated(render_view_host
);
230 view_
->RenderViewCreated();
232 // If the host is bound to a window, then extract its id. Extensions hosted
233 // in ExternalTabContainer objects may not have an associated window.
234 WindowController
* window
= GetExtensionWindowController();
236 render_view_host
->Send(new ExtensionMsg_UpdateBrowserWindowId(
237 render_view_host
->GetRoutingID(), window
->GetWindowId()));
241 // web_modal::WebContentsModalDialogManagerDelegate overrides:
243 web_modal::WebContentsModalDialogHost
*
244 ExtensionViewHost::GetWebContentsModalDialogHost() {
248 bool ExtensionViewHost::IsWebContentsVisible(WebContents
* web_contents
) {
249 return platform_util::IsVisible(web_contents
->GetNativeView());
252 gfx::NativeView
ExtensionViewHost::GetHostView() const {
253 return view_
->GetNativeView();
256 gfx::Point
ExtensionViewHost::GetDialogPosition(const gfx::Size
& size
) {
257 if (!GetVisibleWebContents())
259 gfx::Rect bounds
= GetVisibleWebContents()->GetViewBounds();
261 std::max(0, (bounds
.width() - size
.width()) / 2),
262 std::max(0, (bounds
.height() - size
.height()) / 2));
265 gfx::Size
ExtensionViewHost::GetMaximumDialogSize() {
266 if (!GetVisibleWebContents())
268 return GetVisibleWebContents()->GetViewBounds().size();
271 void ExtensionViewHost::AddObserver(
272 web_modal::ModalDialogHostObserver
* observer
) {
275 void ExtensionViewHost::RemoveObserver(
276 web_modal::ModalDialogHostObserver
* observer
) {
279 WindowController
* ExtensionViewHost::GetExtensionWindowController() const {
280 Browser
* browser
= view_
->GetBrowser();
281 return browser
? browser
->extension_window_controller() : NULL
;
284 WebContents
* ExtensionViewHost::GetAssociatedWebContents() const {
285 return associated_web_contents_
;
288 WebContents
* ExtensionViewHost::GetVisibleWebContents() const {
289 if (associated_web_contents_
)
290 return associated_web_contents_
;
291 if (extension_host_type() == VIEW_TYPE_EXTENSION_POPUP
)
292 return host_contents();
296 void ExtensionViewHost::Observe(int type
,
297 const content::NotificationSource
& source
,
298 const content::NotificationDetails
& details
) {
299 DCHECK_EQ(type
, extensions::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY
);
300 DCHECK(ExtensionSystem::Get(browser_context())
302 ->IsBackgroundPageReady(extension()));
306 } // namespace extensions