Chromium Code Reviews| 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" |
|
Nebojša Ćirić
2011/08/16 15:04:54
Do you need this now?
adriansc
2011/08/16 17:07:04
Done.
| |
| 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 MessageLoop::current())); | |
|
Nebojša Ćirić
2011/08/16 15:04:54
Do you need MessageLoop::current()?
adriansc
2011/08/16 17:07:04
Done.
| |
| 146 } else { | |
| 147 DidLoadAndLocalizeFile(success, data); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void ExecuteCodeInTabFunction::LocalizeCSS( | |
| 152 const std::string& data, | |
| 153 const std::string& extension_id, | |
| 154 const FilePath& extension_path, | |
| 155 const std::string& extension_default_locale, | |
| 156 MessageLoop* origin_loop) { | |
|
Nebojša Ćirić
2011/08/16 15:04:54
Remove origin_loop?
adriansc
2011/08/16 17:07:04
Done.
| |
| 157 scoped_ptr<SubstitutionMap> localization_messages( | |
| 158 extension_file_util::LoadExtensionMessageBundleSubstitutionMap( | |
| 159 extension_path, extension_id, extension_default_locale)); | |
| 160 | |
| 161 // We need to do message replacement on the data, so it has to be mutable. | |
| 162 std::string css_data = data; | |
| 163 std::string error; | |
| 164 ExtensionMessageBundle::ReplaceMessagesWithExternalDictionary( | |
| 165 *localization_messages, &css_data, &error); | |
| 166 | |
| 167 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter | |
| 168 // is always true, because if loading had failed, we wouldn't have had | |
| 169 // anything to localize. | |
| 170 origin_loop->PostTask( | |
|
Mihai Parparita -not on Chrome
2011/08/12 23:15:20
The pattern in other extension APIs to to use Brow
adriansc
2011/08/16 07:09:19
Done.
| |
| 171 FROM_HERE, | |
| 172 NewRunnableMethod(this, | |
| 173 &ExecuteCodeInTabFunction::DidLoadAndLocalizeFile, | |
| 174 true, css_data)); | |
| 175 } | |
| 176 | |
| 177 void ExecuteCodeInTabFunction::DidLoadAndLocalizeFile(bool success, | |
| 178 const std::string& data) { | |
| 127 if (success) { | 179 if (success) { |
| 128 Execute(data); | 180 Execute(data); |
| 129 } else { | 181 } else { |
| 130 #if defined(OS_POSIX) | 182 #if defined(OS_POSIX) |
| 131 // TODO(viettrungluu): bug: there's no particular reason the path should be | 183 // TODO(viettrungluu): bug: there's no particular reason the path should be |
| 132 // UTF-8, in which case this may fail. | 184 // UTF-8, in which case this may fail. |
| 133 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 185 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
| 134 resource_.relative_path().value()); | 186 resource_.relative_path().value()); |
| 135 #elif defined(OS_WIN) | 187 #elif defined(OS_WIN) |
| 136 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 188 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 if (!error.empty()) { | 262 if (!error.empty()) { |
| 211 CHECK(!success); | 263 CHECK(!success); |
| 212 error_ = error; | 264 error_ = error; |
| 213 } | 265 } |
| 214 | 266 |
| 215 SendResponse(success); | 267 SendResponse(success); |
| 216 | 268 |
| 217 Observe(NULL); | 269 Observe(NULL); |
| 218 Release(); // balanced in Execute() | 270 Release(); // balanced in Execute() |
| 219 } | 271 } |
| OLD | NEW |