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