1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/startup_helper.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/sandboxed_unpacker.h"
#include "chrome/browser/extensions/webstore_startup_installer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/chrome_extensions_client.h"
#include "components/crx_file/id_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/extension.h"
#include "ipc/ipc_message.h"
#if defined(OS_WIN)
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_util.h"
#endif
using content::BrowserThread;
namespace extensions {
namespace {
void PrintPackExtensionMessage(const std::string& message) {
VLOG(1) << message;
}
// On Windows, the jumplist action for installing an ephemeral app has to use
// the --install-ephemeral-app-from-webstore command line arg to initiate an
// install.
scoped_refptr<WebstoreStandaloneInstaller> CreateEphemeralAppInstaller(
Profile* profile,
const std::string& app_id,
WebstoreStandaloneInstaller::Callback callback) {
scoped_refptr<WebstoreStandaloneInstaller> installer;
#if defined(OS_WIN)
ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
DCHECK(registry);
if (!registry->GetExtensionById(app_id, ExtensionRegistry::EVERYTHING) ||
!util::IsEphemeralApp(app_id, profile)) {
return installer;
}
AppWindowRegistry* app_window_registry = AppWindowRegistry::Get(profile);
DCHECK(app_window_registry);
AppWindow* app_window =
app_window_registry->GetCurrentAppWindowForApp(app_id);
if (!app_window)
return installer;
installer = new WebstoreInstallWithPrompt(
app_id, profile, app_window->GetNativeWindow(), callback);
#endif
return installer;
}
} // namespace
StartupHelper::StartupHelper() : pack_job_succeeded_(false) {
ExtensionsClient::Set(ChromeExtensionsClient::GetInstance());
}
void StartupHelper::OnPackSuccess(
const base::FilePath& crx_path,
const base::FilePath& output_private_key_path) {
pack_job_succeeded_ = true;
PrintPackExtensionMessage(
base::UTF16ToUTF8(
PackExtensionJob::StandardSuccessMessage(crx_path,
output_private_key_path)));
}
void StartupHelper::OnPackFailure(const std::string& error_message,
ExtensionCreator::ErrorType type) {
PrintPackExtensionMessage(error_message);
}
bool StartupHelper::PackExtension(const base::CommandLine& cmd_line) {
if (!cmd_line.HasSwitch(switches::kPackExtension))
return false;
// Input Paths.
base::FilePath src_dir =
cmd_line.GetSwitchValuePath(switches::kPackExtension);
base::FilePath private_key_path;
if (cmd_line.HasSwitch(switches::kPackExtensionKey)) {
private_key_path = cmd_line.GetSwitchValuePath(switches::kPackExtensionKey);
}
// Launch a job to perform the packing on the file thread. Ignore warnings
// from the packing process. (e.g. Overwrite any existing crx file.)
pack_job_ = new PackExtensionJob(this, src_dir, private_key_path,
ExtensionCreator::kOverwriteCRX);
pack_job_->set_asynchronous(false);
pack_job_->Start();
return pack_job_succeeded_;
}
namespace {
class ValidateCrxHelper : public SandboxedUnpackerClient {
public:
ValidateCrxHelper(const base::FilePath& crx_file,
const base::FilePath& temp_dir,
base::RunLoop* run_loop)
: crx_file_(crx_file), temp_dir_(temp_dir), run_loop_(run_loop),
finished_(false), success_(false) {}
bool finished() { return finished_; }
bool success() { return success_; }
const base::string16& error() { return error_; }
void Start() {
BrowserThread::PostTask(BrowserThread::FILE,
FROM_HERE,
base::Bind(&ValidateCrxHelper::StartOnFileThread,
this));
}
protected:
~ValidateCrxHelper() override {}
void OnUnpackSuccess(const base::FilePath& temp_dir,
const base::FilePath& extension_root,
const base::DictionaryValue* original_manifest,
const Extension* extension,
const SkBitmap& install_icon) override {
finished_ = true;
success_ = true;
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&ValidateCrxHelper::FinishOnUIThread,
this));
}
void OnUnpackFailure(const base::string16& error) override {
finished_ = true;
success_ = false;
error_ = error;
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&ValidateCrxHelper::FinishOnUIThread,
this));
}
void FinishOnUIThread() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (run_loop_->running())
run_loop_->Quit();
}
void StartOnFileThread() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
scoped_refptr<base::MessageLoopProxy> file_thread_proxy =
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
scoped_refptr<SandboxedUnpacker> unpacker(
new SandboxedUnpacker(crx_file_,
Manifest::INTERNAL,
0, /* no special creation flags */
temp_dir_,
file_thread_proxy.get(),
this));
unpacker->Start();
}
// The file being validated.
const base::FilePath& crx_file_;
// The temporary directory where the sandboxed unpacker will do work.
const base::FilePath& temp_dir_;
// Unowned pointer to a runloop, so our consumer can wait for us to finish.
base::RunLoop* run_loop_;
// Whether we're finished unpacking;
bool finished_;
// Whether the unpacking was successful.
bool success_;
// If the unpacking wasn't successful, this contains an error message.
base::string16 error_;
};
} // namespace
bool StartupHelper::ValidateCrx(const base::CommandLine& cmd_line,
std::string* error) {
CHECK(error);
base::FilePath path = cmd_line.GetSwitchValuePath(switches::kValidateCrx);
if (path.empty()) {
*error = base::StringPrintf("Empty path passed for %s",
switches::kValidateCrx);
return false;
}
base::ScopedTempDir temp_dir;
if (!temp_dir.CreateUniqueTempDir()) {
*error = std::string("Failed to create temp dir");
return false;
}
base::RunLoop run_loop;
scoped_refptr<ValidateCrxHelper> helper(
new ValidateCrxHelper(path, temp_dir.path(), &run_loop));
helper->Start();
if (!helper->finished())
run_loop.Run();
bool success = helper->success();
if (!success)
*error = base::UTF16ToUTF8(helper->error());
return success;
}
namespace {
class AppInstallHelper {
public:
// A callback for when the install process is done.
typedef base::Callback<void()> DoneCallback;
AppInstallHelper();
virtual ~AppInstallHelper();
bool success() { return success_; }
const std::string& error() { return error_; }
void BeginInstall(Profile* profile,
const std::string& id,
bool show_prompt,
DoneCallback callback);
private:
WebstoreStandaloneInstaller::Callback Callback();
void OnAppInstallComplete(bool success,
const std::string& error,
webstore_install::Result result);
DoneCallback done_callback_;
// These hold on to the result of the app install when it is complete.
bool success_;
std::string error_;
scoped_refptr<WebstoreStandaloneInstaller> installer_;
};
AppInstallHelper::AppInstallHelper() : success_(false) {}
AppInstallHelper::~AppInstallHelper() {}
WebstoreStandaloneInstaller::Callback AppInstallHelper::Callback() {
return base::Bind(&AppInstallHelper::OnAppInstallComplete,
base::Unretained(this));
}
void AppInstallHelper::BeginInstall(
Profile* profile,
const std::string& id,
bool show_prompt,
DoneCallback done_callback) {
done_callback_ = done_callback;
WebstoreStandaloneInstaller::Callback callback =
base::Bind(&AppInstallHelper::OnAppInstallComplete,
base::Unretained(this));
installer_ = CreateEphemeralAppInstaller(profile, id, callback);
if (installer_.get()) {
installer_->BeginInstall();
} else {
error_ = "Not a supported ephemeral app installation.";
done_callback_.Run();
}
}
void AppInstallHelper::OnAppInstallComplete(bool success,
const std::string& error,
webstore_install::Result result) {
success_ = success;
error_= error;
done_callback_.Run();
}
} // namespace
bool StartupHelper::InstallEphemeralApp(const base::CommandLine& cmd_line,
Profile* profile) {
std::string id =
cmd_line.GetSwitchValueASCII(switches::kInstallEphemeralAppFromWebstore);
if (!crx_file::id_util::IdIsValid(id)) {
LOG(ERROR) << "Invalid id for "
<< switches::kInstallEphemeralAppFromWebstore << " : '" << id << "'";
return false;
}
AppInstallHelper helper;
base::RunLoop run_loop;
helper.BeginInstall(profile, id, true, run_loop.QuitClosure());
run_loop.Run();
if (!helper.success())
LOG(ERROR) << "InstallFromWebstore failed with error: " << helper.error();
return helper.success();
}
StartupHelper::~StartupHelper() {
if (pack_job_.get())
pack_job_->ClearClient();
}
} // namespace extensions
|