Durable Storage: Refactor browser test and test the basic "deny" flow.
[chromium-blink-merge.git] / extensions / browser / guest_view / extension_view / extension_view_guest.cc
blob101f3534abfffe790acc7282336cb409bdb535af
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 {
25 // static
26 const char ExtensionViewGuest::Type[] = "extensionview";
28 ExtensionViewGuest::ExtensionViewGuest(
29 content::WebContents* owner_web_contents)
30 : GuestView<ExtensionViewGuest>(owner_web_contents) {
33 ExtensionViewGuest::~ExtensionViewGuest() {
36 // static
37 GuestViewBase* ExtensionViewGuest::Create(
38 content::WebContents* owner_web_contents) {
39 return new ExtensionViewGuest(owner_web_contents);
42 bool ExtensionViewGuest::NavigateGuest(const std::string& src,
43 bool force_navigation) {
44 GURL url = extension_url_.Resolve(src);
46 // If the URL is not valid, about:blank, or the same origin as the extension,
47 // then navigate to about:blank.
48 bool url_not_allowed = (url != GURL(url::kAboutBlankURL)) &&
49 (url.GetOrigin() != extension_url_.GetOrigin());
50 if (!url.is_valid() || url_not_allowed)
51 return NavigateGuest(url::kAboutBlankURL, true /* force_navigation */);
53 if (!force_navigation && (url_ == url))
54 return false;
56 web_contents()->GetRenderProcessHost()->FilterURL(false, &url);
57 web_contents()->GetController().LoadURL(url, content::Referrer(),
58 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
59 std::string());
61 url_ = url;
62 return true;
65 // GuestViewBase implementation.
66 bool ExtensionViewGuest::CanRunInDetachedState() const {
67 return true;
70 void ExtensionViewGuest::CreateWebContents(
71 const base::DictionaryValue& create_params,
72 const WebContentsCreatedCallback& callback) {
73 // Gets the extension ID.
74 std::string extension_id;
75 create_params.GetString(extensionview::kAttributeExtension, &extension_id);
77 if (!crx_file::id_util::IdIsValid(extension_id)) {
78 callback.Run(nullptr);
79 return;
82 // Gets the extension URL.
83 extension_url_ =
84 extensions::Extension::GetBaseURLFromExtensionId(extension_id);
86 if (!extension_url_.is_valid()) {
87 callback.Run(nullptr);
88 return;
91 content::SiteInstance* view_site_instance =
92 content::SiteInstance::CreateForURL(browser_context(),
93 extension_url_);
95 WebContents::CreateParams params(browser_context(), view_site_instance);
96 params.guest_delegate = this;
97 callback.Run(WebContents::Create(params));
100 void ExtensionViewGuest::DidInitialize(
101 const base::DictionaryValue& create_params) {
102 ExtensionsAPIClient::Get()->AttachWebContentsHelpers(web_contents());
104 ApplyAttributes(create_params);
107 void ExtensionViewGuest::DidAttachToEmbedder() {
108 ApplyAttributes(*attach_params());
111 const char* ExtensionViewGuest::GetAPINamespace() const {
112 return extensionview::kAPINamespace;
115 int ExtensionViewGuest::GetTaskPrefix() const {
116 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONVIEW_TAG_PREFIX;
119 // content::WebContentsObserver implementation.
120 void ExtensionViewGuest::DidCommitProvisionalLoadForFrame(
121 content::RenderFrameHost* render_frame_host,
122 const GURL& url,
123 ui::PageTransition transition_type) {
124 if (render_frame_host->GetParent())
125 return;
127 url_ = url;
129 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
130 args->SetString(guest_view::kUrl, url_.spec());
131 DispatchEventToView(
132 new GuestViewEvent(extensionview::kEventLoadCommit, args.Pass()));
135 void ExtensionViewGuest::DidNavigateMainFrame(
136 const content::LoadCommittedDetails& details,
137 const content::FrameNavigateParams& params) {
138 if (attached() && (params.url.GetOrigin() != url_.GetOrigin())) {
139 bad_message::ReceivedBadMessage(web_contents()->GetRenderProcessHost(),
140 bad_message::EVG_BAD_ORIGIN);
144 void ExtensionViewGuest::ApplyAttributes(const base::DictionaryValue& params) {
145 std::string src;
146 params.GetString(extensionview::kAttributeSrc, &src);
147 NavigateGuest(src, false /* force_navigation */);
150 } // namespace extensions