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 "components/crx_file/id_util.h"
8 #include "components/guest_view/browser/guest_view_event.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/common/constants.h"
15 #include "extensions/common/extension_messages.h"
16 #include "extensions/strings/grit/extensions_strings.h"
18 using content::WebContents
;
19 using guest_view::GuestViewBase
;
20 using guest_view::GuestViewEvent
;
21 using namespace extensions::api
;
23 namespace extensions
{
26 const char ExtensionViewGuest::Type
[] = "extensionview";
28 ExtensionViewGuest::ExtensionViewGuest(WebContents
* owner_web_contents
)
29 : GuestView
<ExtensionViewGuest
>(owner_web_contents
) {}
31 ExtensionViewGuest::~ExtensionViewGuest() {
35 GuestViewBase
* ExtensionViewGuest::Create(WebContents
* owner_web_contents
) {
36 return new ExtensionViewGuest(owner_web_contents
);
39 bool ExtensionViewGuest::NavigateGuest(const std::string
& src
,
40 bool force_navigation
) {
41 GURL url
= extension_url_
.Resolve(src
);
43 // If the URL is not valid, about:blank, or the same origin as the extension,
44 // then navigate to about:blank.
45 bool url_not_allowed
= (url
!= GURL(url::kAboutBlankURL
)) &&
46 (url
.GetOrigin() != extension_url_
.GetOrigin());
47 if (!url
.is_valid() || url_not_allowed
)
48 return NavigateGuest(url::kAboutBlankURL
, true /* force_navigation */);
50 if (!force_navigation
&& (url_
== url
))
53 web_contents()->GetRenderProcessHost()->FilterURL(false, &url
);
54 web_contents()->GetController().LoadURL(url
, content::Referrer(),
55 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
,
62 // GuestViewBase implementation.
63 bool ExtensionViewGuest::CanRunInDetachedState() const {
67 void ExtensionViewGuest::CreateWebContents(
68 const base::DictionaryValue
& create_params
,
69 const WebContentsCreatedCallback
& callback
) {
70 // Gets the extension ID.
71 std::string extension_id
;
72 create_params
.GetString(extensionview::kAttributeExtension
, &extension_id
);
74 if (!crx_file::id_util::IdIsValid(extension_id
)) {
75 callback
.Run(nullptr);
79 // Gets the extension URL.
81 extensions::Extension::GetBaseURLFromExtensionId(extension_id
);
83 if (!extension_url_
.is_valid()) {
84 callback
.Run(nullptr);
88 content::SiteInstance
* view_site_instance
=
89 content::SiteInstance::CreateForURL(browser_context(),
92 WebContents::CreateParams
params(browser_context(), view_site_instance
);
93 params
.guest_delegate
= this;
94 callback
.Run(WebContents::Create(params
));
97 void ExtensionViewGuest::DidInitialize(
98 const base::DictionaryValue
& create_params
) {
99 ExtensionsAPIClient::Get()->AttachWebContentsHelpers(web_contents());
101 ApplyAttributes(create_params
);
104 void ExtensionViewGuest::DidAttachToEmbedder() {
105 ApplyAttributes(*attach_params());
108 const char* ExtensionViewGuest::GetAPINamespace() const {
109 return extensionview::kAPINamespace
;
112 int ExtensionViewGuest::GetTaskPrefix() const {
113 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONVIEW_TAG_PREFIX
;
116 void ExtensionViewGuest::DidCommitProvisionalLoadForFrame(
117 content::RenderFrameHost
* render_frame_host
,
119 ui::PageTransition transition_type
) {
120 if (render_frame_host
->GetParent())
125 scoped_ptr
<base::DictionaryValue
> args(new base::DictionaryValue());
126 args
->SetString(guest_view::kUrl
, url_
.spec());
128 new GuestViewEvent(extensionview::kEventLoadCommit
, args
.Pass()));
131 void ExtensionViewGuest::DidNavigateMainFrame(
132 const content::LoadCommittedDetails
& details
,
133 const content::FrameNavigateParams
& params
) {
134 if (attached() && (params
.url
.GetOrigin() != url_
.GetOrigin())) {
135 bad_message::ReceivedBadMessage(web_contents()->GetRenderProcessHost(),
136 bad_message::EVG_BAD_ORIGIN
);
140 void ExtensionViewGuest::ApplyAttributes(const base::DictionaryValue
& params
) {
142 params
.GetString(extensionview::kAttributeSrc
, &src
);
143 NavigateGuest(src
, false /* force_navigation */);
146 } // namespace extensions