ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / extensions / browser / guest_view / extension_view / extension_view_guest.cc
blobe9c309ad8d2fb02a46ab1077233ca3d96f4a7073
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/metrics/user_metrics.h"
8 #include "base/strings/string_util.h"
9 #include "components/crx_file/id_util.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/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 namespace extensions::core_api;
21 namespace extensions {
23 // static
24 const char ExtensionViewGuest::Type[] = "extensionview";
26 ExtensionViewGuest::ExtensionViewGuest(
27 content::WebContents* owner_web_contents)
28 : GuestView<ExtensionViewGuest>(owner_web_contents),
29 extension_view_guest_delegate_(
30 extensions::ExtensionsAPIClient::Get()
31 ->CreateExtensionViewGuestDelegate(this)) {
34 ExtensionViewGuest::~ExtensionViewGuest() {
37 // static
38 extensions::GuestViewBase* ExtensionViewGuest::Create(
39 content::WebContents* owner_web_contents) {
40 return new ExtensionViewGuest(owner_web_contents);
43 void ExtensionViewGuest::NavigateGuest(const std::string& src,
44 bool force_navigation) {
45 GURL url = extension_url_.Resolve(src);
47 // If the URL is not valid, about:blank, or the same origin as the extension,
48 // then navigate to about:blank.
49 bool url_not_allowed = (url != GURL(url::kAboutBlankURL)) &&
50 (url.GetOrigin() != extension_url_.GetOrigin());
51 if (!url.is_valid() || url_not_allowed) {
52 NavigateGuest(url::kAboutBlankURL, true /* force_navigation */);
53 return;
56 if (!force_navigation && (view_page_ == url))
57 return;
59 web_contents()->GetRenderProcessHost()->FilterURL(false, &url);
60 web_contents()->GetController().LoadURL(url, content::Referrer(),
61 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
62 std::string());
64 view_page_ = url;
67 // GuestViewBase implementation.
68 bool ExtensionViewGuest::CanRunInDetachedState() const {
69 return true;
72 void ExtensionViewGuest::CreateWebContents(
73 const base::DictionaryValue& create_params,
74 const WebContentsCreatedCallback& callback) {
75 // Gets the extension ID.
76 std::string extension_id;
77 create_params.GetString(extensionview::kAttributeExtension, &extension_id);
79 if (!crx_file::id_util::IdIsValid(extension_id)) {
80 callback.Run(nullptr);
81 return;
84 // Gets the extension URL.
85 extension_url_ =
86 extensions::Extension::GetBaseURLFromExtensionId(extension_id);
88 if (!extension_url_.is_valid()) {
89 callback.Run(nullptr);
90 return;
93 content::SiteInstance* view_site_instance =
94 content::SiteInstance::CreateForURL(browser_context(),
95 extension_url_);
97 WebContents::CreateParams params(browser_context(), view_site_instance);
98 params.guest_delegate = this;
99 callback.Run(WebContents::Create(params));
102 void ExtensionViewGuest::DidInitialize(
103 const base::DictionaryValue& create_params) {
104 extension_function_dispatcher_.reset(
105 new extensions::ExtensionFunctionDispatcher(browser_context(), this));
107 if (extension_view_guest_delegate_)
108 extension_view_guest_delegate_->DidInitialize();
110 ApplyAttributes(create_params);
113 void ExtensionViewGuest::DidAttachToEmbedder() {
114 ApplyAttributes(*attach_params());
117 const char* ExtensionViewGuest::GetAPINamespace() const {
118 return extensionview::kAPINamespace;
121 int ExtensionViewGuest::GetTaskPrefix() const {
122 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONVIEW_TAG_PREFIX;
125 // content::WebContentsObserver implementation.
126 void ExtensionViewGuest::DidCommitProvisionalLoadForFrame(
127 content::RenderFrameHost* render_frame_host,
128 const GURL& url,
129 ui::PageTransition transition_type) {
130 if (render_frame_host->GetParent())
131 return;
133 view_page_ = url;
135 // Gets chrome-extension://extensionid/ prefix.
136 std::string prefix = url.GetWithEmptyPath().spec();
137 std::string relative_url = url.spec();
139 // Removes the prefix.
140 ReplaceFirstSubstringAfterOffset(&relative_url, 0, prefix, "");
142 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
143 args->SetString(guestview::kUrl, relative_url);
144 DispatchEventToView(
145 new GuestViewBase::Event(extensionview::kEventLoadCommit, args.Pass()));
148 void ExtensionViewGuest::DidNavigateMainFrame(
149 const content::LoadCommittedDetails& details,
150 const content::FrameNavigateParams& params) {
151 if (attached() && (params.url.GetOrigin() != view_page_.GetOrigin())) {
152 base::RecordAction(base::UserMetricsAction("BadMessageTerminate_EVG"));
153 web_contents()->GetRenderProcessHost()->Shutdown(
154 content::RESULT_CODE_KILLED_BAD_MESSAGE, false /* wait */);
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