| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/startup_helper.h" | 5 #include "chrome/browser/extensions/startup_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" |
| 10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/run_loop.h" |
| 11 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/stringprintf.h" |
| 12 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/extensions/sandboxed_unpacker.h" |
| 14 #include "chrome/browser/extensions/webstore_startup_installer.h" | 18 #include "chrome/browser/extensions/webstore_startup_installer.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" | 21 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" |
| 18 #include "chrome/common/extensions/background_info.h" | 22 #include "chrome/common/extensions/background_info.h" |
| 19 #include "chrome/common/extensions/extension.h" | 23 #include "chrome/common/extensions/extension.h" |
| 24 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 21 #include "ipc/ipc_message.h" | 26 #include "ipc/ipc_message.h" |
| 22 | 27 |
| 28 using content::BrowserThread; |
| 29 |
| 23 namespace { | 30 namespace { |
| 24 | 31 |
| 25 void PrintPackExtensionMessage(const std::string& message) { | 32 void PrintPackExtensionMessage(const std::string& message) { |
| 26 printf("%s\n", message.c_str()); | 33 printf("%s\n", message.c_str()); |
| 27 } | 34 } |
| 28 | 35 |
| 29 } // namespace | 36 } // namespace |
| 30 | 37 |
| 31 namespace extensions { | 38 namespace extensions { |
| 32 | 39 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 // Launch a job to perform the packing on the file thread. Ignore warnings | 72 // Launch a job to perform the packing on the file thread. Ignore warnings |
| 66 // from the packing process. (e.g. Overwrite any existing crx file.) | 73 // from the packing process. (e.g. Overwrite any existing crx file.) |
| 67 pack_job_ = new PackExtensionJob(this, src_dir, private_key_path, | 74 pack_job_ = new PackExtensionJob(this, src_dir, private_key_path, |
| 68 ExtensionCreator::kOverwriteCRX); | 75 ExtensionCreator::kOverwriteCRX); |
| 69 pack_job_->set_asynchronous(false); | 76 pack_job_->set_asynchronous(false); |
| 70 pack_job_->Start(); | 77 pack_job_->Start(); |
| 71 | 78 |
| 72 return pack_job_succeeded_; | 79 return pack_job_succeeded_; |
| 73 } | 80 } |
| 74 | 81 |
| 82 namespace { |
| 83 |
| 84 class ValidateCrxHelper : public SandboxedUnpackerClient { |
| 85 public: |
| 86 ValidateCrxHelper(const base::FilePath& crx_file, |
| 87 const base::FilePath& temp_dir, |
| 88 base::RunLoop* run_loop) |
| 89 : crx_file_(crx_file), temp_dir_(temp_dir), run_loop_(run_loop), |
| 90 finished_(false), success_(false) {} |
| 91 |
| 92 bool finished() { return finished_; } |
| 93 bool success() { return success_; } |
| 94 const string16& error() { return error_; } |
| 95 |
| 96 void Start() { |
| 97 BrowserThread::PostTask(BrowserThread::FILE, |
| 98 FROM_HERE, |
| 99 base::Bind(&ValidateCrxHelper::StartOnFileThread, |
| 100 this)); |
| 101 } |
| 102 |
| 103 protected: |
| 104 virtual ~ValidateCrxHelper() {} |
| 105 |
| 106 virtual void OnUnpackSuccess(const base::FilePath& temp_dir, |
| 107 const base::FilePath& extension_root, |
| 108 const base::DictionaryValue* original_manifest, |
| 109 const Extension* extension) { |
| 110 finished_ = true; |
| 111 success_ = true; |
| 112 BrowserThread::PostTask(BrowserThread::UI, |
| 113 FROM_HERE, |
| 114 base::Bind(&ValidateCrxHelper::FinishOnUIThread, |
| 115 this)); |
| 116 } |
| 117 |
| 118 virtual void OnUnpackFailure(const string16& error) { |
| 119 finished_ = true; |
| 120 success_ = false; |
| 121 error_ = error; |
| 122 BrowserThread::PostTask(BrowserThread::UI, |
| 123 FROM_HERE, |
| 124 base::Bind(&ValidateCrxHelper::FinishOnUIThread, |
| 125 this)); |
| 126 } |
| 127 |
| 128 void FinishOnUIThread() { |
| 129 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 130 if (run_loop_->running()) |
| 131 run_loop_->Quit(); |
| 132 } |
| 133 |
| 134 void StartOnFileThread() { |
| 135 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 136 scoped_refptr<base::MessageLoopProxy> file_thread_proxy = |
| 137 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); |
| 138 |
| 139 scoped_refptr<SandboxedUnpacker> unpacker( |
| 140 new SandboxedUnpacker(crx_file_, |
| 141 true /* out of process */, |
| 142 Manifest::INTERNAL, |
| 143 0, /* no special creation flags */ |
| 144 temp_dir_, |
| 145 file_thread_proxy, |
| 146 this)); |
| 147 unpacker->Start(); |
| 148 } |
| 149 |
| 150 // The file being validated. |
| 151 const base::FilePath& crx_file_; |
| 152 |
| 153 // The temporary directory where the sandboxed unpacker will do work. |
| 154 const base::FilePath& temp_dir_; |
| 155 |
| 156 // Unowned pointer to a runloop, so our consumer can wait for us to finish. |
| 157 base::RunLoop* run_loop_; |
| 158 |
| 159 // Whether we're finished unpacking; |
| 160 bool finished_; |
| 161 |
| 162 // Whether the unpacking was successful. |
| 163 bool success_; |
| 164 |
| 165 // If the unpacking wasn't successful, this contains an error message. |
| 166 string16 error_; |
| 167 }; |
| 168 |
| 169 } // namespace |
| 170 |
| 171 bool StartupHelper::ValidateCrx(const CommandLine& cmd_line, |
| 172 std::string* error) { |
| 173 CHECK(error); |
| 174 base::FilePath path = cmd_line.GetSwitchValuePath(switches::kValidateCrx); |
| 175 if (path.empty()) { |
| 176 *error = base::StringPrintf("Empty path passed for %s", |
| 177 switches::kValidateCrx); |
| 178 return false; |
| 179 } |
| 180 base::ScopedTempDir temp_dir; |
| 181 |
| 182 if (!temp_dir.CreateUniqueTempDir()) { |
| 183 *error = std::string("Failed to create temp dir"); |
| 184 return false; |
| 185 } |
| 186 |
| 187 base::RunLoop run_loop; |
| 188 scoped_refptr<ValidateCrxHelper> helper( |
| 189 new ValidateCrxHelper(path, temp_dir.path(), &run_loop)); |
| 190 helper->Start(); |
| 191 if (!helper->finished()) |
| 192 run_loop.Run(); |
| 193 |
| 194 bool success = helper->success(); |
| 195 if (!success) |
| 196 *error = UTF16ToUTF8(helper->error()); |
| 197 return success; |
| 198 } |
| 199 |
| 75 bool StartupHelper::UninstallExtension(const CommandLine& cmd_line, | 200 bool StartupHelper::UninstallExtension(const CommandLine& cmd_line, |
| 76 Profile* profile) { | 201 Profile* profile) { |
| 77 DCHECK(profile); | 202 DCHECK(profile); |
| 78 | 203 |
| 79 if (!cmd_line.HasSwitch(switches::kUninstallExtension)) | 204 if (!cmd_line.HasSwitch(switches::kUninstallExtension)) |
| 80 return false; | 205 return false; |
| 81 | 206 |
| 82 ExtensionService* extension_service = profile->GetExtensionService(); | 207 ExtensionService* extension_service = profile->GetExtensionService(); |
| 83 if (!extension_service) | 208 if (!extension_service) |
| 84 return false; | 209 return false; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 } | 334 } |
| 210 return id; | 335 return id; |
| 211 } | 336 } |
| 212 | 337 |
| 213 StartupHelper::~StartupHelper() { | 338 StartupHelper::~StartupHelper() { |
| 214 if (pack_job_.get()) | 339 if (pack_job_.get()) |
| 215 pack_job_->ClearClient(); | 340 pack_job_->ClearClient(); |
| 216 } | 341 } |
| 217 | 342 |
| 218 } // namespace extensions | 343 } // namespace extensions |
| OLD | NEW |