| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/guest_view/extension_view/extension_view_guest.h" | 5 #include "extensions/browser/guest_view/extension_view/extension_view_guest.h" |
| 6 | 6 |
| 7 #include "base/metrics/user_metrics.h" | 7 #include "base/metrics/user_metrics.h" |
| 8 #include "components/crx_file/id_util.h" |
| 8 #include "content/public/browser/render_process_host.h" | 9 #include "content/public/browser/render_process_host.h" |
| 9 #include "content/public/common/result_codes.h" | 10 #include "content/public/common/result_codes.h" |
| 10 #include "extensions/browser/api/extensions_api_client.h" | 11 #include "extensions/browser/api/extensions_api_client.h" |
| 11 #include "extensions/browser/guest_view/extension_view/extension_view_constants.
h" | 12 #include "extensions/browser/guest_view/extension_view/extension_view_constants.
h" |
| 12 #include "extensions/common/constants.h" | 13 #include "extensions/common/constants.h" |
| 13 #include "extensions/common/extension_messages.h" | 14 #include "extensions/common/extension_messages.h" |
| 14 #include "extensions/strings/grit/extensions_strings.h" | 15 #include "extensions/strings/grit/extensions_strings.h" |
| 15 | 16 |
| 16 using content::WebContents; | 17 using content::WebContents; |
| 17 using namespace extensions::core_api; | 18 using namespace extensions::core_api; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 33 } | 34 } |
| 34 | 35 |
| 35 // static | 36 // static |
| 36 extensions::GuestViewBase* ExtensionViewGuest::Create( | 37 extensions::GuestViewBase* ExtensionViewGuest::Create( |
| 37 content::WebContents* owner_web_contents) { | 38 content::WebContents* owner_web_contents) { |
| 38 return new ExtensionViewGuest(owner_web_contents); | 39 return new ExtensionViewGuest(owner_web_contents); |
| 39 } | 40 } |
| 40 | 41 |
| 41 void ExtensionViewGuest::NavigateGuest(const std::string& src, | 42 void ExtensionViewGuest::NavigateGuest(const std::string& src, |
| 42 bool force_navigation) { | 43 bool force_navigation) { |
| 43 if (src.empty()) | 44 GURL url = extension_url_.Resolve(src); |
| 45 |
| 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 NavigateGuest(url::kAboutBlankURL, true /* force_navigation */); |
| 44 return; | 52 return; |
| 53 } |
| 45 | 54 |
| 46 GURL url(src); | 55 if (!force_navigation && (view_page_ == url)) |
| 47 if (!url.is_valid() && !force_navigation && (url == view_page_)) | |
| 48 return; | 56 return; |
| 49 | 57 |
| 50 web_contents()->GetRenderProcessHost()->FilterURL(false, &url); | 58 web_contents()->GetRenderProcessHost()->FilterURL(false, &url); |
| 51 web_contents()->GetController().LoadURL(url, content::Referrer(), | 59 web_contents()->GetController().LoadURL(url, content::Referrer(), |
| 52 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | 60 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 53 std::string()); | 61 std::string()); |
| 54 | 62 |
| 55 view_page_ = url; | 63 view_page_ = url; |
| 56 } | 64 } |
| 57 | 65 |
| 58 // GuestViewBase implementation. | 66 // GuestViewBase implementation. |
| 59 bool ExtensionViewGuest::CanRunInDetachedState() const { | 67 bool ExtensionViewGuest::CanRunInDetachedState() const { |
| 60 return true; | 68 return true; |
| 61 } | 69 } |
| 62 | 70 |
| 63 void ExtensionViewGuest::CreateWebContents( | 71 void ExtensionViewGuest::CreateWebContents( |
| 64 const base::DictionaryValue& create_params, | 72 const base::DictionaryValue& create_params, |
| 65 const WebContentsCreatedCallback& callback) { | 73 const WebContentsCreatedCallback& callback) { |
| 66 std::string str; | 74 // Gets the extension ID. |
| 67 if (!create_params.GetString(extensionview::kAttributeSrc, &str)) { | 75 std::string extension_id; |
| 76 create_params.GetString(extensionview::kAttributeExtension, &extension_id); |
| 77 |
| 78 if (!crx_file::id_util::IdIsValid(extension_id)) { |
| 68 callback.Run(nullptr); | 79 callback.Run(nullptr); |
| 69 return; | 80 return; |
| 70 } | 81 } |
| 71 | 82 |
| 72 GURL source(str); | 83 // Gets the extension URL. |
| 73 if (!source.is_valid()) { | 84 extension_url_ = |
| 85 extensions::Extension::GetBaseURLFromExtensionId(extension_id); |
| 86 |
| 87 if (!extension_url_.is_valid()) { |
| 74 callback.Run(nullptr); | 88 callback.Run(nullptr); |
| 75 return; | 89 return; |
| 76 } | 90 } |
| 77 | 91 |
| 78 content::SiteInstance* view_site_instance = | 92 content::SiteInstance* view_site_instance = |
| 79 content::SiteInstance::CreateForURL(browser_context(), source); | 93 content::SiteInstance::CreateForURL(browser_context(), |
| 94 extension_url_); |
| 80 | 95 |
| 81 WebContents::CreateParams params(browser_context(), view_site_instance); | 96 WebContents::CreateParams params(browser_context(), view_site_instance); |
| 82 params.guest_delegate = this; | 97 params.guest_delegate = this; |
| 83 callback.Run(WebContents::Create(params)); | 98 callback.Run(WebContents::Create(params)); |
| 84 } | 99 } |
| 85 | 100 |
| 86 void ExtensionViewGuest::DidInitialize( | 101 void ExtensionViewGuest::DidInitialize( |
| 87 const base::DictionaryValue& create_params) { | 102 const base::DictionaryValue& create_params) { |
| 88 extension_function_dispatcher_.reset( | 103 extension_function_dispatcher_.reset( |
| 89 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); | 104 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 100 | 115 |
| 101 const char* ExtensionViewGuest::GetAPINamespace() const { | 116 const char* ExtensionViewGuest::GetAPINamespace() const { |
| 102 return extensionview::kAPINamespace; | 117 return extensionview::kAPINamespace; |
| 103 } | 118 } |
| 104 | 119 |
| 105 int ExtensionViewGuest::GetTaskPrefix() const { | 120 int ExtensionViewGuest::GetTaskPrefix() const { |
| 106 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONVIEW_TAG_PREFIX; | 121 return IDS_EXTENSION_TASK_MANAGER_EXTENSIONVIEW_TAG_PREFIX; |
| 107 } | 122 } |
| 108 | 123 |
| 109 // content::WebContentsObserver implementation. | 124 // content::WebContentsObserver implementation. |
| 125 void ExtensionViewGuest::DidCommitProvisionalLoadForFrame( |
| 126 content::RenderFrameHost* render_frame_host, |
| 127 const GURL& url, |
| 128 ui::PageTransition transition_type) { |
| 129 if (!render_frame_host->GetParent()) |
| 130 view_page_ = url; |
| 131 |
| 132 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
| 133 args->SetString(guestview::kUrl, url.spec()); |
| 134 DispatchEventToView( |
| 135 new GuestViewBase::Event(extensionview::kEventLoadCommit, args.Pass())); |
| 136 } |
| 137 |
| 138 void ExtensionViewGuest::DidNavigateMainFrame( |
| 139 const content::LoadCommittedDetails& details, |
| 140 const content::FrameNavigateParams& params) { |
| 141 if (attached() && (params.url.GetOrigin() != view_page_.GetOrigin())) { |
| 142 base::RecordAction(base::UserMetricsAction("BadMessageTerminate_EVG")); |
| 143 web_contents()->GetRenderProcessHost()->Shutdown( |
| 144 content::RESULT_CODE_KILLED_BAD_MESSAGE, false /* wait */); |
| 145 } |
| 146 } |
| 147 |
| 110 bool ExtensionViewGuest::OnMessageReceived(const IPC::Message& message) { | 148 bool ExtensionViewGuest::OnMessageReceived(const IPC::Message& message) { |
| 111 bool handled = true; | 149 bool handled = true; |
| 112 IPC_BEGIN_MESSAGE_MAP(ExtensionViewGuest, message) | 150 IPC_BEGIN_MESSAGE_MAP(ExtensionViewGuest, message) |
| 113 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) | 151 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
| 114 IPC_MESSAGE_UNHANDLED(handled = false) | 152 IPC_MESSAGE_UNHANDLED(handled = false) |
| 115 IPC_END_MESSAGE_MAP() | 153 IPC_END_MESSAGE_MAP() |
| 116 return handled; | 154 return handled; |
| 117 } | 155 } |
| 118 | 156 |
| 119 // Private | 157 // Private |
| 120 void ExtensionViewGuest::OnRequest( | 158 void ExtensionViewGuest::OnRequest( |
| 121 const ExtensionHostMsg_Request_Params& params) { | 159 const ExtensionHostMsg_Request_Params& params) { |
| 122 extension_function_dispatcher_->Dispatch(params, | 160 extension_function_dispatcher_->Dispatch(params, |
| 123 web_contents()->GetRenderViewHost()); | 161 web_contents()->GetRenderViewHost()); |
| 124 } | 162 } |
| 125 | 163 |
| 126 void ExtensionViewGuest::ApplyAttributes(const base::DictionaryValue& params) { | 164 void ExtensionViewGuest::ApplyAttributes(const base::DictionaryValue& params) { |
| 127 std::string src; | 165 std::string src; |
| 128 params.GetString(extensionview::kAttributeSrc, &src); | 166 params.GetString(extensionview::kAttributeSrc, &src); |
| 129 NavigateGuest(src, false /* force_navigation */); | 167 NavigateGuest(src, false /* force_navigation */); |
| 130 } | 168 } |
| 131 | 169 |
| 132 } // namespace extensions | 170 } // namespace extensions |
| OLD | NEW |