Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / extensions / browser / guest_view / extension_options / extension_options_guest.cc
blob9a60a515e3176891f31342cff2221624212c0e70
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/navigation_details.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/browser/site_instance.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/result_codes.h"
14 #include "extensions/browser/api/extensions_api_client.h"
15 #include "extensions/browser/bad_message.h"
16 #include "extensions/browser/extension_function_dispatcher.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/extension_web_contents_observer.h"
19 #include "extensions/browser/guest_view/extension_options/extension_options_constants.h"
20 #include "extensions/browser/guest_view/extension_options/extension_options_guest_delegate.h"
21 #include "extensions/browser/guest_view/guest_view_event.h"
22 #include "extensions/browser/guest_view/guest_view_manager.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 namespace extensions::core_api;
35 namespace extensions {
37 // static
38 const char ExtensionOptionsGuest::Type[] = "extensionoptions";
40 ExtensionOptionsGuest::ExtensionOptionsGuest(
41 content::WebContents* owner_web_contents)
42 : GuestView<ExtensionOptionsGuest>(owner_web_contents),
43 extension_options_guest_delegate_(
44 extensions::ExtensionsAPIClient::Get()
45 ->CreateExtensionOptionsGuestDelegate(this)) {
48 ExtensionOptionsGuest::~ExtensionOptionsGuest() {
51 // static
52 extensions::GuestViewBase* ExtensionOptionsGuest::Create(
53 content::WebContents* owner_web_contents) {
54 return new ExtensionOptionsGuest(owner_web_contents);
57 bool ExtensionOptionsGuest::CanRunInDetachedState() const {
58 return true;
61 void ExtensionOptionsGuest::CreateWebContents(
62 const base::DictionaryValue& create_params,
63 const WebContentsCreatedCallback& callback) {
64 // Get the extension's base URL.
65 std::string extension_id;
66 create_params.GetString(extensionoptions::kExtensionId, &extension_id);
68 if (!crx_file::id_util::IdIsValid(extension_id)) {
69 callback.Run(nullptr);
70 return;
73 std::string embedder_extension_id = GetOwnerSiteURL().host();
74 if (crx_file::id_util::IdIsValid(embedder_extension_id) &&
75 extension_id != embedder_extension_id) {
76 // Extensions cannot embed other extensions' options pages.
77 callback.Run(nullptr);
78 return;
81 GURL extension_url =
82 extensions::Extension::GetBaseURLFromExtensionId(extension_id);
83 if (!extension_url.is_valid()) {
84 callback.Run(nullptr);
85 return;
88 // Get the options page URL for later use.
89 extensions::ExtensionRegistry* registry =
90 extensions::ExtensionRegistry::Get(browser_context());
91 const extensions::Extension* extension =
92 registry->enabled_extensions().GetByID(extension_id);
93 if (!extension) {
94 // The ID was valid but the extension didn't exist. Typically this will
95 // happen when an extension is disabled.
96 callback.Run(nullptr);
97 return;
100 options_page_ = extensions::OptionsPageInfo::GetOptionsPage(extension);
101 if (!options_page_.is_valid()) {
102 callback.Run(nullptr);
103 return;
106 // Create a WebContents using the extension URL. The options page's
107 // WebContents should live in the same process as its parent extension's
108 // WebContents, so we can use |extension_url| for creating the SiteInstance.
109 content::SiteInstance* options_site_instance =
110 content::SiteInstance::CreateForURL(browser_context(), extension_url);
111 WebContents::CreateParams params(browser_context(), options_site_instance);
112 params.guest_delegate = this;
113 callback.Run(WebContents::Create(params));
116 void ExtensionOptionsGuest::DidInitialize(
117 const base::DictionaryValue& create_params) {
118 extension_function_dispatcher_.reset(
119 new extensions::ExtensionFunctionDispatcher(browser_context(), this));
120 if (extension_options_guest_delegate_) {
121 extension_options_guest_delegate_->DidInitialize();
123 web_contents()->GetController().LoadURL(options_page_,
124 content::Referrer(),
125 ui::PAGE_TRANSITION_LINK,
126 std::string());
129 void ExtensionOptionsGuest::GuestViewDidStopLoading() {
130 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
131 DispatchEventToView(new GuestViewEvent(
132 extension_options_internal::OnLoad::kEventName, args.Pass()));
135 const char* ExtensionOptionsGuest::GetAPINamespace() const {
136 return extensionoptions::kAPINamespace;
139 int ExtensionOptionsGuest::GetTaskPrefix() const {
140 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONOPTIONS_TAG_PREFIX;
143 bool ExtensionOptionsGuest::IsPreferredSizeModeEnabled() const {
144 return true;
147 bool ExtensionOptionsGuest::IsDragAndDropEnabled() const {
148 return true;
151 void ExtensionOptionsGuest::OnPreferredSizeChanged(const gfx::Size& pref_size) {
152 extension_options_internal::PreferredSizeChangedOptions options;
153 // Convert the size from physical pixels to logical pixels.
154 options.width = PhysicalPixelsToLogicalPixels(pref_size.width());
155 options.height = PhysicalPixelsToLogicalPixels(pref_size.height());
156 DispatchEventToView(new GuestViewEvent(
157 extension_options_internal::OnPreferredSizeChanged::kEventName,
158 options.ToValue()));
161 content::WebContents* ExtensionOptionsGuest::GetAssociatedWebContents() const {
162 return web_contents();
165 content::WebContents* ExtensionOptionsGuest::OpenURLFromTab(
166 content::WebContents* source,
167 const content::OpenURLParams& params) {
168 if (!extension_options_guest_delegate_)
169 return nullptr;
171 // Don't allow external URLs with the CURRENT_TAB disposition be opened in
172 // this guest view, change the disposition to NEW_FOREGROUND_TAB.
173 if ((!params.url.SchemeIs(extensions::kExtensionScheme) ||
174 params.url.host() != options_page_.host()) &&
175 params.disposition == CURRENT_TAB) {
176 return extension_options_guest_delegate_->OpenURLInNewTab(
177 content::OpenURLParams(params.url,
178 params.referrer,
179 params.frame_tree_node_id,
180 NEW_FOREGROUND_TAB,
181 params.transition,
182 params.is_renderer_initiated));
184 return extension_options_guest_delegate_->OpenURLInNewTab(params);
187 void ExtensionOptionsGuest::CloseContents(content::WebContents* source) {
188 DispatchEventToView(
189 new GuestViewEvent(extension_options_internal::OnClose::kEventName,
190 make_scoped_ptr(new base::DictionaryValue())));
193 bool ExtensionOptionsGuest::HandleContextMenu(
194 const content::ContextMenuParams& params) {
195 if (!extension_options_guest_delegate_)
196 return false;
198 return extension_options_guest_delegate_->HandleContextMenu(params);
201 bool ExtensionOptionsGuest::ShouldCreateWebContents(
202 content::WebContents* web_contents,
203 int route_id,
204 int main_frame_route_id,
205 WindowContainerType window_container_type,
206 const base::string16& frame_name,
207 const GURL& target_url,
208 const std::string& partition_id,
209 content::SessionStorageNamespace* session_storage_namespace) {
210 // This method handles opening links from within the guest. Since this guest
211 // view is used for displaying embedded extension options, we want any
212 // external links to be opened in a new tab, not in a new guest view.
213 // Therefore we just open the URL in a new tab, and since we aren't handling
214 // the new web contents, we return false.
215 // TODO(ericzeng): Open the tab in the background if the click was a
216 // ctrl-click or middle mouse button click
217 if (extension_options_guest_delegate_) {
218 extension_options_guest_delegate_->OpenURLInNewTab(
219 content::OpenURLParams(target_url,
220 content::Referrer(),
221 NEW_FOREGROUND_TAB,
222 ui::PAGE_TRANSITION_LINK,
223 false));
225 return false;
228 void ExtensionOptionsGuest::DidNavigateMainFrame(
229 const content::LoadCommittedDetails& details,
230 const content::FrameNavigateParams& params) {
231 if (attached()) {
232 auto guest_zoom_controller =
233 ui_zoom::ZoomController::FromWebContents(web_contents());
234 guest_zoom_controller->SetZoomMode(
235 ui_zoom::ZoomController::ZOOM_MODE_ISOLATED);
236 SetGuestZoomLevelToMatchEmbedder();
238 if (params.url.GetOrigin() != options_page_.GetOrigin()) {
239 bad_message::ReceivedBadMessage(web_contents()->GetRenderProcessHost(),
240 bad_message::EOG_BAD_ORIGIN);
245 bool ExtensionOptionsGuest::OnMessageReceived(const IPC::Message& message) {
246 bool handled = true;
247 IPC_BEGIN_MESSAGE_MAP(ExtensionOptionsGuest, message)
248 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
249 IPC_MESSAGE_UNHANDLED(handled = false)
250 IPC_END_MESSAGE_MAP()
251 return handled;
254 void ExtensionOptionsGuest::OnRequest(
255 const ExtensionHostMsg_Request_Params& params) {
256 extension_function_dispatcher_->Dispatch(params,
257 web_contents()->GetRenderViewHost());
260 } // namespace extensions