OLD | NEW |
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/user_script_injector.h" | 5 #include "extensions/renderer/user_script_injector.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "content/public/common/url_constants.h" | 10 #include "content/public/common/url_constants.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 return blink::WebScriptSource(blink::WebString::fromUTF8(source_)); | 49 return blink::WebScriptSource(blink::WebString::fromUTF8(source_)); |
50 } | 50 } |
51 | 51 |
52 base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api = | 52 base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api = |
53 LAZY_INSTANCE_INITIALIZER; | 53 LAZY_INSTANCE_INITIALIZER; |
54 | 54 |
55 } // namespace | 55 } // namespace |
56 | 56 |
57 UserScriptInjector::UserScriptInjector( | 57 UserScriptInjector::UserScriptInjector( |
58 const UserScript* script, | 58 const UserScript* script, |
59 UserScriptSet* script_list) | 59 UserScriptSet* script_list, |
| 60 bool is_declarative) |
60 : script_(script), | 61 : script_(script), |
61 script_id_(script_->id()), | 62 script_id_(script_->id()), |
62 extension_id_(script_->extension_id()), | 63 extension_id_(script_->extension_id()), |
| 64 is_declarative_(is_declarative), |
63 user_script_set_observer_(this) { | 65 user_script_set_observer_(this) { |
64 user_script_set_observer_.Add(script_list); | 66 user_script_set_observer_.Add(script_list); |
65 } | 67 } |
66 | 68 |
67 UserScriptInjector::~UserScriptInjector() { | 69 UserScriptInjector::~UserScriptInjector() { |
68 } | 70 } |
69 | 71 |
| 72 bool UserScriptInjector::is_declarative() const { |
| 73 return is_declarative_; |
| 74 } |
| 75 |
70 void UserScriptInjector::OnUserScriptsUpdated( | 76 void UserScriptInjector::OnUserScriptsUpdated( |
71 const std::set<std::string>& changed_extensions, | 77 const std::set<std::string>& changed_extensions, |
72 const std::vector<UserScript*>& scripts) { | 78 const std::vector<UserScript*>& scripts) { |
73 // If the extension causing this injection changed, then this injection | 79 // If the extension causing this injection changed, then this injection |
74 // will be removed, and there's no guarantee the backing script still exists. | 80 // will be removed, and there's no guarantee the backing script still exists. |
75 if (changed_extensions.count(extension_id_) > 0) | 81 if (changed_extensions.count(extension_id_) > 0) |
76 return; | 82 return; |
77 | 83 |
78 for (std::vector<UserScript*>::const_iterator iter = scripts.begin(); | 84 for (std::vector<UserScript*>::const_iterator iter = scripts.begin(); |
79 iter != scripts.end(); | 85 iter != scripts.end(); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 int tab_id, | 131 int tab_id, |
126 const GURL& top_url) const { | 132 const GURL& top_url) const { |
127 // If we don't have a tab id, we have no UI surface to ask for user consent. | 133 // If we don't have a tab id, we have no UI surface to ask for user consent. |
128 // For now, we treat this as an automatic allow. | 134 // For now, we treat this as an automatic allow. |
129 if (tab_id == -1) | 135 if (tab_id == -1) |
130 return PermissionsData::ACCESS_ALLOWED; | 136 return PermissionsData::ACCESS_ALLOWED; |
131 | 137 |
132 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( | 138 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
133 web_frame, web_frame->document().url(), script_->match_about_blank()); | 139 web_frame, web_frame->document().url(), script_->match_about_blank()); |
134 | 140 |
135 return extension->permissions_data()->GetContentScriptAccess( | 141 // Declarative scripts use "page access" (from "permissions" section in |
136 extension, | 142 // manifest) whereas non-declarative scripts use custom |
137 effective_document_url, | 143 // "content script access" logic. |
138 top_url, | 144 if (is_declarative_) { |
139 tab_id, | 145 return extension->permissions_data()->GetPageAccess( |
140 -1, // no process id | 146 extension, |
141 NULL /* ignore error */); | 147 effective_document_url, |
| 148 top_url, |
| 149 tab_id, |
| 150 -1, // no process id |
| 151 NULL /* ignore error */); |
| 152 } else { |
| 153 return extension->permissions_data()->GetContentScriptAccess( |
| 154 extension, |
| 155 effective_document_url, |
| 156 top_url, |
| 157 tab_id, |
| 158 -1, // no process id |
| 159 NULL /* ignore error */); |
| 160 } |
142 } | 161 } |
143 | 162 |
144 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( | 163 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
145 UserScript::RunLocation run_location) const { | 164 UserScript::RunLocation run_location) const { |
146 DCHECK_EQ(script_->run_location(), run_location); | 165 DCHECK_EQ(script_->run_location(), run_location); |
147 | 166 |
148 std::vector<blink::WebScriptSource> sources; | 167 std::vector<blink::WebScriptSource> sources; |
149 const UserScript::FileList& js_scripts = script_->js_scripts(); | 168 const UserScript::FileList& js_scripts = script_->js_scripts(); |
150 bool is_standalone_or_emulate_greasemonkey = | 169 bool is_standalone_or_emulate_greasemonkey = |
151 script_->is_standalone() || script_->emulate_greasemonkey(); | 170 script_->is_standalone() || script_->emulate_greasemonkey(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 } | 224 } |
206 | 225 |
207 if (ShouldInjectCss(run_location)) | 226 if (ShouldInjectCss(run_location)) |
208 scripts_run_info->num_css += script_->css_scripts().size(); | 227 scripts_run_info->num_css += script_->css_scripts().size(); |
209 } | 228 } |
210 | 229 |
211 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason) { | 230 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason) { |
212 } | 231 } |
213 | 232 |
214 } // namespace extensions | 233 } // namespace extensions |
OLD | NEW |