Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: extensions/renderer/programmatic_script_injector.cc

Issue 1628423002: Add frameId to chrome.tabs.executeScript/insertCSS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@permissiondata-remove-process_id
Patch Set: Revert to wc->GetURL for chrome.tabs.update Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/renderer/programmatic_script_injector.h" 5 #include "extensions/renderer/programmatic_script_injector.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "content/public/common/url_constants.h" 11 #include "content/public/common/url_constants.h"
12 #include "content/public/renderer/render_frame.h" 12 #include "content/public/renderer/render_frame.h"
13 #include "extensions/common/error_utils.h" 13 #include "extensions/common/error_utils.h"
14 #include "extensions/common/extension_messages.h" 14 #include "extensions/common/extension_messages.h"
15 #include "extensions/common/manifest_constants.h" 15 #include "extensions/common/manifest_constants.h"
16 #include "extensions/common/permissions/api_permission.h"
16 #include "extensions/common/permissions/permissions_data.h" 17 #include "extensions/common/permissions/permissions_data.h"
17 #include "extensions/renderer/injection_host.h" 18 #include "extensions/renderer/injection_host.h"
19 #include "extensions/renderer/renderer_extension_registry.h"
18 #include "extensions/renderer/script_context.h" 20 #include "extensions/renderer/script_context.h"
19 #include "third_party/WebKit/public/platform/WebString.h" 21 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/web/WebDocument.h" 22 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebLocalFrame.h" 23 #include "third_party/WebKit/public/web/WebLocalFrame.h"
22 #include "third_party/WebKit/public/web/WebScriptSource.h" 24 #include "third_party/WebKit/public/web/WebScriptSource.h"
23 25
24 namespace extensions { 26 namespace extensions {
25 27
26 ProgrammaticScriptInjector::ProgrammaticScriptInjector( 28 ProgrammaticScriptInjector::ProgrammaticScriptInjector(
27 const ExtensionMsg_ExecuteCode_Params& params, 29 const ExtensionMsg_ExecuteCode_Params& params,
28 content::RenderFrame* render_frame) 30 content::RenderFrame* render_frame)
29 : params_(new ExtensionMsg_ExecuteCode_Params(params)), 31 : params_(new ExtensionMsg_ExecuteCode_Params(params)),
30 url_( 32 url_(
31 ScriptContext::GetDataSourceURLForFrame(render_frame->GetWebFrame())), 33 ScriptContext::GetDataSourceURLForFrame(render_frame->GetWebFrame())),
32 finished_(false) { 34 finished_(false) {
33 effective_url_ = ScriptContext::GetEffectiveDocumentURL( 35 if (url_.SchemeIs(url::kAboutScheme)) {
34 render_frame->GetWebFrame(), url_, params.match_about_blank); 36 origin_for_about_error_ =
37 render_frame->GetWebFrame()->securityOrigin().toString().utf8();
38 }
35 } 39 }
36 40
37 ProgrammaticScriptInjector::~ProgrammaticScriptInjector() { 41 ProgrammaticScriptInjector::~ProgrammaticScriptInjector() {
38 } 42 }
39 43
40 UserScript::InjectionType ProgrammaticScriptInjector::script_type() 44 UserScript::InjectionType ProgrammaticScriptInjector::script_type()
41 const { 45 const {
42 return UserScript::PROGRAMMATIC_SCRIPT; 46 return UserScript::PROGRAMMATIC_SCRIPT;
43 } 47 }
44 48
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 results_.Append(std::move(execution_result)); 126 results_.Append(std::move(execution_result));
123 Finish(std::string(), render_frame); 127 Finish(std::string(), render_frame);
124 } 128 }
125 129
126 void ProgrammaticScriptInjector::OnWillNotInject( 130 void ProgrammaticScriptInjector::OnWillNotInject(
127 InjectFailureReason reason, 131 InjectFailureReason reason,
128 content::RenderFrame* render_frame) { 132 content::RenderFrame* render_frame) {
129 std::string error; 133 std::string error;
130 switch (reason) { 134 switch (reason) {
131 case NOT_ALLOWED: 135 case NOT_ALLOWED:
132 if (url_.SchemeIs(url::kAboutScheme)) { 136 if (!CanShowUrlInError()) {
137 error = manifest_errors::kCannotAccessPage;
138 } else if (!origin_for_about_error_.empty()) {
133 error = ErrorUtils::FormatErrorMessage( 139 error = ErrorUtils::FormatErrorMessage(
134 manifest_errors::kCannotAccessAboutUrl, url_.spec(), 140 manifest_errors::kCannotAccessAboutUrl, url_.spec(),
135 effective_url_.GetOrigin().spec()); 141 origin_for_about_error_);
136 } else { 142 } else {
137 // TODO(?) It would be nice to show kCannotAccessPageWithUrl here if 143 error = ErrorUtils::FormatErrorMessage(
138 // this is triggered by an extension with tabs permission. See 144 manifest_errors::kCannotAccessPageWithUrl, url_.spec());
139 // https://codereview.chromium.org/1414223005/diff/1/extensions/
140 // common/manifest_constants.cc#newcode269
141 error = manifest_errors::kCannotAccessPage;
142 } 145 }
143 break; 146 break;
144 case EXTENSION_REMOVED: // no special error here. 147 case EXTENSION_REMOVED: // no special error here.
145 case WONT_INJECT: 148 case WONT_INJECT:
146 break; 149 break;
147 } 150 }
148 Finish(error, render_frame); 151 Finish(error, render_frame);
149 } 152 }
150 153
154 bool ProgrammaticScriptInjector::CanShowUrlInError() const {
155 if (params_->host_id.type() != HostID::EXTENSIONS)
156 return false;
157 const Extension* extension =
158 RendererExtensionRegistry::Get()->GetByID(params_->host_id.id());
159 if (!extension)
160 return false;
161 return extension->permissions_data()->active_permissions().HasAPIPermission(
Devlin 2016/01/25 19:36:40 It might be worth a note that we don't need to acc
robwu 2016/01/26 11:03:02 Even with activeTab, one does not get access to re
Devlin 2016/01/26 20:40:36 Nah - activeTab shouldn't really grant any access
robwu 2016/01/26 23:57:58 Acknowledged.
162 APIPermission::kTab);
163 }
164
151 UserScript::RunLocation ProgrammaticScriptInjector::GetRunLocation() const { 165 UserScript::RunLocation ProgrammaticScriptInjector::GetRunLocation() const {
152 return static_cast<UserScript::RunLocation>(params_->run_at); 166 return static_cast<UserScript::RunLocation>(params_->run_at);
153 } 167 }
154 168
155 void ProgrammaticScriptInjector::Finish(const std::string& error, 169 void ProgrammaticScriptInjector::Finish(const std::string& error,
156 content::RenderFrame* render_frame) { 170 content::RenderFrame* render_frame) {
157 DCHECK(!finished_); 171 DCHECK(!finished_);
158 finished_ = true; 172 finished_ = true;
159 173
160 // It's possible that the render frame was destroyed in the course of 174 // It's possible that the render frame was destroyed in the course of
161 // injecting scripts. Don't respond if it was (the browser side watches for 175 // injecting scripts. Don't respond if it was (the browser side watches for
162 // frame deletions so nothing is left hanging). 176 // frame deletions so nothing is left hanging).
163 if (render_frame) { 177 if (render_frame) {
164 render_frame->Send( 178 render_frame->Send(
165 new ExtensionHostMsg_ExecuteCodeFinished( 179 new ExtensionHostMsg_ExecuteCodeFinished(
166 render_frame->GetRoutingID(), params_->request_id, 180 render_frame->GetRoutingID(), params_->request_id,
167 error, url_, results_)); 181 error, url_, results_));
168 } 182 }
169 } 183 }
170 184
171 } // namespace extensions 185 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698