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 "content/public/browser/render_process_host.h"
10 #include "content/public/browser/site_instance.h"
11 #include "content/public/browser/web_contents.h"
12 #include "extensions/browser/api/extensions_api_client.h"
13 #include "extensions/browser/extension_function_dispatcher.h"
14 #include "extensions/browser/extension_registry.h"
15 #include "extensions/browser/extension_web_contents_observer.h"
16 #include "extensions/browser/guest_view/extension_options/extension_options_constants.h"
17 #include "extensions/browser/guest_view/extension_options/extension_options_guest_delegate.h"
18 #include "extensions/browser/guest_view/guest_view_manager.h"
19 #include "extensions/common/api/extension_options_internal.h"
20 #include "extensions/common/constants.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/extension_messages.h"
23 #include "extensions/common/manifest_handlers/options_page_info.h"
24 #include "extensions/common/permissions/permissions_data.h"
25 #include "extensions/strings/grit/extensions_strings.h"
26 #include "ipc/ipc_message_macros.h"
28 using content::WebContents;
29 using namespace extensions::core_api;
31 namespace extensions {
34 const char ExtensionOptionsGuest::Type[] = "extensionoptions";
36 ExtensionOptionsGuest::ExtensionOptionsGuest(
37 content::BrowserContext* browser_context,
38 int guest_instance_id)
39 : GuestView<ExtensionOptionsGuest>(browser_context, guest_instance_id),
40 extension_options_guest_delegate_(
41 extensions::ExtensionsAPIClient::Get()
42 ->CreateExtensionOptionsGuestDelegate(this)),
43 has_navigated_(false) {
46 ExtensionOptionsGuest::~ExtensionOptionsGuest() {
50 extensions::GuestViewBase* ExtensionOptionsGuest::Create(
51 content::BrowserContext* browser_context,
52 int guest_instance_id) {
53 return new ExtensionOptionsGuest(browser_context, guest_instance_id);
56 void ExtensionOptionsGuest::CreateWebContents(
57 const std::string& embedder_extension_id,
58 int embedder_render_process_id,
59 const GURL& embedder_site_url,
60 const base::DictionaryValue& create_params,
61 const WebContentsCreatedCallback& callback) {
62 // Get the extension's base URL.
63 std::string extension_id;
64 create_params.GetString(extensionoptions::kExtensionId, &extension_id);
66 if (!crx_file::id_util::IdIsValid(extension_id)) {
71 if (crx_file::id_util::IdIsValid(embedder_extension_id) &&
72 extension_id != embedder_extension_id) {
73 // Extensions cannot embed other extensions' options pages.
79 extensions::Extension::GetBaseURLFromExtensionId(extension_id);
80 if (!extension_url.is_valid()) {
85 // Get the options page URL for later use.
86 extensions::ExtensionRegistry* registry =
87 extensions::ExtensionRegistry::Get(browser_context());
88 const extensions::Extension* extension =
89 registry->enabled_extensions().GetByID(extension_id);
91 // The ID was valid but the extension didn't exist. Typically this will
92 // happen when an extension is disabled.
97 options_page_ = extensions::OptionsPageInfo::GetOptionsPage(extension);
98 if (!options_page_.is_valid()) {
103 // Create a WebContents using the extension URL. The options page's
104 // WebContents should live in the same process as its parent extension's
105 // WebContents, so we can use |extension_url| for creating the SiteInstance.
106 content::SiteInstance* options_site_instance =
107 content::SiteInstance::CreateForURL(browser_context(), extension_url);
108 WebContents::CreateParams params(browser_context(), options_site_instance);
109 params.guest_delegate = this;
110 callback.Run(WebContents::Create(params));
113 void ExtensionOptionsGuest::DidAttachToEmbedder() {
116 // We should not re-navigate on reattachment.
120 web_contents()->GetController().LoadURL(options_page_,
122 ui::PAGE_TRANSITION_LINK,
124 has_navigated_ = true;
127 void ExtensionOptionsGuest::DidInitialize() {
128 extension_function_dispatcher_.reset(
129 new extensions::ExtensionFunctionDispatcher(browser_context(), this));
130 if (extension_options_guest_delegate_) {
131 extension_options_guest_delegate_->DidInitialize();
135 void ExtensionOptionsGuest::DidStopLoading() {
136 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
137 DispatchEventToEmbedder(new extensions::GuestViewBase::Event(
138 extension_options_internal::OnLoad::kEventName, args.Pass()));
141 const char* ExtensionOptionsGuest::GetAPINamespace() const {
142 return extensionoptions::kAPINamespace;
145 int ExtensionOptionsGuest::GetTaskPrefix() const {
146 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONOPTIONS_TAG_PREFIX;
149 void ExtensionOptionsGuest::GuestSizeChangedDueToAutoSize(
150 const gfx::Size& old_size,
151 const gfx::Size& new_size) {
152 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
153 args->SetInteger(extensionoptions::kNewWidth, new_size.width());
154 args->SetInteger(extensionoptions::kNewHeight, new_size.height());
155 args->SetInteger(extensionoptions::kOldWidth, old_size.width());
156 args->SetInteger(extensionoptions::kOldHeight, old_size.height());
157 DispatchEventToEmbedder(new extensions::GuestViewBase::Event(
158 extension_options_internal::OnSizeChanged::kEventName, args.Pass()));
161 bool ExtensionOptionsGuest::IsAutoSizeSupported() const {
165 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const {
166 return web_contents();
169 content::WebContents* ExtensionOptionsGuest::OpenURLFromTab(
170 content::WebContents* source,
171 const content::OpenURLParams& params) {
172 if (!extension_options_guest_delegate_)
175 // Don't allow external URLs with the CURRENT_TAB disposition be opened in
176 // this guest view, change the disposition to NEW_FOREGROUND_TAB.
177 if ((!params.url.SchemeIs(extensions::kExtensionScheme) ||
178 params.url.host() != options_page_.host()) &&
179 params.disposition == CURRENT_TAB) {
180 return extension_options_guest_delegate_->OpenURLInNewTab(
181 content::OpenURLParams(params.url,
183 params.frame_tree_node_id,
186 params.is_renderer_initiated));
188 return extension_options_guest_delegate_->OpenURLInNewTab(params);
191 void ExtensionOptionsGuest::CloseContents(content::WebContents* source) {
192 DispatchEventToEmbedder(new extensions::GuestViewBase::Event(
193 extension_options_internal::OnClose::kEventName,
194 make_scoped_ptr(new base::DictionaryValue())));
197 bool ExtensionOptionsGuest::HandleContextMenu(
198 const content::ContextMenuParams& params) {
199 if (!extension_options_guest_delegate_)
202 return extension_options_guest_delegate_->HandleContextMenu(params);
205 bool ExtensionOptionsGuest::ShouldCreateWebContents(
206 content::WebContents* web_contents,
208 WindowContainerType window_container_type,
209 const base::string16& frame_name,
210 const GURL& target_url,
211 const std::string& partition_id,
212 content::SessionStorageNamespace* session_storage_namespace) {
213 // This method handles opening links from within the guest. Since this guest
214 // view is used for displaying embedded extension options, we want any
215 // external links to be opened in a new tab, not in a new guest view.
216 // Therefore we just open the URL in a new tab, and since we aren't handling
217 // the new web contents, we return false.
218 // TODO(ericzeng): Open the tab in the background if the click was a
219 // ctrl-click or middle mouse button click
220 if (extension_options_guest_delegate_) {
221 extension_options_guest_delegate_->OpenURLInNewTab(
222 content::OpenURLParams(target_url,
225 ui::PAGE_TRANSITION_LINK,
231 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) {
233 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message)
234 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
235 IPC_MESSAGE_UNHANDLED(handled = false)
236 IPC_END_MESSAGE_MAP()
240 void ExtensionOptionsGuest::OnRequest(
241 const ExtensionHostMsg_Request_Params& params) {
242 extension_function_dispatcher_->Dispatch(params,
243 web_contents()->GetRenderViewHost());
246 void ExtensionOptionsGuest::SetUpAutoSize() {
247 // Read the autosize parameters passed in from the embedder.
248 bool auto_size_enabled = false;
249 attach_params()->GetBoolean(extensionoptions::kAttributeAutoSize,
254 attach_params()->GetInteger(extensionoptions::kAttributeMaxHeight,
256 attach_params()->GetInteger(extensionoptions::kAttributeMaxWidth, &max_width);
260 attach_params()->GetInteger(extensionoptions::kAttributeMinHeight,
262 attach_params()->GetInteger(extensionoptions::kAttributeMinWidth, &min_width);
264 // Call SetAutoSize to apply all the appropriate validation and clipping of
266 SetAutoSize(auto_size_enabled,
267 gfx::Size(min_width, min_height),
268 gfx::Size(max_width, max_height));
271 } // namespace extensions