Chromium Code Reviews| 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" |
| 12 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/sandboxed_unpacker.h" | |
| 14 #include "chrome/browser/extensions/webstore_startup_installer.h" | 17 #include "chrome/browser/extensions/webstore_startup_installer.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" | 20 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" |
| 18 #include "chrome/common/extensions/background_info.h" | 21 #include "chrome/common/extensions/background_info.h" |
| 19 #include "chrome/common/extensions/extension.h" | 22 #include "chrome/common/extensions/extension.h" |
| 23 #include "content/public/browser/browser_thread.h" | |
| 20 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 21 #include "ipc/ipc_message.h" | 25 #include "ipc/ipc_message.h" |
| 22 | 26 |
| 27 using content::BrowserThread; | |
| 28 | |
| 23 namespace { | 29 namespace { |
| 24 | 30 |
| 25 void PrintPackExtensionMessage(const std::string& message) { | 31 void PrintPackExtensionMessage(const std::string& message) { |
| 26 printf("%s\n", message.c_str()); | 32 printf("%s\n", message.c_str()); |
| 27 } | 33 } |
| 28 | 34 |
| 29 } // namespace | 35 } // namespace |
| 30 | 36 |
| 31 namespace extensions { | 37 namespace extensions { |
| 32 | 38 |
| (...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 | 71 // 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.) | 72 // from the packing process. (e.g. Overwrite any existing crx file.) |
| 67 pack_job_ = new PackExtensionJob(this, src_dir, private_key_path, | 73 pack_job_ = new PackExtensionJob(this, src_dir, private_key_path, |
| 68 ExtensionCreator::kOverwriteCRX); | 74 ExtensionCreator::kOverwriteCRX); |
| 69 pack_job_->set_asynchronous(false); | 75 pack_job_->set_asynchronous(false); |
| 70 pack_job_->Start(); | 76 pack_job_->Start(); |
| 71 | 77 |
| 72 return pack_job_succeeded_; | 78 return pack_job_succeeded_; |
| 73 } | 79 } |
| 74 | 80 |
| 81 namespace { | |
| 82 | |
| 83 class ValidateCrxHelper : public SandboxedUnpackerClient { | |
| 84 public: | |
| 85 ValidateCrxHelper(const base::FilePath& crx_file, | |
| 86 const base::FilePath& temp_dir, | |
| 87 base::RunLoop* run_loop) | |
| 88 : crx_file_(crx_file), temp_dir_(temp_dir), run_loop_(run_loop), | |
| 89 finished_(false), success_(false) {} | |
| 90 | |
| 91 bool finished() { return finished_; } | |
| 92 bool success() { return success_; } | |
| 93 const string16& error() { return error_; } | |
| 94 | |
| 95 void Start() { | |
| 96 BrowserThread::PostTask(BrowserThread::FILE, | |
| 97 FROM_HERE, | |
| 98 base::Bind(&ValidateCrxHelper::StartOnFileThread, | |
| 99 this)); | |
| 100 } | |
| 101 | |
| 102 protected: | |
| 103 virtual ~ValidateCrxHelper() {} | |
| 104 | |
| 105 virtual void OnUnpackSuccess(const base::FilePath& temp_dir, | |
| 106 const base::FilePath& extension_root, | |
| 107 const base::DictionaryValue* original_manifest, | |
| 108 const Extension* extension) { | |
| 109 finished_ = true; | |
| 110 success_ = true; | |
| 111 BrowserThread::PostTask(BrowserThread::UI, | |
| 112 FROM_HERE, | |
| 113 base::Bind(&ValidateCrxHelper::FinishOnUIThread, | |
| 114 this)); | |
| 115 } | |
| 116 | |
| 117 virtual void OnUnpackFailure(const string16& error) { | |
| 118 finished_ = true; | |
| 119 success_ = false; | |
| 120 error_ = error; | |
| 121 BrowserThread::PostTask(BrowserThread::UI, | |
| 122 FROM_HERE, | |
| 123 base::Bind(&ValidateCrxHelper::FinishOnUIThread, | |
| 124 this)); | |
| 125 } | |
| 126 | |
| 127 void FinishOnUIThread() { | |
| 128 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 129 if (run_loop_->running()) | |
| 130 run_loop_->Quit(); | |
| 131 } | |
| 132 | |
| 133 void StartOnFileThread() { | |
| 134 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 135 scoped_refptr<base::MessageLoopProxy> file_thread_proxy = | |
| 136 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); | |
| 137 | |
| 138 scoped_refptr<SandboxedUnpacker> unpacker( | |
| 139 new SandboxedUnpacker(crx_file_, | |
| 140 true /* out of process */, | |
| 141 Manifest::INTERNAL, | |
| 142 0, /* no special creation flags */ | |
| 143 temp_dir_, | |
| 144 file_thread_proxy, | |
| 145 this)); | |
| 146 unpacker->Start(); | |
| 147 } | |
| 148 | |
| 149 // The file being validated. | |
| 150 const base::FilePath& crx_file_; | |
| 151 | |
| 152 // The temporary directory where the sandboxed unpacker will do work. | |
| 153 const base::FilePath& temp_dir_; | |
| 154 | |
| 155 // Unowned pointer to a runloop, so our consumer can wait for us to finish. | |
| 156 base::RunLoop* run_loop_; | |
| 157 | |
| 158 // Whether we're finished unpacking; | |
| 159 bool finished_; | |
| 160 | |
| 161 // Whether the unpacking was successful. | |
| 162 bool success_; | |
| 163 | |
| 164 // If the unpacking wasn't successful, this contains an error message. | |
| 165 string16 error_; | |
| 166 }; | |
| 167 | |
| 168 } // namespace | |
| 169 | |
| 170 bool StartupHelper::ValidateCrx(const CommandLine& cmd_line) { | |
| 171 base::FilePath path = cmd_line.GetSwitchValuePath(switches::kValidateCrx); | |
| 172 if (path.empty()) { | |
| 173 LOG(ERROR) << "Empty path passed for " << switches::kValidateCrx; | |
| 174 return false; | |
| 175 } | |
| 176 base::ScopedTempDir temp_dir; | |
| 177 | |
| 178 if (!temp_dir.CreateUniqueTempDir()) { | |
| 179 LOG(ERROR) << "Failed to create temp dir"; | |
| 180 return false; | |
| 181 } | |
| 182 | |
| 183 base::RunLoop run_loop; | |
| 184 scoped_refptr<ValidateCrxHelper> helper( | |
| 185 new ValidateCrxHelper(path, temp_dir.path(), &run_loop)); | |
| 186 helper->Start(); | |
| 187 if (!helper->finished()) | |
| 188 run_loop.Run(); | |
| 189 | |
| 190 bool success = helper->success(); | |
| 191 if (success) | |
|
asargent_no_longer_on_chrome
2013/03/20 17:43:47
Just using LOG(ERROR) here to report both success
Matt Perry
2013/03/20 19:52:15
This is intended to be used as an automated tool b
asargent_no_longer_on_chrome
2013/03/21 21:34:44
As we discussed offline, I've switched this to jus
| |
| 192 LOG(ERROR) << "ValidateCrx Success: '" << path.LossyDisplayName() | |
| 193 << "' is valid"; | |
| 194 else | |
| 195 LOG(ERROR) << "ValidateCrx Error: '" << path.LossyDisplayName() | |
| 196 << "' is invalid. Details: '" << 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 |