1 // Copyright 2014 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 "extensions/browser/guest_view/extension_options/extension_options_guest.h"
7 #include "base/values.h"
8 #include "components/crx_file/id_util.h"
9 #include "components/guest_view/browser/guest_view_event.h"
10 #include "components/guest_view/browser/guest_view_manager.h"
11 #include "content/public/browser/navigation_details.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/site_instance.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/result_codes.h"
16 #include "extensions/browser/api/extensions_api_client.h"
17 #include "extensions/browser/bad_message.h"
18 #include "extensions/browser/extension_function_dispatcher.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/extension_web_contents_observer.h"
21 #include "extensions/browser/guest_view/extension_options/extension_options_constants.h"
22 #include "extensions/browser/guest_view/extension_options/extension_options_guest_delegate.h"
23 #include "extensions/common/api/extension_options_internal.h"
24 #include "extensions/common/constants.h"
25 #include "extensions/common/extension.h"
26 #include "extensions/common/extension_messages.h"
27 #include "extensions/common/manifest_handlers/options_page_info.h"
28 #include "extensions/common/permissions/permissions_data.h"
29 #include "extensions/strings/grit/extensions_strings.h"
30 #include "ipc/ipc_message_macros.h"
32 using content::WebContents
;
33 using guest_view::GuestViewBase
;
34 using guest_view::GuestViewEvent
;
35 using namespace extensions::core_api
;
37 namespace extensions
{
40 const char ExtensionOptionsGuest::Type
[] = "extensionoptions";
42 ExtensionOptionsGuest::ExtensionOptionsGuest(
43 content::WebContents
* owner_web_contents
)
44 : GuestView
<ExtensionOptionsGuest
>(owner_web_contents
),
45 extension_options_guest_delegate_(
46 extensions::ExtensionsAPIClient::Get()
47 ->CreateExtensionOptionsGuestDelegate(this)) {
50 ExtensionOptionsGuest::~ExtensionOptionsGuest() {
54 GuestViewBase
* ExtensionOptionsGuest::Create(
55 content::WebContents
* owner_web_contents
) {
56 return new ExtensionOptionsGuest(owner_web_contents
);
59 bool ExtensionOptionsGuest::CanRunInDetachedState() const {
63 void ExtensionOptionsGuest::CreateWebContents(
64 const base::DictionaryValue
& create_params
,
65 const WebContentsCreatedCallback
& callback
) {
66 // Get the extension's base URL.
67 std::string extension_id
;
68 create_params
.GetString(extensionoptions::kExtensionId
, &extension_id
);
70 if (!crx_file::id_util::IdIsValid(extension_id
)) {
71 callback
.Run(nullptr);
75 std::string embedder_extension_id
= GetOwnerSiteURL().host();
76 if (crx_file::id_util::IdIsValid(embedder_extension_id
) &&
77 extension_id
!= embedder_extension_id
) {
78 // Extensions cannot embed other extensions' options pages.
79 callback
.Run(nullptr);
84 extensions::Extension::GetBaseURLFromExtensionId(extension_id
);
85 if (!extension_url
.is_valid()) {
86 callback
.Run(nullptr);
90 // Get the options page URL for later use.
91 extensions::ExtensionRegistry
* registry
=
92 extensions::ExtensionRegistry::Get(browser_context());
93 const extensions::Extension
* extension
=
94 registry
->enabled_extensions().GetByID(extension_id
);
96 // The ID was valid but the extension didn't exist. Typically this will
97 // happen when an extension is disabled.
98 callback
.Run(nullptr);
102 options_page_
= extensions::OptionsPageInfo::GetOptionsPage(extension
);
103 if (!options_page_
.is_valid()) {
104 callback
.Run(nullptr);
108 // Create a WebContents using the extension URL. The options page's
109 // WebContents should live in the same process as its parent extension's
110 // WebContents, so we can use |extension_url| for creating the SiteInstance.
111 content::SiteInstance
* options_site_instance
=
112 content::SiteInstance::CreateForURL(browser_context(), extension_url
);
113 WebContents::CreateParams
params(browser_context(), options_site_instance
);
114 params
.guest_delegate
= this;
115 callback
.Run(WebContents::Create(params
));
118 void ExtensionOptionsGuest::DidInitialize(
119 const base::DictionaryValue
& create_params
) {
120 extension_function_dispatcher_
.reset(
121 new extensions::ExtensionFunctionDispatcher(browser_context(), this));
122 if (extension_options_guest_delegate_
) {
123 extension_options_guest_delegate_
->DidInitialize();
125 web_contents()->GetController().LoadURL(options_page_
,
127 ui::PAGE_TRANSITION_LINK
,
131 void ExtensionOptionsGuest::GuestViewDidStopLoading() {
132 scoped_ptr
<base::DictionaryValue
> args(new base::DictionaryValue());
133 DispatchEventToView(new GuestViewEvent(
134 extension_options_internal::OnLoad::kEventName
, args
.Pass()));
137 const char* ExtensionOptionsGuest::GetAPINamespace() const {
138 return extensionoptions::kAPINamespace
;
141 int ExtensionOptionsGuest::GetTaskPrefix() const {
142 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONOPTIONS_TAG_PREFIX
;
145 bool ExtensionOptionsGuest::IsPreferredSizeModeEnabled() const {
149 bool ExtensionOptionsGuest::IsDragAndDropEnabled() const {
153 void ExtensionOptionsGuest::OnPreferredSizeChanged(const gfx::Size
& pref_size
) {
154 extension_options_internal::PreferredSizeChangedOptions options
;
155 // Convert the size from physical pixels to logical pixels.
156 options
.width
= PhysicalPixelsToLogicalPixels(pref_size
.width());
157 options
.height
= PhysicalPixelsToLogicalPixels(pref_size
.height());
158 DispatchEventToView(new GuestViewEvent(
159 extension_options_internal::OnPreferredSizeChanged::kEventName
,
163 content::WebContents
* ExtensionOptionsGuest::GetAssociatedWebContents() const {
164 return web_contents();
167 content::WebContents
* ExtensionOptionsGuest::OpenURLFromTab(
168 content::WebContents
* source
,
169 const content::OpenURLParams
& params
) {
170 if (!extension_options_guest_delegate_
)
173 // Don't allow external URLs with the CURRENT_TAB disposition be opened in
174 // this guest view, change the disposition to NEW_FOREGROUND_TAB.
175 if ((!params
.url
.SchemeIs(extensions::kExtensionScheme
) ||
176 params
.url
.host() != options_page_
.host()) &&
177 params
.disposition
== CURRENT_TAB
) {
178 return extension_options_guest_delegate_
->OpenURLInNewTab(
179 content::OpenURLParams(params
.url
,
181 params
.frame_tree_node_id
,
184 params
.is_renderer_initiated
));
186 return extension_options_guest_delegate_
->OpenURLInNewTab(params
);
189 void ExtensionOptionsGuest::CloseContents(content::WebContents
* source
) {
191 new GuestViewEvent(extension_options_internal::OnClose::kEventName
,
192 make_scoped_ptr(new base::DictionaryValue())));
195 bool ExtensionOptionsGuest::HandleContextMenu(
196 const content::ContextMenuParams
& params
) {
197 if (!extension_options_guest_delegate_
)
200 return extension_options_guest_delegate_
->HandleContextMenu(params
);
203 bool ExtensionOptionsGuest::ShouldCreateWebContents(
204 content::WebContents
* web_contents
,
206 int main_frame_route_id
,
207 WindowContainerType window_container_type
,
208 const base::string16
& frame_name
,
209 const GURL
& target_url
,
210 const std::string
& partition_id
,
211 content::SessionStorageNamespace
* session_storage_namespace
) {
212 // This method handles opening links from within the guest. Since this guest
213 // view is used for displaying embedded extension options, we want any
214 // external links to be opened in a new tab, not in a new guest view.
215 // Therefore we just open the URL in a new tab, and since we aren't handling
216 // the new web contents, we return false.
217 // TODO(ericzeng): Open the tab in the background if the click was a
218 // ctrl-click or middle mouse button click
219 if (extension_options_guest_delegate_
) {
220 extension_options_guest_delegate_
->OpenURLInNewTab(
221 content::OpenURLParams(target_url
,
224 ui::PAGE_TRANSITION_LINK
,
230 void ExtensionOptionsGuest::DidNavigateMainFrame(
231 const content::LoadCommittedDetails
& details
,
232 const content::FrameNavigateParams
& params
) {
234 auto guest_zoom_controller
=
235 ui_zoom::ZoomController::FromWebContents(web_contents());
236 guest_zoom_controller
->SetZoomMode(
237 ui_zoom::ZoomController::ZOOM_MODE_ISOLATED
);
238 SetGuestZoomLevelToMatchEmbedder();
240 if (params
.url
.GetOrigin() != options_page_
.GetOrigin()) {
241 bad_message::ReceivedBadMessage(web_contents()->GetRenderProcessHost(),
242 bad_message::EOG_BAD_ORIGIN
);
247 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message
& message
) {
249 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest
, message
)
250 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request
, OnRequest
)
251 IPC_MESSAGE_UNHANDLED(handled
= false)
252 IPC_END_MESSAGE_MAP()
256 void ExtensionOptionsGuest::OnRequest(
257 const ExtensionHostMsg_Request_Params
& params
) {
258 extension_function_dispatcher_
->Dispatch(params
,
259 web_contents()->GetRenderViewHost());
262 } // namespace extensions