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

Side by Side Diff: extensions/browser/api/execute_code_function.cc

Issue 1004253002: Enable <webview>.executeScript outside of Apps and Extensions [2] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Devlin's comments. Created 5 years, 9 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 #ifndef EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ 5 #ifndef EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
6 #define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ 6 #define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
7 7
8 #include "extensions/browser/api/execute_code_function.h" 8 #include "extensions/browser/api/execute_code_function.h"
9 9
10 #include "extensions/browser/component_extension_resource_manager.h" 10 #include "extensions/browser/component_extension_resource_manager.h"
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 content::BrowserThread::UI, 101 content::BrowserThread::UI,
102 FROM_HERE, 102 FROM_HERE,
103 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, 103 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile,
104 this, 104 this,
105 true, 105 true,
106 localized_data)); 106 localized_data));
107 } 107 }
108 108
109 void ExecuteCodeFunction::DidLoadAndLocalizeFile(bool success, 109 void ExecuteCodeFunction::DidLoadAndLocalizeFile(bool success,
110 const std::string& data) { 110 const std::string& data) {
111 DidLoadFileForHost(resource_.relative_path().AsUTF8Unsafe(), success, data);
112 }
113
114 void ExecuteCodeFunction::DidLoadFileForHost(const std::string& file,
Devlin 2015/03/25 17:55:15 Why not just make DidLoadAndLocalizeFile() take in
Xi Han 2015/03/25 20:49:21 That is much simpler! Thanks!
115 bool success,
116 const std::string& data) {
111 if (success) { 117 if (success) {
112 if (!base::IsStringUTF8(data)) { 118 if (!base::IsStringUTF8(data)) {
113 error_ = ErrorUtils::FormatErrorMessage( 119 error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file);
114 kBadFileEncodingError, resource_.relative_path().AsUTF8Unsafe());
115 SendResponse(false); 120 SendResponse(false);
116 } else if (!Execute(data)) 121 } else if (!Execute(data))
117 SendResponse(false); 122 SendResponse(false);
118 } else { 123 } else {
119 // TODO(viettrungluu): bug: there's no particular reason the path should be 124 // TODO(viettrungluu): bug: there's no particular reason the path should be
120 // UTF-8, in which case this may fail. 125 // UTF-8, in which case this may fail.
121 error_ = ErrorUtils::FormatErrorMessage( 126 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file);
122 kLoadFileError, resource_.relative_path().AsUTF8Unsafe());
123 SendResponse(false); 127 SendResponse(false);
124 } 128 }
125 } 129 }
126 130
127 bool ExecuteCodeFunction::Execute(const std::string& code_string) { 131 bool ExecuteCodeFunction::Execute(const std::string& code_string) {
128 ScriptExecutor* executor = GetScriptExecutor(); 132 ScriptExecutor* executor = GetScriptExecutor();
129 if (!executor) 133 if (!executor)
130 return false; 134 return false;
131 135
132 if (!extension() && !IsWebView()) 136 if (!extension() && !IsWebView())
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 202
199 if (!CanExecuteScriptOnPage()) 203 if (!CanExecuteScriptOnPage())
200 return false; 204 return false;
201 205
202 if (details_->code.get()) 206 if (details_->code.get())
203 return Execute(*details_->code); 207 return Execute(*details_->code);
204 208
205 if (!details_->file.get()) 209 if (!details_->file.get())
206 return false; 210 return false;
207 211
208 if (!extension()) 212 return LoadFile(*details_->file);
209 return false; 213 }
210 214
211 resource_ = extension()->GetResource(*details_->file); 215 bool ExecuteCodeFunction::LoadFile(const std::string& file) {
216 resource_ = extension()->GetResource(file);
212 217
213 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { 218 if (resource_.extension_root().empty() || resource_.relative_path().empty()) {
214 error_ = kNoCodeOrFileToExecuteError; 219 error_ = kNoCodeOrFileToExecuteError;
215 return false; 220 return false;
216 } 221 }
217 222
218 int resource_id; 223 int resource_id;
219 const ComponentExtensionResourceManager* 224 const ComponentExtensionResourceManager*
220 component_extension_resource_manager = 225 component_extension_resource_manager =
221 ExtensionsBrowserClient::Get() 226 ExtensionsBrowserClient::Get()
(...skipping 19 matching lines...) Expand all
241 const base::ListValue& result) { 246 const base::ListValue& result) {
242 if (!error.empty()) 247 if (!error.empty())
243 SetError(error); 248 SetError(error);
244 249
245 SendResponse(error.empty()); 250 SendResponse(error.empty());
246 } 251 }
247 252
248 } // namespace extensions 253 } // namespace extensions
249 254
250 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ 255 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698