| 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/bind.h" |
| 7 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_tab_util.h" | 11 #include "chrome/browser/extensions/extension_tab_util.h" |
| 11 #include "chrome/browser/extensions/extension_tabs_module.h" | 12 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 12 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 13 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
| 13 #include "chrome/browser/extensions/file_reader.h" | 14 #include "chrome/browser/extensions/file_reader.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 if (!script_info->GetString(keys::kFileKey, &relative_path)) | 112 if (!script_info->GetString(keys::kFileKey, &relative_path)) |
| 112 return false; | 113 return false; |
| 113 resource_ = GetExtension()->GetResource(relative_path); | 114 resource_ = GetExtension()->GetResource(relative_path); |
| 114 } | 115 } |
| 115 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { | 116 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { |
| 116 error_ = keys::kNoCodeOrFileToExecuteError; | 117 error_ = keys::kNoCodeOrFileToExecuteError; |
| 117 return false; | 118 return false; |
| 118 } | 119 } |
| 119 | 120 |
| 120 scoped_refptr<FileReader> file_reader(new FileReader( | 121 scoped_refptr<FileReader> file_reader(new FileReader( |
| 121 resource_, NewCallback(this, &ExecuteCodeInTabFunction::DidLoadFile))); | 122 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); |
| 122 file_reader->Start(); | 123 file_reader->Start(); |
| 123 AddRef(); // Keep us alive until DidLoadAndLocalizeFile is called. | |
| 124 | 124 |
| 125 return true; | 125 return true; |
| 126 } | 126 } |
| 127 | 127 |
| 128 void ExecuteCodeInTabFunction::DidLoadFile(bool success, | 128 void ExecuteCodeInTabFunction::DidLoadFile(bool success, |
| 129 const std::string& data) { | 129 const std::string& data) { |
| 130 std::string function_name = name(); | 130 std::string function_name = name(); |
| 131 const Extension* extension = GetExtension(); | 131 const Extension* extension = GetExtension(); |
| 132 | 132 |
| 133 // Check if the file is CSS and needs localization. | 133 // Check if the file is CSS and needs localization. |
| 134 if (success && | 134 if (success && |
| 135 function_name == TabsInsertCSSFunction::function_name() && | 135 function_name == TabsInsertCSSFunction::function_name() && |
| 136 extension != NULL && | 136 extension != NULL && |
| 137 data.find(ExtensionMessageBundle::kMessageBegin) != std::string::npos) { | 137 data.find(ExtensionMessageBundle::kMessageBegin) != std::string::npos) { |
| 138 BrowserThread::PostTask( | 138 BrowserThread::PostTask( |
| 139 BrowserThread::FILE, FROM_HERE, | 139 BrowserThread::FILE, FROM_HERE, |
| 140 NewRunnableMethod(this, &ExecuteCodeInTabFunction::LocalizeCSS, | 140 base::Bind(&ExecuteCodeInTabFunction::LocalizeCSS, this, |
| 141 data, | 141 data, |
| 142 extension->id(), | 142 extension->id(), |
| 143 extension->path(), | 143 extension->path(), |
| 144 extension->default_locale())); | 144 extension->default_locale())); |
| 145 } else { | 145 } else { |
| 146 DidLoadAndLocalizeFile(success, data); | 146 DidLoadAndLocalizeFile(success, data); |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 | 149 |
| 150 void ExecuteCodeInTabFunction::LocalizeCSS( | 150 void ExecuteCodeInTabFunction::LocalizeCSS( |
| 151 const std::string& data, | 151 const std::string& data, |
| 152 const std::string& extension_id, | 152 const std::string& extension_id, |
| 153 const FilePath& extension_path, | 153 const FilePath& extension_path, |
| 154 const std::string& extension_default_locale) { | 154 const std::string& extension_default_locale) { |
| 155 scoped_ptr<SubstitutionMap> localization_messages( | 155 scoped_ptr<SubstitutionMap> localization_messages( |
| 156 extension_file_util::LoadExtensionMessageBundleSubstitutionMap( | 156 extension_file_util::LoadExtensionMessageBundleSubstitutionMap( |
| 157 extension_path, extension_id, extension_default_locale)); | 157 extension_path, extension_id, extension_default_locale)); |
| 158 | 158 |
| 159 // We need to do message replacement on the data, so it has to be mutable. | 159 // We need to do message replacement on the data, so it has to be mutable. |
| 160 std::string css_data = data; | 160 std::string css_data = data; |
| 161 std::string error; | 161 std::string error; |
| 162 ExtensionMessageBundle::ReplaceMessagesWithExternalDictionary( | 162 ExtensionMessageBundle::ReplaceMessagesWithExternalDictionary( |
| 163 *localization_messages, &css_data, &error); | 163 *localization_messages, &css_data, &error); |
| 164 | 164 |
| 165 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter | 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 | 166 // is always true, because if loading had failed, we wouldn't have had |
| 167 // anything to localize. | 167 // anything to localize. |
| 168 BrowserThread::PostTask( | 168 BrowserThread::PostTask( |
| 169 BrowserThread::UI, FROM_HERE, | 169 BrowserThread::UI, FROM_HERE, |
| 170 NewRunnableMethod(this, | 170 base::Bind(&ExecuteCodeInTabFunction::DidLoadAndLocalizeFile, this, |
| 171 &ExecuteCodeInTabFunction::DidLoadAndLocalizeFile, | 171 true, css_data)); |
| 172 true, css_data)); | |
| 173 } | 172 } |
| 174 | 173 |
| 175 void ExecuteCodeInTabFunction::DidLoadAndLocalizeFile(bool success, | 174 void ExecuteCodeInTabFunction::DidLoadAndLocalizeFile(bool success, |
| 176 const std::string& data) { | 175 const std::string& data) { |
| 177 if (success) { | 176 if (success) { |
| 178 Execute(data); | 177 Execute(data); |
| 179 } else { | 178 } else { |
| 180 #if defined(OS_POSIX) | 179 #if defined(OS_POSIX) |
| 181 // 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 |
| 182 // UTF-8, in which case this may fail. | 181 // UTF-8, in which case this may fail. |
| 183 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 182 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
| 184 resource_.relative_path().value()); | 183 resource_.relative_path().value()); |
| 185 #elif defined(OS_WIN) | 184 #elif defined(OS_WIN) |
| 186 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 185 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
| 187 WideToUTF8(resource_.relative_path().value())); | 186 WideToUTF8(resource_.relative_path().value())); |
| 188 #endif // OS_WIN | 187 #endif // OS_WIN |
| 189 SendResponse(false); | 188 SendResponse(false); |
| 190 } | 189 } |
| 191 Release(); // Balance the AddRef taken in RunImpl | |
| 192 } | 190 } |
| 193 | 191 |
| 194 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) { | 192 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) { |
| 195 TabContentsWrapper* contents = NULL; | 193 TabContentsWrapper* contents = NULL; |
| 196 Browser* browser = NULL; | 194 Browser* browser = NULL; |
| 197 | 195 |
| 198 bool success = ExtensionTabUtil::GetTabById( | 196 bool success = ExtensionTabUtil::GetTabById( |
| 199 execute_tab_id_, profile(), include_incognito(), &browser, NULL, | 197 execute_tab_id_, profile(), include_incognito(), &browser, NULL, |
| 200 &contents, NULL) && contents && browser; | 198 &contents, NULL) && contents && browser; |
| 201 | 199 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 if (!error.empty()) { | 258 if (!error.empty()) { |
| 261 CHECK(!success); | 259 CHECK(!success); |
| 262 error_ = error; | 260 error_ = error; |
| 263 } | 261 } |
| 264 | 262 |
| 265 SendResponse(success); | 263 SendResponse(success); |
| 266 | 264 |
| 267 Observe(NULL); | 265 Observe(NULL); |
| 268 Release(); // balanced in Execute() | 266 Release(); // balanced in Execute() |
| 269 } | 267 } |
| OLD | NEW |