OLD | NEW |
1 // Copyright (c) 2013 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 "chrome/browser/extensions/api/execute_code_function.h" | 5 #ifndef EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ |
| 6 #define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ |
6 | 7 |
7 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 8 #include "extensions/browser/api/execute_code_function.h" |
8 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" | 9 |
9 #include "extensions/browser/component_extension_resource_manager.h" | 10 #include "extensions/browser/component_extension_resource_manager.h" |
10 #include "extensions/browser/extensions_browser_client.h" | 11 #include "extensions/browser/extensions_browser_client.h" |
11 #include "extensions/browser/file_reader.h" | 12 #include "extensions/browser/file_reader.h" |
12 #include "extensions/common/error_utils.h" | 13 #include "extensions/common/error_utils.h" |
13 #include "extensions/common/extension_messages.h" | 14 #include "extensions/common/extension_messages.h" |
14 #include "extensions/common/file_util.h" | 15 #include "extensions/common/file_util.h" |
| 16 #include "extensions/common/manifest_constants.h" |
15 #include "extensions/common/message_bundle.h" | 17 #include "extensions/common/message_bundle.h" |
16 #include "net/base/filename_util.h" | 18 #include "net/base/filename_util.h" |
17 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
18 | 20 |
| 21 namespace { |
| 22 |
| 23 // Error messages |
| 24 const char kNoCodeOrFileToExecuteError[] = "No source code or file specified."; |
| 25 const char kMoreThanOneValuesError[] = |
| 26 "Code and file should not be specified " |
| 27 "at the same time in the second argument."; |
| 28 const char kLoadFileError[] = "Failed to load file: \"*\". "; |
| 29 |
| 30 } |
| 31 |
19 namespace extensions { | 32 namespace extensions { |
20 | 33 |
21 namespace keys = tabs_constants; | 34 using core_api::extension_types::InjectDetails; |
22 using api::tabs::InjectDetails; | |
23 | 35 |
24 ExecuteCodeFunction::ExecuteCodeFunction() { | 36 ExecuteCodeFunction::ExecuteCodeFunction() { |
25 } | 37 } |
26 | 38 |
27 ExecuteCodeFunction::~ExecuteCodeFunction() { | 39 ExecuteCodeFunction::~ExecuteCodeFunction() { |
28 } | 40 } |
29 | 41 |
30 void ExecuteCodeFunction::DidLoadFile(bool success, | 42 void ExecuteCodeFunction::DidLoadFile(bool success, const std::string& data) { |
31 const std::string& data) { | |
32 | |
33 if (!success || !details_->file) { | 43 if (!success || !details_->file) { |
34 DidLoadAndLocalizeFile(success, data); | 44 DidLoadAndLocalizeFile(success, data); |
35 return; | 45 return; |
36 } | 46 } |
37 | 47 |
38 ScriptExecutor::ScriptType script_type = | 48 ScriptExecutor::ScriptType script_type = |
39 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT; | 49 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT; |
40 | 50 |
41 std::string extension_id; | 51 std::string extension_id; |
42 base::FilePath extension_path; | 52 base::FilePath extension_path; |
43 std::string extension_default_locale; | 53 std::string extension_default_locale; |
44 if (extension()) { | 54 if (extension()) { |
45 extension_id = extension()->id(); | 55 extension_id = extension()->id(); |
46 extension_path = extension()->path(); | 56 extension_path = extension()->path(); |
47 extension_default_locale = LocaleInfo::GetDefaultLocale(extension()); | 57 extension()->manifest()->GetString(manifest_keys::kDefaultLocale, |
| 58 &extension_default_locale); |
48 } | 59 } |
49 | 60 |
50 content::BrowserThread::PostTask( | 61 content::BrowserThread::PostTask( |
51 content::BrowserThread::FILE, FROM_HERE, | 62 content::BrowserThread::FILE, |
52 base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS, this, | 63 FROM_HERE, |
53 script_type, | 64 base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS, |
54 data, | 65 this, |
55 extension_id, | 66 script_type, |
56 extension_path, | 67 data, |
57 extension_default_locale)); | 68 extension_id, |
| 69 extension_path, |
| 70 extension_default_locale)); |
58 } | 71 } |
59 | 72 |
60 void ExecuteCodeFunction::GetFileURLAndLocalizeCSS( | 73 void ExecuteCodeFunction::GetFileURLAndLocalizeCSS( |
61 ScriptExecutor::ScriptType script_type, | 74 ScriptExecutor::ScriptType script_type, |
62 const std::string& data, | 75 const std::string& data, |
63 const std::string& extension_id, | 76 const std::string& extension_id, |
64 const base::FilePath& extension_path, | 77 const base::FilePath& extension_path, |
65 const std::string& extension_default_locale) { | 78 const std::string& extension_default_locale) { |
66 | |
67 std::string localized_data = data; | 79 std::string localized_data = data; |
68 // Check if the file is CSS and needs localization. | 80 // Check if the file is CSS and needs localization. |
69 if ((script_type == ScriptExecutor::CSS) && | 81 if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() && |
70 !extension_id.empty() && | |
71 (data.find(MessageBundle::kMessageBegin) != std::string::npos)) { | 82 (data.find(MessageBundle::kMessageBegin) != std::string::npos)) { |
72 scoped_ptr<SubstitutionMap> localization_messages( | 83 scoped_ptr<SubstitutionMap> localization_messages( |
73 file_util::LoadMessageBundleSubstitutionMap( | 84 file_util::LoadMessageBundleSubstitutionMap( |
74 extension_path, extension_id, extension_default_locale)); | 85 extension_path, extension_id, extension_default_locale)); |
75 | 86 |
76 // We need to do message replacement on the data, so it has to be mutable. | 87 // We need to do message replacement on the data, so it has to be mutable. |
77 std::string error; | 88 std::string error; |
78 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, | 89 MessageBundle::ReplaceMessagesWithExternalDictionary( |
79 &localized_data, | 90 *localization_messages, &localized_data, &error); |
80 &error); | |
81 } | 91 } |
82 | 92 |
83 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); | 93 file_url_ = net::FilePathToFileURL(resource_.GetFilePath()); |
84 | 94 |
85 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter | 95 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter |
86 // is always true, because if loading had failed, we wouldn't have had | 96 // is always true, because if loading had failed, we wouldn't have had |
87 // anything to localize. | 97 // anything to localize. |
88 content::BrowserThread::PostTask( | 98 content::BrowserThread::PostTask( |
89 content::BrowserThread::UI, FROM_HERE, | 99 content::BrowserThread::UI, |
90 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this, | 100 FROM_HERE, |
91 true, localized_data)); | 101 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, |
| 102 this, |
| 103 true, |
| 104 localized_data)); |
92 } | 105 } |
93 | 106 |
94 void ExecuteCodeFunction::DidLoadAndLocalizeFile(bool success, | 107 void ExecuteCodeFunction::DidLoadAndLocalizeFile(bool success, |
95 const std::string& data) { | 108 const std::string& data) { |
96 if (success) { | 109 if (success) { |
97 if (!Execute(data)) | 110 if (!Execute(data)) |
98 SendResponse(false); | 111 SendResponse(false); |
99 } else { | 112 } else { |
100 // TODO(viettrungluu): bug: there's no particular reason the path should be | 113 // TODO(viettrungluu): bug: there's no particular reason the path should be |
101 // UTF-8, in which case this may fail. | 114 // UTF-8, in which case this may fail. |
102 error_ = ErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 115 error_ = ErrorUtils::FormatErrorMessage( |
103 resource_.relative_path().AsUTF8Unsafe()); | 116 kLoadFileError, resource_.relative_path().AsUTF8Unsafe()); |
104 SendResponse(false); | 117 SendResponse(false); |
105 } | 118 } |
106 } | 119 } |
107 | 120 |
108 bool ExecuteCodeFunction::Execute(const std::string& code_string) { | 121 bool ExecuteCodeFunction::Execute(const std::string& code_string) { |
109 ScriptExecutor* executor = GetScriptExecutor(); | 122 ScriptExecutor* executor = GetScriptExecutor(); |
110 if (!executor) | 123 if (!executor) |
111 return false; | 124 return false; |
112 | 125 |
113 if (!extension()) | 126 if (!extension()) |
114 return false; | 127 return false; |
115 | 128 |
116 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; | 129 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; |
117 if (ShouldInsertCSS()) | 130 if (ShouldInsertCSS()) |
118 script_type = ScriptExecutor::CSS; | 131 script_type = ScriptExecutor::CSS; |
119 | 132 |
120 ScriptExecutor::FrameScope frame_scope = | 133 ScriptExecutor::FrameScope frame_scope = |
121 details_->all_frames.get() && *details_->all_frames ? | 134 details_->all_frames.get() && *details_->all_frames |
122 ScriptExecutor::ALL_FRAMES : | 135 ? ScriptExecutor::ALL_FRAMES |
123 ScriptExecutor::TOP_FRAME; | 136 : ScriptExecutor::TOP_FRAME; |
124 | 137 |
125 ScriptExecutor::MatchAboutBlank match_about_blank = | 138 ScriptExecutor::MatchAboutBlank match_about_blank = |
126 details_->match_about_blank.get() && *details_->match_about_blank ? | 139 details_->match_about_blank.get() && *details_->match_about_blank |
127 ScriptExecutor::MATCH_ABOUT_BLANK : | 140 ? ScriptExecutor::MATCH_ABOUT_BLANK |
128 ScriptExecutor::DONT_MATCH_ABOUT_BLANK; | 141 : ScriptExecutor::DONT_MATCH_ABOUT_BLANK; |
129 | 142 |
130 UserScript::RunLocation run_at = | 143 UserScript::RunLocation run_at = UserScript::UNDEFINED; |
131 UserScript::UNDEFINED; | |
132 switch (details_->run_at) { | 144 switch (details_->run_at) { |
133 case InjectDetails::RUN_AT_NONE: | 145 case InjectDetails::RUN_AT_NONE: |
134 case InjectDetails::RUN_AT_DOCUMENT_IDLE: | 146 case InjectDetails::RUN_AT_DOCUMENT_IDLE: |
135 run_at = UserScript::DOCUMENT_IDLE; | 147 run_at = UserScript::DOCUMENT_IDLE; |
136 break; | 148 break; |
137 case InjectDetails::RUN_AT_DOCUMENT_START: | 149 case InjectDetails::RUN_AT_DOCUMENT_START: |
138 run_at = UserScript::DOCUMENT_START; | 150 run_at = UserScript::DOCUMENT_START; |
139 break; | 151 break; |
140 case InjectDetails::RUN_AT_DOCUMENT_END: | 152 case InjectDetails::RUN_AT_DOCUMENT_END: |
141 run_at = UserScript::DOCUMENT_END; | 153 run_at = UserScript::DOCUMENT_END; |
(...skipping 21 matching lines...) Expand all Loading... |
163 } | 175 } |
164 | 176 |
165 bool ExecuteCodeFunction::HasPermission() { | 177 bool ExecuteCodeFunction::HasPermission() { |
166 return true; | 178 return true; |
167 } | 179 } |
168 | 180 |
169 bool ExecuteCodeFunction::RunAsync() { | 181 bool ExecuteCodeFunction::RunAsync() { |
170 EXTENSION_FUNCTION_VALIDATE(Init()); | 182 EXTENSION_FUNCTION_VALIDATE(Init()); |
171 | 183 |
172 if (!details_->code.get() && !details_->file.get()) { | 184 if (!details_->code.get() && !details_->file.get()) { |
173 error_ = keys::kNoCodeOrFileToExecuteError; | 185 error_ = kNoCodeOrFileToExecuteError; |
174 return false; | 186 return false; |
175 } | 187 } |
176 if (details_->code.get() && details_->file.get()) { | 188 if (details_->code.get() && details_->file.get()) { |
177 error_ = keys::kMoreThanOneValuesError; | 189 error_ = kMoreThanOneValuesError; |
178 return false; | 190 return false; |
179 } | 191 } |
180 | 192 |
181 if (!CanExecuteScriptOnPage()) | 193 if (!CanExecuteScriptOnPage()) |
182 return false; | 194 return false; |
183 | 195 |
184 if (details_->code.get()) | 196 if (details_->code.get()) |
185 return Execute(*details_->code); | 197 return Execute(*details_->code); |
186 | 198 |
187 if (!details_->file.get()) | 199 if (!details_->file.get()) |
188 return false; | 200 return false; |
189 resource_ = extension()->GetResource(*details_->file); | 201 resource_ = extension()->GetResource(*details_->file); |
190 | 202 |
191 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { | 203 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { |
192 error_ = keys::kNoCodeOrFileToExecuteError; | 204 error_ = kNoCodeOrFileToExecuteError; |
193 return false; | 205 return false; |
194 } | 206 } |
195 | 207 |
196 int resource_id; | 208 int resource_id; |
197 if (ExtensionsBrowserClient::Get()->GetComponentExtensionResourceManager()-> | 209 if (ExtensionsBrowserClient::Get() |
198 IsComponentExtensionResource( | 210 ->GetComponentExtensionResourceManager() |
199 resource_.extension_root(), resource_.relative_path(), | 211 ->IsComponentExtensionResource(resource_.extension_root(), |
200 &resource_id)) { | 212 resource_.relative_path(), |
| 213 &resource_id)) { |
201 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 214 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
202 DidLoadFile(true, rb.GetRawDataResource(resource_id).as_string()); | 215 DidLoadFile(true, rb.GetRawDataResource(resource_id).as_string()); |
203 } else { | 216 } else { |
204 scoped_refptr<FileReader> file_reader(new FileReader( | 217 scoped_refptr<FileReader> file_reader(new FileReader( |
205 resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this))); | 218 resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this))); |
206 file_reader->Start(); | 219 file_reader->Start(); |
207 } | 220 } |
208 | 221 |
209 return true; | 222 return true; |
210 } | 223 } |
211 | 224 |
212 void ExecuteCodeFunction::OnExecuteCodeFinished( | 225 void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error, |
213 const std::string& error, | 226 const GURL& on_url, |
214 const GURL& on_url, | 227 const base::ListValue& result) { |
215 const base::ListValue& result) { | |
216 if (!error.empty()) | 228 if (!error.empty()) |
217 SetError(error); | 229 SetError(error); |
218 | 230 |
219 SendResponse(error.empty()); | 231 SendResponse(error.empty()); |
220 } | 232 } |
221 | 233 |
222 } // namespace extensions | 234 } // namespace extensions |
| 235 |
| 236 #endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_ |
OLD | NEW |