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

Side by Side Diff: extensions/browser/api/guest_view/web_view/web_view_internal_api.cc

Issue 2227193002: Make UserScript non-copyable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove browser/renderer specific file impl as it is not helping much Created 4 years, 4 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/browser/api/guest_view/web_view/web_view_internal_api.h" 5 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 if (web_contents && web_contents->GetWebUI()) { 79 if (web_contents && web_contents->GetWebUI()) {
80 const GURL& url = web_contents->GetSiteInstance()->GetSiteURL(); 80 const GURL& url = web_contents->GetSiteInstance()->GetSiteURL();
81 return HostID(HostID::WEBUI, url.spec()); 81 return HostID(HostID::WEBUI, url.spec());
82 } 82 }
83 NOTREACHED(); 83 NOTREACHED();
84 return HostID(); 84 return HostID();
85 } 85 }
86 86
87 // Creates content script files when parsing InjectionItems of "js" or "css" 87 // Creates content script files when parsing InjectionItems of "js" or "css"
88 // proterties, and stores them in the |result|. 88 // proterties, and stores them in the |result|.
89 void AddScriptFiles(const GURL& owner_base_url, 89 void ParseScriptFiles(const GURL& owner_base_url,
90 const extensions::Extension* extension, 90 const extensions::Extension* extension,
91 const InjectionItems& items, 91 const InjectionItems& items,
92 UserScript::FileList* result) { 92 UserScript::FileList* list) {
93 DCHECK(list->size() == 0u);
Devlin 2016/08/17 16:39:30 list->empty()
lazyboy 2016/08/17 18:55:52 Done.
94 list->reserve((items.files.get() ? items.files->size() : 0) +
95 (items.code.get() ? 1 : 0));
Devlin 2016/08/17 16:39:30 ditto re unique_ptr bool op
lazyboy 2016/08/17 18:55:52 Done.
93 // files: 96 // files:
94 if (items.files) { 97 if (items.files) {
95 for (const std::string& relative : *items.files) { 98 for (const std::string& relative : *items.files) {
96 GURL url = owner_base_url.Resolve(relative); 99 GURL url = owner_base_url.Resolve(relative);
97 if (extension) { 100 if (extension) {
98 ExtensionResource resource = extension->GetResource(relative); 101 ExtensionResource resource = extension->GetResource(relative);
99 result->push_back(UserScript::File(resource.extension_root(), 102
100 resource.relative_path(), url)); 103 list->push_back(base::WrapUnique(new extensions::UserScript::File(
104 resource.extension_root(), resource.relative_path(), url)));
101 } else { 105 } else {
102 result->push_back(extensions::UserScript::File(base::FilePath(), 106 list->push_back(base::WrapUnique(new extensions::UserScript::File(
103 base::FilePath(), url)); 107 base::FilePath(), base::FilePath(), url)));
104 } 108 }
105 } 109 }
106 } 110 }
107 // code: 111 // code:
108 if (items.code) { 112 if (items.code) {
109 extensions::UserScript::File file((base::FilePath()), (base::FilePath()), 113 std::unique_ptr<extensions::UserScript::File> file(
110 GURL()); 114 new extensions::UserScript::File(base::FilePath(), base::FilePath(),
111 file.set_content(*items.code); 115 GURL()));
112 result->push_back(file); 116 file->set_content(*items.code);
117 list->push_back(std::move(file));
113 } 118 }
114 } 119 }
115 120
116 // Parses the values stored in ContentScriptDetails, and constructs a 121 // Parses the values stored in ContentScriptDetails, and constructs a
117 // UserScript. 122 // user script.
118 bool ParseContentScript(const ContentScriptDetails& script_value, 123 std::unique_ptr<extensions::UserScript> ParseContentScript(
119 const extensions::Extension* extension, 124 const ContentScriptDetails& script_value,
120 const GURL& owner_base_url, 125 const extensions::Extension* extension,
121 UserScript* script, 126 const GURL& owner_base_url,
122 std::string* error) { 127 std::string* error) {
123 // matches (required): 128 // matches (required):
124 if (script_value.matches.empty()) 129 if (script_value.matches.empty())
125 return false; 130 return std::unique_ptr<extensions::UserScript>();
131
132 std::unique_ptr<extensions::UserScript> script =
133 base::WrapUnique(new extensions::UserScript());
126 134
127 // The default for WebUI is not having special access, but we can change that 135 // The default for WebUI is not having special access, but we can change that
128 // if needed. 136 // if needed.
129 bool allowed_everywhere = false; 137 bool allowed_everywhere = false;
130 if (extension && 138 if (extension &&
131 extensions::PermissionsData::CanExecuteScriptEverywhere(extension)) 139 extensions::PermissionsData::CanExecuteScriptEverywhere(extension))
132 allowed_everywhere = true; 140 allowed_everywhere = true;
133 141
134 for (const std::string& match : script_value.matches) { 142 for (const std::string& match : script_value.matches) {
135 URLPattern pattern(UserScript::ValidUserScriptSchemes(allowed_everywhere)); 143 URLPattern pattern(UserScript::ValidUserScriptSchemes(allowed_everywhere));
136 if (pattern.Parse(match) != URLPattern::PARSE_SUCCESS) { 144 if (pattern.Parse(match) != URLPattern::PARSE_SUCCESS) {
137 *error = errors::kInvalidMatches; 145 *error = errors::kInvalidMatches;
138 return false; 146 return std::unique_ptr<extensions::UserScript>();
139 } 147 }
140 script->add_url_pattern(pattern); 148 script->add_url_pattern(pattern);
141 } 149 }
142 150
143 // exclude_matches: 151 // exclude_matches:
144 if (script_value.exclude_matches) { 152 if (script_value.exclude_matches) {
145 const std::vector<std::string>& exclude_matches = 153 const std::vector<std::string>& exclude_matches =
146 *(script_value.exclude_matches.get()); 154 *(script_value.exclude_matches.get());
147 for (const std::string& exclude_match : exclude_matches) { 155 for (const std::string& exclude_match : exclude_matches) {
148 URLPattern pattern( 156 URLPattern pattern(
149 UserScript::ValidUserScriptSchemes(allowed_everywhere)); 157 UserScript::ValidUserScriptSchemes(allowed_everywhere));
150 158
151 if (pattern.Parse(exclude_match) != URLPattern::PARSE_SUCCESS) { 159 if (pattern.Parse(exclude_match) != URLPattern::PARSE_SUCCESS) {
152 *error = errors::kInvalidExcludeMatches; 160 *error = errors::kInvalidExcludeMatches;
153 return false; 161 return std::unique_ptr<extensions::UserScript>();
154 } 162 }
155 script->add_exclude_url_pattern(pattern); 163 script->add_exclude_url_pattern(pattern);
156 } 164 }
157 } 165 }
158 // run_at: 166 // run_at:
159 if (script_value.run_at) { 167 if (script_value.run_at) {
160 UserScript::RunLocation run_at = UserScript::UNDEFINED; 168 UserScript::RunLocation run_at = UserScript::UNDEFINED;
161 switch (script_value.run_at) { 169 switch (script_value.run_at) {
162 case extensions::api::extension_types::RUN_AT_NONE: 170 case extensions::api::extension_types::RUN_AT_NONE:
163 case extensions::api::extension_types::RUN_AT_DOCUMENT_IDLE: 171 case extensions::api::extension_types::RUN_AT_DOCUMENT_IDLE:
164 run_at = UserScript::DOCUMENT_IDLE; 172 run_at = UserScript::DOCUMENT_IDLE;
165 break; 173 break;
166 case extensions::api::extension_types::RUN_AT_DOCUMENT_START: 174 case extensions::api::extension_types::RUN_AT_DOCUMENT_START:
167 run_at = UserScript::DOCUMENT_START; 175 run_at = UserScript::DOCUMENT_START;
168 break; 176 break;
169 case extensions::api::extension_types::RUN_AT_DOCUMENT_END: 177 case extensions::api::extension_types::RUN_AT_DOCUMENT_END:
170 run_at = UserScript::DOCUMENT_END; 178 run_at = UserScript::DOCUMENT_END;
171 break; 179 break;
172 } 180 }
173 // The default for run_at is RUN_AT_DOCUMENT_IDLE. 181 // The default for run_at is RUN_AT_DOCUMENT_IDLE.
174 script->set_run_location(run_at); 182 script->set_run_location(run_at);
175 } 183 }
176 184
177 // match_about_blank: 185 // match_about_blank:
178 if (script_value.match_about_blank) 186 if (script_value.match_about_blank)
179 script->set_match_about_blank(*script_value.match_about_blank); 187 script->set_match_about_blank(*script_value.match_about_blank);
180 188
181 // css: 189 // css:
182 if (script_value.css) { 190 if (script_value.css) {
183 AddScriptFiles(owner_base_url, extension, *script_value.css, 191 ParseScriptFiles(owner_base_url, extension, *script_value.css,
184 &script->css_scripts()); 192 &script->css_scripts());
185 } 193 }
186 194
187 // js: 195 // js:
188 if (script_value.js) { 196 if (script_value.js) {
189 AddScriptFiles(owner_base_url, extension, *script_value.js, 197 ParseScriptFiles(owner_base_url, extension, *script_value.js,
190 &script->js_scripts()); 198 &script->js_scripts());
191 } 199 }
192 200
193 // all_frames: 201 // all_frames:
194 if (script_value.all_frames) 202 if (script_value.all_frames)
195 script->set_match_all_frames(*script_value.all_frames); 203 script->set_match_all_frames(*script_value.all_frames);
196 204
197 // include_globs: 205 // include_globs:
198 if (script_value.include_globs) { 206 if (script_value.include_globs) {
199 for (const std::string& glob : *script_value.include_globs) 207 for (const std::string& glob : *script_value.include_globs)
200 script->add_glob(glob); 208 script->add_glob(glob);
201 } 209 }
202 210
203 // exclude_globs: 211 // exclude_globs:
204 if (script_value.exclude_globs) { 212 if (script_value.exclude_globs) {
205 for (const std::string& glob : *script_value.exclude_globs) 213 for (const std::string& glob : *script_value.exclude_globs)
206 script->add_exclude_glob(glob); 214 script->add_exclude_glob(glob);
207 } 215 }
208 216
209 return true; 217 return script;
210 } 218 }
211 219
212 bool ParseContentScripts( 220 bool ParseContentScripts(
213 const std::vector<ContentScriptDetails>& content_script_list, 221 const std::vector<ContentScriptDetails>& content_script_list,
214 const extensions::Extension* extension, 222 const extensions::Extension* extension,
215 const HostID& host_id, 223 const HostID& host_id,
216 bool incognito_enabled, 224 bool incognito_enabled,
217 const GURL& owner_base_url, 225 const GURL& owner_base_url,
218 extensions::UserScriptList* result, 226 extensions::UserScriptList* result,
219 std::string* error) { 227 std::string* error) {
220 if (content_script_list.empty()) 228 if (content_script_list.empty())
221 return false; 229 return false;
222 230
223 std::set<std::string> names; 231 std::set<std::string> names;
224 for (const ContentScriptDetails& script_value : content_script_list) { 232 for (const ContentScriptDetails& script_value : content_script_list) {
225 const std::string& name = script_value.name; 233 const std::string& name = script_value.name;
226 if (!names.insert(name).second) { 234 if (!names.insert(name).second) {
227 // The name was already in the list. 235 // The name was already in the list.
228 *error = kDuplicatedContentScriptNamesError; 236 *error = kDuplicatedContentScriptNamesError;
229 return false; 237 return false;
230 } 238 }
231 239
232 UserScript script; 240 std::unique_ptr<extensions::UserScript> script =
233 if (!ParseContentScript(script_value, extension, owner_base_url, &script, 241 ParseContentScript(script_value, extension, owner_base_url, error);
234 error)) 242 if (!script.get())
235 return false; 243 return false;
236 244 script->set_id(UserScript::GenerateUserScriptID());
237 script.set_id(UserScript::GenerateUserScriptID()); 245 script->set_name(name);
238 script.set_name(name); 246 script->set_incognito_enabled(incognito_enabled);
239 script.set_incognito_enabled(incognito_enabled); 247 script->set_host_id(host_id);
240 script.set_host_id(host_id); 248 script->set_consumer_instance_type(extensions::UserScript::WEBVIEW);
241 script.set_consumer_instance_type(UserScript::WEBVIEW); 249 result->push_back(std::move(script));
242 result->push_back(script);
243 } 250 }
244 return true; 251 return true;
245 } 252 }
246 253
247 } // namespace 254 } // namespace
248 255
249 namespace extensions { 256 namespace extensions {
250 257
251 bool LegacyWebViewInternalExtensionFunction::RunAsync() { 258 bool LegacyWebViewInternalExtensionFunction::RunAsync() {
252 int instance_id = 0; 259 int instance_id = 0;
(...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 // Will finish asynchronously. 971 // Will finish asynchronously.
965 return true; 972 return true;
966 } 973 }
967 974
968 void WebViewInternalClearDataFunction::ClearDataDone() { 975 void WebViewInternalClearDataFunction::ClearDataDone() {
969 Release(); // Balanced in RunAsync(). 976 Release(); // Balanced in RunAsync().
970 SendResponse(true); 977 SendResponse(true);
971 } 978 }
972 979
973 } // namespace extensions 980 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698