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 "extensions/common/error_utils.h"
13 #include "extensions/common/extension_messages.h"
14 #include "extensions/common/manifest_constants.h"
15 #include "extensions/common/permissions/permissions_data.h"
16 #include "extensions/renderer/injection_host.h"
17 #include "extensions/renderer/script_context.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "third_party/WebKit/public/web/WebDocument.h"
20 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 #include "third_party/WebKit/public/web/WebScriptSource.h"
23 namespace extensions
{
25 ProgrammaticScriptInjector::ProgrammaticScriptInjector(
26 const ExtensionMsg_ExecuteCode_Params
& params
,
27 content::RenderFrame
* render_frame
)
28 : params_(new ExtensionMsg_ExecuteCode_Params(params
)),
29 url_(ScriptContext::GetDataSourceURLForFrame(
30 render_frame
->GetWebFrame())),
31 render_frame_(render_frame
),
33 effective_url_
= ScriptContext::GetEffectiveDocumentURL(
34 render_frame
->GetWebFrame(), url_
, params
.match_about_blank
);
37 ProgrammaticScriptInjector::~ProgrammaticScriptInjector() {
40 UserScript::InjectionType
ProgrammaticScriptInjector::script_type()
42 return UserScript::PROGRAMMATIC_SCRIPT
;
45 bool ProgrammaticScriptInjector::ShouldExecuteInMainWorld() const {
46 return params_
->in_main_world
;
49 bool ProgrammaticScriptInjector::IsUserGesture() const {
50 return params_
->user_gesture
;
53 bool ProgrammaticScriptInjector::ExpectsResults() const {
54 return params_
->wants_result
;
57 bool ProgrammaticScriptInjector::ShouldInjectJs(
58 UserScript::RunLocation run_location
) const {
59 return GetRunLocation() == run_location
&& params_
->is_javascript
;
62 bool ProgrammaticScriptInjector::ShouldInjectCss(
63 UserScript::RunLocation run_location
) const {
64 return GetRunLocation() == run_location
&& !params_
->is_javascript
;
67 PermissionsData::AccessType
ProgrammaticScriptInjector::CanExecuteOnFrame(
68 const InjectionHost
* injection_host
,
69 blink::WebLocalFrame
* frame
,
71 GURL effective_document_url
= ScriptContext::GetEffectiveDocumentURL(
72 frame
, frame
->document().url(), params_
->match_about_blank
);
73 if (params_
->is_web_view
) {
74 if (frame
->parent()) {
75 // This is a subframe inside <webview>, so allow it.
76 return PermissionsData::ACCESS_ALLOWED
;
79 return effective_document_url
== params_
->webview_src
80 ? PermissionsData::ACCESS_ALLOWED
81 : PermissionsData::ACCESS_DENIED
;
83 DCHECK_EQ(injection_host
->id().type(), HostID::EXTENSIONS
);
85 return injection_host
->CanExecuteOnFrame(
86 effective_document_url
,
87 content::RenderFrame::FromWebFrame(frame
),
89 true /* is_declarative */);
92 std::vector
<blink::WebScriptSource
> ProgrammaticScriptInjector::GetJsSources(
93 UserScript::RunLocation run_location
) const {
94 DCHECK_EQ(GetRunLocation(), run_location
);
95 DCHECK(params_
->is_javascript
);
97 return std::vector
<blink::WebScriptSource
>(
99 blink::WebScriptSource(
100 blink::WebString::fromUTF8(params_
->code
), params_
->file_url
));
103 std::vector
<std::string
> ProgrammaticScriptInjector::GetCssSources(
104 UserScript::RunLocation run_location
) const {
105 DCHECK_EQ(GetRunLocation(), run_location
);
106 DCHECK(!params_
->is_javascript
);
108 return std::vector
<std::string
>(1, params_
->code
);
111 void ProgrammaticScriptInjector::GetRunInfo(
112 ScriptsRunInfo
* scripts_run_info
,
113 UserScript::RunLocation run_location
) const {
116 void ProgrammaticScriptInjector::OnInjectionComplete(
117 scoped_ptr
<base::Value
> execution_result
,
118 UserScript::RunLocation run_location
) {
119 DCHECK(results_
.empty());
120 if (execution_result
)
121 results_
.Append(execution_result
.Pass());
122 Finish(std::string());
125 void ProgrammaticScriptInjector::OnWillNotInject(InjectFailureReason reason
) {
129 if (url_
.SchemeIs(url::kAboutScheme
)) {
130 error
= ErrorUtils::FormatErrorMessage(
131 manifest_errors::kCannotAccessAboutUrl
, url_
.spec(),
132 effective_url_
.GetOrigin().spec());
134 error
= ErrorUtils::FormatErrorMessage(
135 manifest_errors::kCannotAccessPage
, url_
.spec());
138 case EXTENSION_REMOVED
: // no special error here.
145 UserScript::RunLocation
ProgrammaticScriptInjector::GetRunLocation() const {
146 return static_cast<UserScript::RunLocation
>(params_
->run_at
);
149 void ProgrammaticScriptInjector::Finish(const std::string
& error
) {
153 render_frame_
->Send(new ExtensionHostMsg_ExecuteCodeFinished(
154 render_frame_
->GetRoutingID(),
161 } // namespace extensions