Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / extensions / browser / guest_view / extension_options / extension_options_guest.cc
blobcfdb5cf8de6529416ea75b177e6316daa3dc6fee
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/guest_view/extension_options/extension_options_constants.h"
21 #include "extensions/browser/guest_view/extension_options/extension_options_guest_delegate.h"
22 #include "extensions/common/api/extension_options_internal.h"
23 #include "extensions/common/constants.h"
24 #include "extensions/common/extension.h"
25 #include "extensions/common/extension_messages.h"
26 #include "extensions/common/manifest_handlers/options_page_info.h"
27 #include "extensions/common/permissions/permissions_data.h"
28 #include "extensions/strings/grit/extensions_strings.h"
29 #include "ipc/ipc_message_macros.h"
31 using content::WebContents;
32 using guest_view::GuestViewBase;
33 using guest_view::GuestViewEvent;
34 using namespace extensions::api;
36 namespace extensions {
38 // static
39 const char ExtensionOptionsGuest::Type[] = "extensionoptions";
41 ExtensionOptionsGuest::ExtensionOptionsGuest(WebContents* owner_web_contents)
42 : GuestView<ExtensionOptionsGuest>(owner_web_contents),
43 extension_options_guest_delegate_(
44 extensions::ExtensionsAPIClient::Get()
45 ->CreateExtensionOptionsGuestDelegate(this)) {}
47 ExtensionOptionsGuest::~ExtensionOptionsGuest() {
50 // static
51 GuestViewBase* ExtensionOptionsGuest::Create(WebContents* owner_web_contents) {
52 return new ExtensionOptionsGuest(owner_web_contents);
55 bool ExtensionOptionsGuest::CanRunInDetachedState() const {
56 return true;
59 void ExtensionOptionsGuest::CreateWebContents(
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)) {
67 callback.Run(nullptr);
68 return;
71 std::string embedder_extension_id = GetOwnerSiteURL().host();
72 if (crx_file::id_util::IdIsValid(embedder_extension_id) &&
73 extension_id != embedder_extension_id) {
74 // Extensions cannot embed other extensions' options pages.
75 callback.Run(nullptr);
76 return;
79 GURL extension_url =
80 extensions::Extension::GetBaseURLFromExtensionId(extension_id);
81 if (!extension_url.is_valid()) {
82 callback.Run(nullptr);
83 return;
86 // Get the options page URL for later use.
87 extensions::ExtensionRegistry* registry =
88 extensions::ExtensionRegistry::Get(browser_context());
89 const extensions::Extension* extension =
90 registry->enabled_extensions().GetByID(extension_id);
91 if (!extension) {
92 // The ID was valid but the extension didn't exist. Typically this will
93 // happen when an extension is disabled.
94 callback.Run(nullptr);
95 return;
98 options_page_ = extensions::OptionsPageInfo::GetOptionsPage(extension);
99 if (!options_page_.is_valid()) {
100 callback.Run(nullptr);
101 return;
104 // Create a WebContents using the extension URL. The options page's
105 // WebContents should live in the same process as its parent extension's
106 // WebContents, so we can use |extension_url| for creating the SiteInstance.
107 content::SiteInstance* options_site_instance =
108 content::SiteInstance::CreateForURL(browser_context(), extension_url);
109 WebContents::CreateParams params(browser_context(), options_site_instance);
110 params.guest_delegate = this;
111 callback.Run(WebContents::Create(params));
114 void ExtensionOptionsGuest::DidInitialize(
115 const base::DictionaryValue& create_params) {
116 ExtensionsAPIClient::Get()->AttachWebContentsHelpers(web_contents());
117 web_contents()->GetController().LoadURL(options_page_,
118 content::Referrer(),
119 ui::PAGE_TRANSITION_LINK,
120 std::string());
123 void ExtensionOptionsGuest::GuestViewDidStopLoading() {
124 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
125 DispatchEventToView(new GuestViewEvent(
126 extension_options_internal::OnLoad::kEventName, args.Pass()));
129 const char* ExtensionOptionsGuest::GetAPINamespace() const {
130 return extensionoptions::kAPINamespace;
133 int ExtensionOptionsGuest::GetTaskPrefix() const {
134 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONOPTIONS_TAG_PREFIX;
137 bool ExtensionOptionsGuest::IsPreferredSizeModeEnabled() const {
138 return true;
141 void ExtensionOptionsGuest::OnPreferredSizeChanged(const gfx::Size& pref_size) {
142 extension_options_internal::PreferredSizeChangedOptions options;
143 // Convert the size from physical pixels to logical pixels.
144 options.width = PhysicalPixelsToLogicalPixels(pref_size.width());
145 options.height = PhysicalPixelsToLogicalPixels(pref_size.height());
146 DispatchEventToView(new GuestViewEvent(
147 extension_options_internal::OnPreferredSizeChanged::kEventName,
148 options.ToValue()));
151 bool ExtensionOptionsGuest::ShouldHandleFindRequestsForEmbedder() const {
152 return true;
155 WebContents* ExtensionOptionsGuest::OpenURLFromTab(
156 WebContents* source,
157 const content::OpenURLParams& params) {
158 if (!extension_options_guest_delegate_)
159 return nullptr;
161 // Don't allow external URLs with the CURRENT_TAB disposition be opened in
162 // this guest view, change the disposition to NEW_FOREGROUND_TAB.
163 if ((!params.url.SchemeIs(extensions::kExtensionScheme) ||
164 params.url.host() != options_page_.host()) &&
165 params.disposition == CURRENT_TAB) {
166 return extension_options_guest_delegate_->OpenURLInNewTab(
167 content::OpenURLParams(params.url,
168 params.referrer,
169 params.frame_tree_node_id,
170 NEW_FOREGROUND_TAB,
171 params.transition,
172 params.is_renderer_initiated));
174 return extension_options_guest_delegate_->OpenURLInNewTab(params);
177 void ExtensionOptionsGuest::CloseContents(WebContents* source) {
178 DispatchEventToView(
179 new GuestViewEvent(extension_options_internal::OnClose::kEventName,
180 make_scoped_ptr(new base::DictionaryValue())));
183 bool ExtensionOptionsGuest::HandleContextMenu(
184 const content::ContextMenuParams& params) {
185 if (!extension_options_guest_delegate_)
186 return false;
188 return extension_options_guest_delegate_->HandleContextMenu(params);
191 bool ExtensionOptionsGuest::ShouldCreateWebContents(
192 WebContents* web_contents,
193 int route_id,
194 int main_frame_route_id,
195 WindowContainerType window_container_type,
196 const std::string& frame_name,
197 const GURL& target_url,
198 const std::string& partition_id,
199 content::SessionStorageNamespace* session_storage_namespace) {
200 // This method handles opening links from within the guest. Since this guest
201 // view is used for displaying embedded extension options, we want any
202 // external links to be opened in a new tab, not in a new guest view.
203 // Therefore we just open the URL in a new tab, and since we aren't handling
204 // the new web contents, we return false.
205 // TODO(ericzeng): Open the tab in the background if the click was a
206 // ctrl-click or middle mouse button click
207 if (extension_options_guest_delegate_) {
208 extension_options_guest_delegate_->OpenURLInNewTab(
209 content::OpenURLParams(target_url,
210 content::Referrer(),
211 NEW_FOREGROUND_TAB,
212 ui::PAGE_TRANSITION_LINK,
213 false));
215 return false;
218 void ExtensionOptionsGuest::DidNavigateMainFrame(
219 const content::LoadCommittedDetails& details,
220 const content::FrameNavigateParams& params) {
221 if (attached()) {
222 auto guest_zoom_controller =
223 ui_zoom::ZoomController::FromWebContents(web_contents());
224 guest_zoom_controller->SetZoomMode(
225 ui_zoom::ZoomController::ZOOM_MODE_ISOLATED);
226 SetGuestZoomLevelToMatchEmbedder();
228 if (params.url.GetOrigin() != options_page_.GetOrigin()) {
229 bad_message::ReceivedBadMessage(web_contents()->GetRenderProcessHost(),
230 bad_message::EOG_BAD_ORIGIN);
235 } // namespace extensions