Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / extensions / browser / guest_view / extension_options / extension_options_guest.cc
blob28014e2a15283ea4c04415f84de2fd85024eb566
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 content::WebContents* ExtensionOptionsGuest::OpenURLFromTab(
155 content::WebContents* source,
156 const content::OpenURLParams& params) {
157 if (!extension_options_guest_delegate_)
158 return nullptr;
160 // Don't allow external URLs with the CURRENT_TAB disposition be opened in
161 // this guest view, change the disposition to NEW_FOREGROUND_TAB.
162 if ((!params.url.SchemeIs(extensions::kExtensionScheme) ||
163 params.url.host() != options_page_.host()) &&
164 params.disposition == CURRENT_TAB) {
165 return extension_options_guest_delegate_->OpenURLInNewTab(
166 content::OpenURLParams(params.url,
167 params.referrer,
168 params.frame_tree_node_id,
169 NEW_FOREGROUND_TAB,
170 params.transition,
171 params.is_renderer_initiated));
173 return extension_options_guest_delegate_->OpenURLInNewTab(params);
176 void ExtensionOptionsGuest::CloseContents(content::WebContents* source) {
177 DispatchEventToView(
178 new GuestViewEvent(extension_options_internal::OnClose::kEventName,
179 make_scoped_ptr(new base::DictionaryValue())));
182 bool ExtensionOptionsGuest::HandleContextMenu(
183 const content::ContextMenuParams& params) {
184 if (!extension_options_guest_delegate_)
185 return false;
187 return extension_options_guest_delegate_->HandleContextMenu(params);
190 bool ExtensionOptionsGuest::ShouldCreateWebContents(
191 content::WebContents* web_contents,
192 int route_id,
193 int main_frame_route_id,
194 WindowContainerType window_container_type,
195 const std::string& frame_name,
196 const GURL& target_url,
197 const std::string& partition_id,
198 content::SessionStorageNamespace* session_storage_namespace) {
199 // This method handles opening links from within the guest. Since this guest
200 // view is used for displaying embedded extension options, we want any
201 // external links to be opened in a new tab, not in a new guest view.
202 // Therefore we just open the URL in a new tab, and since we aren't handling
203 // the new web contents, we return false.
204 // TODO(ericzeng): Open the tab in the background if the click was a
205 // ctrl-click or middle mouse button click
206 if (extension_options_guest_delegate_) {
207 extension_options_guest_delegate_->OpenURLInNewTab(
208 content::OpenURLParams(target_url,
209 content::Referrer(),
210 NEW_FOREGROUND_TAB,
211 ui::PAGE_TRANSITION_LINK,
212 false));
214 return false;
217 void ExtensionOptionsGuest::DidNavigateMainFrame(
218 const content::LoadCommittedDetails& details,
219 const content::FrameNavigateParams& params) {
220 if (attached()) {
221 auto guest_zoom_controller =
222 ui_zoom::ZoomController::FromWebContents(web_contents());
223 guest_zoom_controller->SetZoomMode(
224 ui_zoom::ZoomController::ZOOM_MODE_ISOLATED);
225 SetGuestZoomLevelToMatchEmbedder();
227 if (params.url.GetOrigin() != options_page_.GetOrigin()) {
228 bad_message::ReceivedBadMessage(web_contents()->GetRenderProcessHost(),
229 bad_message::EOG_BAD_ORIGIN);
234 } // namespace extensions