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
3 // found in the LICENSE file.
5 #include "extensions/renderer/programmatic_script_injector.h"
9 #include "base/values.h"
10 #include "content/public/common/url_constants.h"
11 #include "content/public/renderer/render_frame.h"
12 #include "content/public/renderer/render_view.h"
13 #include "extensions/common/error_utils.h"
14 #include "extensions/common/extension_messages.h"
15 #include "extensions/common/manifest_constants.h"
16 #include "extensions/common/permissions/permissions_data.h"
17 #include "extensions/renderer/injection_host.h"
18 #include "extensions/renderer/script_context.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebLocalFrame.h"
22 #include "third_party/WebKit/public/web/WebScriptSource.h"
24 namespace extensions
{
26 ProgrammaticScriptInjector::ProgrammaticScriptInjector(
27 const ExtensionMsg_ExecuteCode_Params
& params
,
28 content::RenderFrame
* render_frame
)
29 : params_(new ExtensionMsg_ExecuteCode_Params(params
)),
30 url_(ScriptContext::GetDataSourceURLForFrame(
31 render_frame
->GetWebFrame())),
32 render_view_(render_frame
->GetRenderView()),
34 effective_url_
= ScriptContext::GetEffectiveDocumentURL(
35 render_frame
->GetWebFrame(), url_
, params
.match_about_blank
);
38 ProgrammaticScriptInjector::~ProgrammaticScriptInjector() {
41 UserScript::InjectionType
ProgrammaticScriptInjector::script_type()
43 return UserScript::PROGRAMMATIC_SCRIPT
;
46 bool ProgrammaticScriptInjector::ShouldExecuteInMainWorld() const {
47 return params_
->in_main_world
;
50 bool ProgrammaticScriptInjector::IsUserGesture() const {
51 return params_
->user_gesture
;
54 bool ProgrammaticScriptInjector::ExpectsResults() const {
55 return params_
->wants_result
;
58 bool ProgrammaticScriptInjector::ShouldInjectJs(
59 UserScript::RunLocation run_location
) const {
60 return GetRunLocation() == run_location
&& params_
->is_javascript
;
63 bool ProgrammaticScriptInjector::ShouldInjectCss(
64 UserScript::RunLocation run_location
) const {
65 return GetRunLocation() == run_location
&& !params_
->is_javascript
;
68 PermissionsData::AccessType
ProgrammaticScriptInjector::CanExecuteOnFrame(
69 const InjectionHost
* injection_host
,
70 blink::WebLocalFrame
* frame
,
72 GURL effective_document_url
= ScriptContext::GetEffectiveDocumentURL(
73 frame
, frame
->document().url(), params_
->match_about_blank
);
74 if (params_
->is_web_view
) {
75 if (frame
->parent()) {
76 // This is a subframe inside <webview>, so allow it.
77 return PermissionsData::ACCESS_ALLOWED
;
80 return effective_document_url
== params_
->webview_src
81 ? PermissionsData::ACCESS_ALLOWED
82 : PermissionsData::ACCESS_DENIED
;
84 DCHECK_EQ(injection_host
->id().type(), HostID::EXTENSIONS
);
86 return injection_host
->CanExecuteOnFrame(
87 effective_document_url
,
88 content::RenderFrame::FromWebFrame(frame
),
90 true /* is_declarative */);
93 std::vector
<blink::WebScriptSource
> ProgrammaticScriptInjector::GetJsSources(
94 UserScript::RunLocation run_location
) const {
95 DCHECK_EQ(GetRunLocation(), run_location
);
96 DCHECK(params_
->is_javascript
);
98 return std::vector
<blink::WebScriptSource
>(
100 blink::WebScriptSource(
101 blink::WebString::fromUTF8(params_
->code
), params_
->file_url
));
104 std::vector
<std::string
> ProgrammaticScriptInjector::GetCssSources(
105 UserScript::RunLocation run_location
) const {
106 DCHECK_EQ(GetRunLocation(), run_location
);
107 DCHECK(!params_
->is_javascript
);
109 return std::vector
<std::string
>(1, params_
->code
);
112 void ProgrammaticScriptInjector::GetRunInfo(
113 ScriptsRunInfo
* scripts_run_info
,
114 UserScript::RunLocation run_location
) const {
117 void ProgrammaticScriptInjector::OnInjectionComplete(
118 scoped_ptr
<base::Value
> execution_result
,
119 UserScript::RunLocation run_location
) {
120 DCHECK(results_
.empty());
121 if (execution_result
)
122 results_
.Append(execution_result
.Pass());
123 Finish(std::string());
126 void ProgrammaticScriptInjector::OnWillNotInject(InjectFailureReason reason
) {
130 if (url_
.SchemeIs(url::kAboutScheme
)) {
131 error
= ErrorUtils::FormatErrorMessage(
132 manifest_errors::kCannotAccessAboutUrl
, url_
.spec(),
133 effective_url_
.GetOrigin().spec());
135 error
= ErrorUtils::FormatErrorMessage(
136 manifest_errors::kCannotAccessPage
, url_
.spec());
139 case EXTENSION_REMOVED
: // no special error here.
146 UserScript::RunLocation
ProgrammaticScriptInjector::GetRunLocation() const {
147 return static_cast<UserScript::RunLocation
>(params_
->run_at
);
150 void ProgrammaticScriptInjector::Finish(const std::string
& error
) {
154 render_view_
->Send(new ExtensionHostMsg_ExecuteCodeFinished(
155 render_view_
->GetRoutingID(),
162 } // namespace extensions