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