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 "components/guest_view/browser/guest_view_event.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/common/result_codes.h"
12 #include "extensions/browser/api/extensions_api_client.h"
13 #include "extensions/browser/bad_message.h"
14 #include "extensions/browser/guest_view/extension_view/extension_view_constants.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 guest_view::GuestViewBase
;
21 using guest_view::GuestViewEvent
;
22 using namespace extensions::core_api
;
24 namespace extensions
{
27 const char ExtensionViewGuest::Type
[] = "extensionview";
29 ExtensionViewGuest::ExtensionViewGuest(
30 content::WebContents
* owner_web_contents
)
31 : GuestView
<ExtensionViewGuest
>(owner_web_contents
),
32 extension_view_guest_delegate_(
33 extensions::ExtensionsAPIClient::Get()
34 ->CreateExtensionViewGuestDelegate(this)) {
37 ExtensionViewGuest::~ExtensionViewGuest() {
41 GuestViewBase
* ExtensionViewGuest::Create(
42 content::WebContents
* owner_web_contents
) {
43 return new ExtensionViewGuest(owner_web_contents
);
46 void ExtensionViewGuest::NavigateGuest(const std::string
& src
,
47 bool force_navigation
) {
48 GURL url
= extension_url_
.Resolve(src
);
50 // If the URL is not valid, about:blank, or the same origin as the extension,
51 // then navigate to about:blank.
52 bool url_not_allowed
= (url
!= GURL(url::kAboutBlankURL
)) &&
53 (url
.GetOrigin() != extension_url_
.GetOrigin());
54 if (!url
.is_valid() || url_not_allowed
) {
55 NavigateGuest(url::kAboutBlankURL
, true /* force_navigation */);
59 if (!force_navigation
&& (view_page_
== url
))
62 web_contents()->GetRenderProcessHost()->FilterURL(false, &url
);
63 web_contents()->GetController().LoadURL(url
, content::Referrer(),
64 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
,
70 // GuestViewBase implementation.
71 bool ExtensionViewGuest::CanRunInDetachedState() const {
75 void ExtensionViewGuest::CreateWebContents(
76 const base::DictionaryValue
& create_params
,
77 const WebContentsCreatedCallback
& callback
) {
78 // Gets the extension ID.
79 std::string extension_id
;
80 create_params
.GetString(extensionview::kAttributeExtension
, &extension_id
);
82 if (!crx_file::id_util::IdIsValid(extension_id
)) {
83 callback
.Run(nullptr);
87 // Gets the extension URL.
89 extensions::Extension::GetBaseURLFromExtensionId(extension_id
);
91 if (!extension_url_
.is_valid()) {
92 callback
.Run(nullptr);
96 content::SiteInstance
* view_site_instance
=
97 content::SiteInstance::CreateForURL(browser_context(),
100 WebContents::CreateParams
params(browser_context(), view_site_instance
);
101 params
.guest_delegate
= this;
102 callback
.Run(WebContents::Create(params
));
105 void ExtensionViewGuest::DidInitialize(
106 const base::DictionaryValue
& create_params
) {
107 extension_function_dispatcher_
.reset(
108 new extensions::ExtensionFunctionDispatcher(browser_context(), this));
110 if (extension_view_guest_delegate_
)
111 extension_view_guest_delegate_
->DidInitialize();
113 ApplyAttributes(create_params
);
116 void ExtensionViewGuest::DidAttachToEmbedder() {
117 ApplyAttributes(*attach_params());
120 const char* ExtensionViewGuest::GetAPINamespace() const {
121 return extensionview::kAPINamespace
;
124 int ExtensionViewGuest::GetTaskPrefix() const {
125 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONVIEW_TAG_PREFIX
;
128 // content::WebContentsObserver implementation.
129 void ExtensionViewGuest::DidCommitProvisionalLoadForFrame(
130 content::RenderFrameHost
* render_frame_host
,
132 ui::PageTransition transition_type
) {
133 if (render_frame_host
->GetParent())
138 // Gets chrome-extension://extensionid/ prefix.
139 std::string prefix
= url
.GetWithEmptyPath().spec();
140 std::string relative_url
= url
.spec();
142 // Removes the prefix.
143 ReplaceFirstSubstringAfterOffset(&relative_url
, 0, prefix
, "");
145 scoped_ptr
<base::DictionaryValue
> args(new base::DictionaryValue());
146 args
->SetString(guest_view::kUrl
, relative_url
);
148 new GuestViewEvent(extensionview::kEventLoadCommit
, args
.Pass()));
151 void ExtensionViewGuest::DidNavigateMainFrame(
152 const content::LoadCommittedDetails
& details
,
153 const content::FrameNavigateParams
& params
) {
154 if (attached() && (params
.url
.GetOrigin() != view_page_
.GetOrigin())) {
155 bad_message::ReceivedBadMessage(web_contents()->GetRenderProcessHost(),
156 bad_message::EVG_BAD_ORIGIN
);
160 bool ExtensionViewGuest::OnMessageReceived(const IPC::Message
& message
) {
162 IPC_BEGIN_MESSAGE_MAP(ExtensionViewGuest
, message
)
163 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request
, OnRequest
)
164 IPC_MESSAGE_UNHANDLED(handled
= false)
165 IPC_END_MESSAGE_MAP()
170 void ExtensionViewGuest::OnRequest(
171 const ExtensionHostMsg_Request_Params
& params
) {
172 extension_function_dispatcher_
->Dispatch(params
,
173 web_contents()->GetRenderViewHost());
176 void ExtensionViewGuest::ApplyAttributes(const base::DictionaryValue
& params
) {
178 params
.GetString(extensionview::kAttributeSrc
, &src
);
179 NavigateGuest(src
, false /* force_navigation */);
182 } // namespace extensions