Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / extensions / startup_helper.cc
blob49068c4aa4ebbf7571802f50a3cfc37f95ad7aea
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/startup_helper.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/extensions/chrome_extensions_client.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "extensions/browser/sandboxed_unpacker.h"
20 #include "extensions/common/extension.h"
22 using content::BrowserThread;
24 namespace extensions {
26 namespace {
28 void PrintPackExtensionMessage(const std::string& message) {
29 VLOG(1) << message;
32 } // namespace
34 StartupHelper::StartupHelper() : pack_job_succeeded_(false) {
35 ExtensionsClient::Set(ChromeExtensionsClient::GetInstance());
38 void StartupHelper::OnPackSuccess(
39 const base::FilePath& crx_path,
40 const base::FilePath& output_private_key_path) {
41 pack_job_succeeded_ = true;
42 PrintPackExtensionMessage(
43 base::UTF16ToUTF8(
44 PackExtensionJob::StandardSuccessMessage(crx_path,
45 output_private_key_path)));
48 void StartupHelper::OnPackFailure(const std::string& error_message,
49 ExtensionCreator::ErrorType type) {
50 PrintPackExtensionMessage(error_message);
53 bool StartupHelper::PackExtension(const base::CommandLine& cmd_line) {
54 if (!cmd_line.HasSwitch(switches::kPackExtension))
55 return false;
57 // Input Paths.
58 base::FilePath src_dir =
59 cmd_line.GetSwitchValuePath(switches::kPackExtension);
60 base::FilePath private_key_path;
61 if (cmd_line.HasSwitch(switches::kPackExtensionKey)) {
62 private_key_path = cmd_line.GetSwitchValuePath(switches::kPackExtensionKey);
65 // 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.)
67 pack_job_ = new PackExtensionJob(this, src_dir, private_key_path,
68 ExtensionCreator::kOverwriteCRX);
69 pack_job_->set_asynchronous(false);
70 pack_job_->Start();
72 return pack_job_succeeded_;
75 namespace {
77 class ValidateCrxHelper : public SandboxedUnpackerClient {
78 public:
79 ValidateCrxHelper(const CRXFileInfo& file,
80 const base::FilePath& temp_dir,
81 base::RunLoop* run_loop)
82 : crx_file_(file),
83 temp_dir_(temp_dir),
84 run_loop_(run_loop),
85 finished_(false),
86 success_(false) {}
88 bool finished() { return finished_; }
89 bool success() { return success_; }
90 const base::string16& error() { return error_; }
92 void Start() {
93 BrowserThread::PostTask(BrowserThread::FILE,
94 FROM_HERE,
95 base::Bind(&ValidateCrxHelper::StartOnFileThread,
96 this));
99 protected:
100 ~ValidateCrxHelper() override {}
102 void OnUnpackSuccess(const base::FilePath& temp_dir,
103 const base::FilePath& extension_root,
104 const base::DictionaryValue* original_manifest,
105 const Extension* extension,
106 const SkBitmap& install_icon) override {
107 finished_ = true;
108 success_ = true;
109 BrowserThread::PostTask(BrowserThread::UI,
110 FROM_HERE,
111 base::Bind(&ValidateCrxHelper::FinishOnUIThread,
112 this));
115 void OnUnpackFailure(const CrxInstallError& error) override {
116 finished_ = true;
117 success_ = false;
118 error_ = error.message();
119 BrowserThread::PostTask(BrowserThread::UI,
120 FROM_HERE,
121 base::Bind(&ValidateCrxHelper::FinishOnUIThread,
122 this));
125 void FinishOnUIThread() {
126 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
127 if (run_loop_->running())
128 run_loop_->Quit();
131 void StartOnFileThread() {
132 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
133 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner =
134 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
136 scoped_refptr<SandboxedUnpacker> unpacker(
137 new SandboxedUnpacker(crx_file_,
138 Manifest::INTERNAL,
139 0, /* no special creation flags */
140 temp_dir_,
141 file_task_runner.get(),
142 this));
143 unpacker->Start();
146 // The file being validated.
147 const CRXFileInfo& crx_file_;
149 // The temporary directory where the sandboxed unpacker will do work.
150 const base::FilePath& temp_dir_;
152 // Unowned pointer to a runloop, so our consumer can wait for us to finish.
153 base::RunLoop* run_loop_;
155 // Whether we're finished unpacking;
156 bool finished_;
158 // Whether the unpacking was successful.
159 bool success_;
161 // If the unpacking wasn't successful, this contains an error message.
162 base::string16 error_;
165 } // namespace
167 bool StartupHelper::ValidateCrx(const base::CommandLine& cmd_line,
168 std::string* error) {
169 CHECK(error);
170 base::FilePath path = cmd_line.GetSwitchValuePath(switches::kValidateCrx);
171 if (path.empty()) {
172 *error = base::StringPrintf("Empty path passed for %s",
173 switches::kValidateCrx);
174 return false;
176 base::ScopedTempDir temp_dir;
178 if (!temp_dir.CreateUniqueTempDir()) {
179 *error = std::string("Failed to create temp dir");
180 return false;
183 base::RunLoop run_loop;
184 CRXFileInfo file(path);
185 scoped_refptr<ValidateCrxHelper> helper(
186 new ValidateCrxHelper(file, temp_dir.path(), &run_loop));
187 helper->Start();
188 if (!helper->finished())
189 run_loop.Run();
191 bool success = helper->success();
192 if (!success)
193 *error = base::UTF16ToUTF8(helper->error());
194 return success;
197 StartupHelper::~StartupHelper() {
198 if (pack_job_.get())
199 pack_job_->ClearClient();
202 } // namespace extensions