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

Side by Side Diff: extensions/browser/api/execute_code_function_impl.h

Issue 541753004: Split web_view_internal_api and move part of it to extensions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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/api/tabs/tabs_constants.h" 10 #include "extensions/browser/api/tabs/tabs_constants.h"
(...skipping 15 matching lines...) Expand all
26 template <typename T> 26 template <typename T>
27 ExecuteCodeFunction<T>::ExecuteCodeFunction() { 27 ExecuteCodeFunction<T>::ExecuteCodeFunction() {
28 } 28 }
29 29
30 template <typename T> 30 template <typename T>
31 ExecuteCodeFunction<T>::~ExecuteCodeFunction() { 31 ExecuteCodeFunction<T>::~ExecuteCodeFunction() {
32 } 32 }
33 33
34 template <typename T> 34 template <typename T>
35 void ExecuteCodeFunction<T>::DidLoadFile(bool success, 35 void ExecuteCodeFunction<T>::DidLoadFile(bool success,
36 const std::string& data) { 36 const std::string& data) {
37
38 if (!success || !details_->file) { 37 if (!success || !details_->file) {
39 DidLoadAndLocalizeFile(success, data); 38 DidLoadAndLocalizeFile(success, data);
40 return; 39 return;
41 } 40 }
42 41
43 ScriptExecutor::ScriptType script_type = 42 ScriptExecutor::ScriptType script_type =
44 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT; 43 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT;
45 44
46 std::string extension_id; 45 std::string extension_id;
47 base::FilePath extension_path; 46 base::FilePath extension_path;
48 std::string extension_default_locale; 47 std::string extension_default_locale;
49 if (T::extension()) { 48 if (T::extension()) {
50 extension_id = T::extension()->id(); 49 extension_id = T::extension()->id();
51 extension_path = T::extension()->path(); 50 extension_path = T::extension()->path();
52 T::extension()->manifest()->GetString(manifest_keys::kDefaultLocale, 51 T::extension()->manifest()->GetString(manifest_keys::kDefaultLocale,
53 &extension_default_locale); 52 &extension_default_locale);
54 } 53 }
55 54
56 content::BrowserThread::PostTask( 55 content::BrowserThread::PostTask(
57 content::BrowserThread::FILE, FROM_HERE, 56 content::BrowserThread::FILE,
Fady Samuel 2014/09/04 20:41:09 Can you make these changes in the ExecuteCodeFunct
58 base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS, this, 57 FROM_HERE,
59 script_type, 58 base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS,
60 data, 59 this,
61 extension_id, 60 script_type,
62 extension_path, 61 data,
63 extension_default_locale)); 62 extension_id,
63 extension_path,
64 extension_default_locale));
64 } 65 }
65 66
66 template <typename T> 67 template <typename T>
67 void ExecuteCodeFunction<T>::GetFileURLAndLocalizeCSS( 68 void ExecuteCodeFunction<T>::GetFileURLAndLocalizeCSS(
68 ScriptExecutor::ScriptType script_type, 69 ScriptExecutor::ScriptType script_type,
69 const std::string& data, 70 const std::string& data,
70 const std::string& extension_id, 71 const std::string& extension_id,
71 const base::FilePath& extension_path, 72 const base::FilePath& extension_path,
72 const std::string& extension_default_locale) { 73 const std::string& extension_default_locale) {
73
74 std::string localized_data = data; 74 std::string localized_data = data;
75 // Check if the file is CSS and needs localization. 75 // Check if the file is CSS and needs localization.
76 if ((script_type == ScriptExecutor::CSS) && 76 if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() &&
77 !extension_id.empty() &&
78 (data.find(MessageBundle::kMessageBegin) != std::string::npos)) { 77 (data.find(MessageBundle::kMessageBegin) != std::string::npos)) {
79 scoped_ptr<SubstitutionMap> localization_messages( 78 scoped_ptr<SubstitutionMap> localization_messages(
80 file_util::LoadMessageBundleSubstitutionMap( 79 file_util::LoadMessageBundleSubstitutionMap(
81 extension_path, extension_id, extension_default_locale)); 80 extension_path, extension_id, extension_default_locale));
82 81
83 // We need to do message replacement on the data, so it has to be mutable. 82 // We need to do message replacement on the data, so it has to be mutable.
84 std::string error; 83 std::string error;
85 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, 84 MessageBundle::ReplaceMessagesWithExternalDictionary(
86 &localized_data, 85 *localization_messages, &localized_data, &error);
87 &error);
88 } 86 }
89 87
90 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); 88 file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
91 89
92 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter 90 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter
93 // is always true, because if loading had failed, we wouldn't have had 91 // is always true, because if loading had failed, we wouldn't have had
94 // anything to localize. 92 // anything to localize.
95 content::BrowserThread::PostTask( 93 content::BrowserThread::PostTask(
96 content::BrowserThread::UI, FROM_HERE, 94 content::BrowserThread::UI,
97 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, 95 FROM_HERE,
98 true, localized_data)); 96 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile,
97 this,
98 true,
99 localized_data));
99 } 100 }
100 101
101 template <typename T> 102 template <typename T>
102 void ExecuteCodeFunction<T>::DidLoadAndLocalizeFile(bool success, 103 void ExecuteCodeFunction<T>::DidLoadAndLocalizeFile(bool success,
103 const std::string& data) { 104 const std::string& data) {
104 if (success) { 105 if (success) {
105 if (!Execute(data)) 106 if (!Execute(data))
106 T::SendResponse(false); 107 T::SendResponse(false);
107 } else { 108 } else {
108 // TODO(viettrungluu): bug: there's no particular reason the path should be 109 // TODO(viettrungluu): bug: there's no particular reason the path should be
109 // UTF-8, in which case this may fail. 110 // UTF-8, in which case this may fail.
110 T::error_ = ErrorUtils::FormatErrorMessage(tabs_constants::kLoadFileError, 111 T::error_ = ErrorUtils::FormatErrorMessage(
112 tabs_constants::kLoadFileError,
111 resource_.relative_path().AsUTF8Unsafe()); 113 resource_.relative_path().AsUTF8Unsafe());
112 T::SendResponse(false); 114 T::SendResponse(false);
113 } 115 }
114 } 116 }
115 117
116 template <typename T> 118 template <typename T>
117 bool ExecuteCodeFunction<T>::Execute(const std::string& code_string) { 119 bool ExecuteCodeFunction<T>::Execute(const std::string& code_string) {
118 ScriptExecutor* executor = GetScriptExecutor(); 120 ScriptExecutor* executor = GetScriptExecutor();
119 if (!executor) 121 if (!executor)
120 return false; 122 return false;
121 123
122 if (!T::extension()) 124 if (!T::extension())
123 return false; 125 return false;
124 126
125 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; 127 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT;
126 if (ShouldInsertCSS()) 128 if (ShouldInsertCSS())
127 script_type = ScriptExecutor::CSS; 129 script_type = ScriptExecutor::CSS;
128 130
129 ScriptExecutor::FrameScope frame_scope = 131 ScriptExecutor::FrameScope frame_scope =
130 details_->all_frames.get() && *details_->all_frames ? 132 details_->all_frames.get() && *details_->all_frames
131 ScriptExecutor::ALL_FRAMES : 133 ? ScriptExecutor::ALL_FRAMES
132 ScriptExecutor::TOP_FRAME; 134 : ScriptExecutor::TOP_FRAME;
133 135
134 ScriptExecutor::MatchAboutBlank match_about_blank = 136 ScriptExecutor::MatchAboutBlank match_about_blank =
135 details_->match_about_blank.get() && *details_->match_about_blank ? 137 details_->match_about_blank.get() && *details_->match_about_blank
136 ScriptExecutor::MATCH_ABOUT_BLANK : 138 ? ScriptExecutor::MATCH_ABOUT_BLANK
137 ScriptExecutor::DONT_MATCH_ABOUT_BLANK; 139 : ScriptExecutor::DONT_MATCH_ABOUT_BLANK;
138 140
139 UserScript::RunLocation run_at = 141 UserScript::RunLocation run_at = UserScript::UNDEFINED;
140 UserScript::UNDEFINED;
141 switch (details_->run_at) { 142 switch (details_->run_at) {
142 case InjectDetails::RUN_AT_NONE: 143 case InjectDetails::RUN_AT_NONE:
143 case InjectDetails::RUN_AT_DOCUMENT_IDLE: 144 case InjectDetails::RUN_AT_DOCUMENT_IDLE:
144 run_at = UserScript::DOCUMENT_IDLE; 145 run_at = UserScript::DOCUMENT_IDLE;
145 break; 146 break;
146 case InjectDetails::RUN_AT_DOCUMENT_START: 147 case InjectDetails::RUN_AT_DOCUMENT_START:
147 run_at = UserScript::DOCUMENT_START; 148 run_at = UserScript::DOCUMENT_START;
148 break; 149 break;
149 case InjectDetails::RUN_AT_DOCUMENT_END: 150 case InjectDetails::RUN_AT_DOCUMENT_END:
150 run_at = UserScript::DOCUMENT_END; 151 run_at = UserScript::DOCUMENT_END;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 if (!details_->file.get()) 199 if (!details_->file.get())
199 return false; 200 return false;
200 resource_ = T::extension()->GetResource(*details_->file); 201 resource_ = T::extension()->GetResource(*details_->file);
201 202
202 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { 203 if (resource_.extension_root().empty() || resource_.relative_path().empty()) {
203 T::error_ = tabs_constants::kNoCodeOrFileToExecuteError; 204 T::error_ = tabs_constants::kNoCodeOrFileToExecuteError;
204 return false; 205 return false;
205 } 206 }
206 207
207 int resource_id; 208 int resource_id;
208 if (ExtensionsBrowserClient::Get()->GetComponentExtensionResourceManager()-> 209 if (ExtensionsBrowserClient::Get()
209 IsComponentExtensionResource( 210 ->GetComponentExtensionResourceManager()
210 resource_.extension_root(), resource_.relative_path(), 211 ->IsComponentExtensionResource(resource_.extension_root(),
211 &resource_id)) { 212 resource_.relative_path(),
213 &resource_id)) {
212 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 214 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
213 DidLoadFile(true, rb.GetRawDataResource(resource_id).as_string()); 215 DidLoadFile(true, rb.GetRawDataResource(resource_id).as_string());
214 } else { 216 } else {
215 scoped_refptr<FileReader> file_reader(new FileReader( 217 scoped_refptr<FileReader> file_reader(new FileReader(
216 resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this))); 218 resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this)));
217 file_reader->Start(); 219 file_reader->Start();
218 } 220 }
219 221
220 return true; 222 return true;
221 } 223 }
(...skipping 10 matching lines...) Expand all
232 } 234 }
233 235
234 template <typename T> 236 template <typename T>
235 bool ExecuteCodeFunction<T>::ValidationFailure(T* function) { 237 bool ExecuteCodeFunction<T>::ValidationFailure(T* function) {
236 return T::ValidationFailure(function); 238 return T::ValidationFailure(function);
237 } 239 }
238 240
239 } // namespace extensions 241 } // namespace extensions
240 242
241 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ 243 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
242
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698