OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/execute_code_in_tab_function.h" | 5 #include "chrome/browser/extensions/execute_code_in_tab_function.h" |
6 | 6 |
7 #include "base/callback.h" | |
8 #include "base/string_util.h" | 7 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/extension_tabs_module.h" | 10 #include "chrome/browser/extensions/extension_tabs_module.h" |
12 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 11 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
13 #include "chrome/browser/extensions/file_reader.h" | 12 #include "chrome/browser/extensions/file_reader.h" |
14 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 15 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
17 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
18 #include "chrome/common/extensions/extension_constants.h" | 17 #include "chrome/common/extensions/extension_constants.h" |
19 #include "chrome/common/extensions/extension_error_utils.h" | 18 #include "chrome/common/extensions/extension_error_utils.h" |
| 19 #include "chrome/common/extensions/extension_file_util.h" |
| 20 #include "chrome/common/extensions/extension_l10n_util.h" |
20 #include "chrome/common/extensions/extension_messages.h" | 21 #include "chrome/common/extensions/extension_messages.h" |
| 22 #include "chrome/common/extensions/extension_message_bundle.h" |
21 #include "content/browser/renderer_host/render_view_host.h" | 23 #include "content/browser/renderer_host/render_view_host.h" |
22 #include "content/browser/tab_contents/tab_contents.h" | 24 #include "content/browser/tab_contents/tab_contents.h" |
23 #include "content/browser/renderer_host/render_view_host.h" | 25 #include "content/browser/renderer_host/render_view_host.h" |
24 #include "content/common/notification_service.h" | 26 #include "content/common/notification_service.h" |
25 | 27 |
26 namespace keys = extension_tabs_module_constants; | 28 namespace keys = extension_tabs_module_constants; |
27 | 29 |
28 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() | 30 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() |
29 : execute_tab_id_(-1), | 31 : execute_tab_id_(-1), |
30 all_frames_(false) { | 32 all_frames_(false) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 resource_ = GetExtension()->GetResource(relative_path); | 112 resource_ = GetExtension()->GetResource(relative_path); |
111 } | 113 } |
112 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { | 114 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { |
113 error_ = keys::kNoCodeOrFileToExecuteError; | 115 error_ = keys::kNoCodeOrFileToExecuteError; |
114 return false; | 116 return false; |
115 } | 117 } |
116 | 118 |
117 scoped_refptr<FileReader> file_reader(new FileReader( | 119 scoped_refptr<FileReader> file_reader(new FileReader( |
118 resource_, NewCallback(this, &ExecuteCodeInTabFunction::DidLoadFile))); | 120 resource_, NewCallback(this, &ExecuteCodeInTabFunction::DidLoadFile))); |
119 file_reader->Start(); | 121 file_reader->Start(); |
120 AddRef(); // Keep us alive until DidLoadFile is called. | 122 AddRef(); // Keep us alive until DidLoadAndLocalizeFile is called. |
121 | 123 |
122 return true; | 124 return true; |
123 } | 125 } |
124 | 126 |
125 void ExecuteCodeInTabFunction::DidLoadFile(bool success, | 127 void ExecuteCodeInTabFunction::DidLoadFile(bool success, |
126 const std::string& data) { | 128 const std::string& data) { |
| 129 std::string function_name = name(); |
| 130 const Extension* extension = GetExtension(); |
| 131 |
| 132 // Check if the file is CSS and needs localization. |
| 133 if (success && |
| 134 function_name == TabsInsertCSSFunction::function_name() && |
| 135 extension != NULL && |
| 136 data.find(ExtensionMessageBundle::kMessageBegin) != std::string::npos) { |
| 137 BrowserThread::PostTask( |
| 138 BrowserThread::FILE, FROM_HERE, |
| 139 NewRunnableMethod(this, &ExecuteCodeInTabFunction::LocalizeCSS, |
| 140 data, |
| 141 extension->id(), |
| 142 extension->path(), |
| 143 extension->default_locale())); |
| 144 } else { |
| 145 DidLoadAndLocalizeFile(success, data); |
| 146 } |
| 147 } |
| 148 |
| 149 void ExecuteCodeInTabFunction::LocalizeCSS( |
| 150 const std::string& data, |
| 151 const std::string& extension_id, |
| 152 const FilePath& extension_path, |
| 153 const std::string& extension_default_locale) { |
| 154 scoped_ptr<SubstitutionMap> localization_messages( |
| 155 extension_file_util::LoadExtensionMessageBundleSubstitutionMap( |
| 156 extension_path, extension_id, extension_default_locale)); |
| 157 |
| 158 // We need to do message replacement on the data, so it has to be mutable. |
| 159 std::string css_data = data; |
| 160 std::string error; |
| 161 ExtensionMessageBundle::ReplaceMessagesWithExternalDictionary( |
| 162 *localization_messages, &css_data, &error); |
| 163 |
| 164 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter |
| 165 // is always true, because if loading had failed, we wouldn't have had |
| 166 // anything to localize. |
| 167 BrowserThread::PostTask( |
| 168 BrowserThread::UI, FROM_HERE, |
| 169 NewRunnableMethod(this, |
| 170 &ExecuteCodeInTabFunction::DidLoadAndLocalizeFile, |
| 171 true, css_data)); |
| 172 } |
| 173 |
| 174 void ExecuteCodeInTabFunction::DidLoadAndLocalizeFile(bool success, |
| 175 const std::string& data) { |
127 if (success) { | 176 if (success) { |
128 Execute(data); | 177 Execute(data); |
129 } else { | 178 } else { |
130 #if defined(OS_POSIX) | 179 #if defined(OS_POSIX) |
131 // TODO(viettrungluu): bug: there's no particular reason the path should be | 180 // TODO(viettrungluu): bug: there's no particular reason the path should be |
132 // UTF-8, in which case this may fail. | 181 // UTF-8, in which case this may fail. |
133 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 182 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
134 resource_.relative_path().value()); | 183 resource_.relative_path().value()); |
135 #elif defined(OS_WIN) | 184 #elif defined(OS_WIN) |
136 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 185 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 if (!error.empty()) { | 259 if (!error.empty()) { |
211 CHECK(!success); | 260 CHECK(!success); |
212 error_ = error; | 261 error_ = error; |
213 } | 262 } |
214 | 263 |
215 SendResponse(success); | 264 SendResponse(success); |
216 | 265 |
217 Observe(NULL); | 266 Observe(NULL); |
218 Release(); // balanced in Execute() | 267 Release(); // balanced in Execute() |
219 } | 268 } |
OLD | NEW |