Update broken references to image assets
[chromium-blink-merge.git] / extensions / browser / guest_view / extension_options / extension_options_guest.cc
blob3b40a5dac1aa375783686f7606a670f20d7c3572
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(
42 content::WebContents* owner_web_contents)
43 : GuestView<ExtensionOptionsGuest>(owner_web_contents),
44 extension_options_guest_delegate_(
45 extensions::ExtensionsAPIClient::Get()
46 ->CreateExtensionOptionsGuestDelegate(this)) {
49 ExtensionOptionsGuest::~ExtensionOptionsGuest() {
52 // static
53 GuestViewBase* ExtensionOptionsGuest::Create(
54 content::WebContents* owner_web_contents) {
55 return new ExtensionOptionsGuest(owner_web_contents);
58 bool ExtensionOptionsGuest::CanRunInDetachedState() const {
59 return true;
62 void ExtensionOptionsGuest::CreateWebContents(
63 const base::DictionaryValue& create_params,
64 const WebContentsCreatedCallback& callback) {
65 // Get the extension's base URL.
66 std::string extension_id;
67 create_params.GetString(extensionoptions::kExtensionId, &extension_id);
69 if (!crx_file::id_util::IdIsValid(extension_id)) {
70 callback.Run(nullptr);
71 return;
74 std::string embedder_extension_id = GetOwnerSiteURL().host();
75 if (crx_file::id_util::IdIsValid(embedder_extension_id) &&
76 extension_id != embedder_extension_id) {
77 // Extensions cannot embed other extensions' options pages.
78 callback.Run(nullptr);
79 return;
82 GURL extension_url =
83 extensions::Extension::GetBaseURLFromExtensionId(extension_id);
84 if (!extension_url.is_valid()) {
85 callback.Run(nullptr);
86 return;
89 // Get the options page URL for later use.
90 extensions::ExtensionRegistry* registry =
91 extensions::ExtensionRegistry::Get(browser_context());
92 const extensions::Extension* extension =
93 registry->enabled_extensions().GetByID(extension_id);
94 if (!extension) {
95 // The ID was valid but the extension didn't exist. Typically this will
96 // happen when an extension is disabled.
97 callback.Run(nullptr);
98 return;
101 options_page_ = extensions::OptionsPageInfo::GetOptionsPage(extension);
102 if (!options_page_.is_valid()) {
103 callback.Run(nullptr);
104 return;
107 // Create a WebContents using the extension URL. The options page's
108 // WebContents should live in the same process as its parent extension's
109 // WebContents, so we can use |extension_url| for creating the SiteInstance.
110 content::SiteInstance* options_site_instance =
111 content::SiteInstance::CreateForURL(browser_context(), extension_url);
112 WebContents::CreateParams params(browser_context(), options_site_instance);
113 params.guest_delegate = this;
114 callback.Run(WebContents::Create(params));
117 void ExtensionOptionsGuest::DidInitialize(
118 const base::DictionaryValue& create_params) {
119 ExtensionsAPIClient::Get()->AttachWebContentsHelpers(web_contents());
120 web_contents()->GetController().LoadURL(options_page_,
121 content::Referrer(),
122 ui::PAGE_TRANSITION_LINK,
123 std::string());
126 void ExtensionOptionsGuest::GuestViewDidStopLoading() {
127 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
128 DispatchEventToView(new GuestViewEvent(
129 extension_options_internal::OnLoad::kEventName, args.Pass()));
132 const char* ExtensionOptionsGuest::GetAPINamespace() const {
133 return extensionoptions::kAPINamespace;
136 int ExtensionOptionsGuest::GetTaskPrefix() const {
137 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONOPTIONS_TAG_PREFIX;
140 bool ExtensionOptionsGuest::IsPreferredSizeModeEnabled() const {
141 return true;
144 void ExtensionOptionsGuest::OnPreferredSizeChanged(const gfx::Size& pref_size) {
145 extension_options_internal::PreferredSizeChangedOptions options;
146 // Convert the size from physical pixels to logical pixels.
147 options.width = PhysicalPixelsToLogicalPixels(pref_size.width());
148 options.height = PhysicalPixelsToLogicalPixels(pref_size.height());
149 DispatchEventToView(new GuestViewEvent(
150 extension_options_internal::OnPreferredSizeChanged::kEventName,
151 options.ToValue()));
154 bool ExtensionOptionsGuest::ShouldHandleFindRequestsForEmbedder() const {
155 return true;
158 content::WebContents* ExtensionOptionsGuest::OpenURLFromTab(
159 content::WebContents* source,
160 const content::OpenURLParams& params) {
161 if (!extension_options_guest_delegate_)
162 return nullptr;
164 // Don't allow external URLs with the CURRENT_TAB disposition be opened in
165 // this guest view, change the disposition to NEW_FOREGROUND_TAB.
166 if ((!params.url.SchemeIs(extensions::kExtensionScheme) ||
167 params.url.host() != options_page_.host()) &&
168 params.disposition == CURRENT_TAB) {
169 return extension_options_guest_delegate_->OpenURLInNewTab(
170 content::OpenURLParams(params.url,
171 params.referrer,
172 params.frame_tree_node_id,
173 NEW_FOREGROUND_TAB,
174 params.transition,
175 params.is_renderer_initiated));
177 return extension_options_guest_delegate_->OpenURLInNewTab(params);
180 void ExtensionOptionsGuest::CloseContents(content::WebContents* source) {
181 DispatchEventToView(
182 new GuestViewEvent(extension_options_internal::OnClose::kEventName,
183 make_scoped_ptr(new base::DictionaryValue())));
186 bool ExtensionOptionsGuest::HandleContextMenu(
187 const content::ContextMenuParams& params) {
188 if (!extension_options_guest_delegate_)
189 return false;
191 return extension_options_guest_delegate_->HandleContextMenu(params);
194 bool ExtensionOptionsGuest::ShouldCreateWebContents(
195 content::WebContents* web_contents,
196 int route_id,
197 int main_frame_route_id,
198 WindowContainerType window_container_type,
199 const std::string& frame_name,
200 const GURL& target_url,
201 const std::string& partition_id,
202 content::SessionStorageNamespace* session_storage_namespace) {
203 // This method handles opening links from within the guest. Since this guest
204 // view is used for displaying embedded extension options, we want any
205 // external links to be opened in a new tab, not in a new guest view.
206 // Therefore we just open the URL in a new tab, and since we aren't handling
207 // the new web contents, we return false.
208 // TODO(ericzeng): Open the tab in the background if the click was a
209 // ctrl-click or middle mouse button click
210 if (extension_options_guest_delegate_) {
211 extension_options_guest_delegate_->OpenURLInNewTab(
212 content::OpenURLParams(target_url,
213 content::Referrer(),
214 NEW_FOREGROUND_TAB,
215 ui::PAGE_TRANSITION_LINK,
216 false));
218 return false;
221 void ExtensionOptionsGuest::DidNavigateMainFrame(
222 const content::LoadCommittedDetails& details,
223 const content::FrameNavigateParams& params) {
224 if (attached()) {
225 auto guest_zoom_controller =
226 ui_zoom::ZoomController::FromWebContents(web_contents());
227 guest_zoom_controller->SetZoomMode(
228 ui_zoom::ZoomController::ZOOM_MODE_ISOLATED);
229 SetGuestZoomLevelToMatchEmbedder();
231 if (params.url.GetOrigin() != options_page_.GetOrigin()) {
232 bad_message::ReceivedBadMessage(web_contents()->GetRenderProcessHost(),
233 bad_message::EOG_BAD_ORIGIN);
238 } // namespace extensions