Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / extensions / browser / guest_view / extension_view / extension_view_guest.cc
blob79922ee2997ea40ec2daeb76fc397373d2c6836f
1 // Copyright 2015 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_view/extension_view_guest.h"
7 #include "base/strings/string_util.h"
8 #include "components/crx_file/id_util.h"
9 #include "content/public/browser/render_process_host.h"
10 #include "content/public/common/result_codes.h"
11 #include "extensions/browser/api/extensions_api_client.h"
12 #include "extensions/browser/bad_message.h"
13 #include "extensions/browser/guest_view/extension_view/extension_view_constants.h"
14 #include "extensions/browser/guest_view/guest_view_event.h"
15 #include "extensions/common/constants.h"
16 #include "extensions/common/extension_messages.h"
17 #include "extensions/strings/grit/extensions_strings.h"
19 using content::WebContents;
20 using namespace extensions::core_api;
22 namespace extensions {
24 // static
25 const char ExtensionViewGuest::Type[] = "extensionview";
27 ExtensionViewGuest::ExtensionViewGuest(
28 content::WebContents* owner_web_contents)
29 : GuestView<ExtensionViewGuest>(owner_web_contents),
30 extension_view_guest_delegate_(
31 extensions::ExtensionsAPIClient::Get()
32 ->CreateExtensionViewGuestDelegate(this)) {
35 ExtensionViewGuest::~ExtensionViewGuest() {
38 // static
39 extensions::GuestViewBase* ExtensionViewGuest::Create(
40 content::WebContents* owner_web_contents) {
41 return new ExtensionViewGuest(owner_web_contents);
44 void ExtensionViewGuest::NavigateGuest(const std::string& src,
45 bool force_navigation) {
46 GURL url = extension_url_.Resolve(src);
48 // If the URL is not valid, about:blank, or the same origin as the extension,
49 // then navigate to about:blank.
50 bool url_not_allowed = (url != GURL(url::kAboutBlankURL)) &&
51 (url.GetOrigin() != extension_url_.GetOrigin());
52 if (!url.is_valid() || url_not_allowed) {
53 NavigateGuest(url::kAboutBlankURL, true /* force_navigation */);
54 return;
57 if (!force_navigation && (view_page_ == url))
58 return;
60 web_contents()->GetRenderProcessHost()->FilterURL(false, &url);
61 web_contents()->GetController().LoadURL(url, content::Referrer(),
62 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
63 std::string());
65 view_page_ = url;
68 // GuestViewBase implementation.
69 bool ExtensionViewGuest::CanRunInDetachedState() const {
70 return true;
73 void ExtensionViewGuest::CreateWebContents(
74 const base::DictionaryValue& create_params,
75 const WebContentsCreatedCallback& callback) {
76 // Gets the extension ID.
77 std::string extension_id;
78 create_params.GetString(extensionview::kAttributeExtension, &extension_id);
80 if (!crx_file::id_util::IdIsValid(extension_id)) {
81 callback.Run(nullptr);
82 return;
85 // Gets the extension URL.
86 extension_url_ =
87 extensions::Extension::GetBaseURLFromExtensionId(extension_id);
89 if (!extension_url_.is_valid()) {
90 callback.Run(nullptr);
91 return;
94 content::SiteInstance* view_site_instance =
95 content::SiteInstance::CreateForURL(browser_context(),
96 extension_url_);
98 WebContents::CreateParams params(browser_context(), view_site_instance);
99 params.guest_delegate = this;
100 callback.Run(WebContents::Create(params));
103 void ExtensionViewGuest::DidInitialize(
104 const base::DictionaryValue& create_params) {
105 extension_function_dispatcher_.reset(
106 new extensions::ExtensionFunctionDispatcher(browser_context(), this));
108 if (extension_view_guest_delegate_)
109 extension_view_guest_delegate_->DidInitialize();
111 ApplyAttributes(create_params);
114 void ExtensionViewGuest::DidAttachToEmbedder() {
115 ApplyAttributes(*attach_params());
118 const char* ExtensionViewGuest::GetAPINamespace() const {
119 return extensionview::kAPINamespace;
122 int ExtensionViewGuest::GetTaskPrefix() const {
123 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONVIEW_TAG_PREFIX;
126 // content::WebContentsObserver implementation.
127 void ExtensionViewGuest::DidCommitProvisionalLoadForFrame(
128 content::RenderFrameHost* render_frame_host,
129 const GURL& url,
130 ui::PageTransition transition_type) {
131 if (render_frame_host->GetParent())
132 return;
134 view_page_ = url;
136 // Gets chrome-extension://extensionid/ prefix.
137 std::string prefix = url.GetWithEmptyPath().spec();
138 std::string relative_url = url.spec();
140 // Removes the prefix.
141 ReplaceFirstSubstringAfterOffset(&relative_url, 0, prefix, "");
143 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
144 args->SetString(guestview::kUrl, relative_url);
145 DispatchEventToView(
146 new GuestViewEvent(extensionview::kEventLoadCommit, args.Pass()));
149 void ExtensionViewGuest::DidNavigateMainFrame(
150 const content::LoadCommittedDetails& details,
151 const content::FrameNavigateParams& params) {
152 if (attached() && (params.url.GetOrigin() != view_page_.GetOrigin())) {
153 bad_message::ReceivedBadMessage(web_contents()->GetRenderProcessHost(),
154 bad_message::EVG_BAD_ORIGIN);
158 bool ExtensionViewGuest::OnMessageReceived(const IPC::Message& message) {
159 bool handled = true;
160 IPC_BEGIN_MESSAGE_MAP(ExtensionViewGuest, message)
161 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
162 IPC_MESSAGE_UNHANDLED(handled = false)
163 IPC_END_MESSAGE_MAP()
164 return handled;
167 // Private
168 void ExtensionViewGuest::OnRequest(
169 const ExtensionHostMsg_Request_Params& params) {
170 extension_function_dispatcher_->Dispatch(params,
171 web_contents()->GetRenderViewHost());
174 void ExtensionViewGuest::ApplyAttributes(const base::DictionaryValue& params) {
175 std::string src;
176 params.GetString(extensionview::kAttributeSrc, &src);
177 NavigateGuest(src, false /* force_navigation */);
180 } // namespace extensions