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
|
// 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/message_loop.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/webstore_standalone_installer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/api/i18n/default_locale_handler.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_manifest_constants.h"
#include "chrome/common/extensions/manifest_handler.h"
#include "content/public/browser/web_contents.h"
#include "ipc/ipc_message.h"
namespace {
void PrintPackExtensionMessage(const std::string& message) {
printf("%s\n", message.c_str());
}
} // namespace
namespace extensions {
StartupHelper::StartupHelper() : pack_job_succeeded_(false) {
ManifestHandler::Register(extension_manifest_keys::kDefaultLocale,
make_linked_ptr(new DefaultLocaleHandler));
}
void StartupHelper::OnPackSuccess(
const base::FilePath& crx_path,
const base::FilePath& output_private_key_path) {
pack_job_succeeded_ = true;
PrintPackExtensionMessage(
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 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_;
}
bool StartupHelper::UninstallExtension(const CommandLine& cmd_line,
Profile* profile) {
DCHECK(profile);
if (!cmd_line.HasSwitch(switches::kUninstallExtension))
return false;
ExtensionService* extension_service = profile->GetExtensionService();
if (!extension_service)
return false;
std::string extension_id = cmd_line.GetSwitchValueASCII(
switches::kUninstallExtension);
return ExtensionService::UninstallExtensionHelper(extension_service,
extension_id);
}
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,
WebstoreStandaloneInstaller::PromptType prompt_type,
DoneCallback callback);
private:
WebstoreStandaloneInstaller::Callback Callback();
void OnAppInstallComplete(bool success, const std::string& error);
DoneCallback done_callback_;
// These hold on to the result of the app install when it is complete.
bool success_;
std::string error_;
scoped_ptr<content::WebContents> web_contents_;
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,
WebstoreStandaloneInstaller::PromptType prompt_type,
DoneCallback done_callback) {
done_callback_ = done_callback;
// TODO(asargent) - it would be nice not to need a WebContents just to
// use the standalone installer. (crbug.com/149039)
web_contents_.reset(content::WebContents::Create(
content::WebContents::CreateParams(profile)));
WebstoreStandaloneInstaller::Callback callback =
base::Bind(&AppInstallHelper::OnAppInstallComplete,
base::Unretained(this));
installer_ = new WebstoreStandaloneInstaller(
web_contents_.get(),
id,
WebstoreStandaloneInstaller::DO_NOT_REQUIRE_VERIFIED_SITE,
prompt_type,
GURL(),
callback);
installer_->set_skip_post_install_ui(true);
installer_->BeginInstall();
}
void AppInstallHelper::OnAppInstallComplete(bool success,
const std::string& error) {
success_ = success;
error_= error;
done_callback_.Run();
}
void DeleteHelperAndRunCallback(AppInstallHelper* helper,
base::Callback<void()> callback) {
delete helper;
callback.Run();
}
} // namespace
bool StartupHelper::InstallFromWebstore(const CommandLine& cmd_line,
Profile* profile) {
std::string id = cmd_line.GetSwitchValueASCII(switches::kInstallFromWebstore);
if (!Extension::IdIsValid(id)) {
LOG(ERROR) << "Invalid id for " << switches::kInstallFromWebstore
<< " : '" << id << "'";
return false;
}
AppInstallHelper helper;
helper.BeginInstall(profile, id,
cmd_line.HasSwitch(switches::kForceAppMode) ?
WebstoreStandaloneInstaller::SKIP_PROMPT :
WebstoreStandaloneInstaller::STANDARD_PROMPT,
MessageLoop::QuitWhenIdleClosure());
MessageLoop::current()->Run();
if (!helper.success())
LOG(ERROR) << "InstallFromWebstore failed with error: " << helper.error();
return helper.success();
}
void StartupHelper::LimitedInstallFromWebstore(
const CommandLine& cmd_line,
Profile* profile,
base::Callback<void()> done_callback) {
std::string id = WebStoreIdFromLimitedInstallCmdLine(cmd_line);
if (!Extension::IdIsValid(id)) {
LOG(ERROR) << "Invalid index for " << switches::kLimitedInstallFromWebstore;
done_callback.Run();
return;
}
AppInstallHelper* helper = new AppInstallHelper();
helper->BeginInstall(profile, id,
WebstoreStandaloneInstaller::STANDARD_PROMPT,
base::Bind(&DeleteHelperAndRunCallback, helper, done_callback));
}
std::string StartupHelper::WebStoreIdFromLimitedInstallCmdLine(
const CommandLine& cmd_line) {
std::string index = cmd_line.GetSwitchValueASCII(
switches::kLimitedInstallFromWebstore);
std::string id;
if (index == "1") {
id = "nckgahadagoaajjgafhacjanaoiihapd";
} else if (index == "2") {
id = "ecglahbcnmdpdciemllbhojghbkagdje";
}
return id;
}
StartupHelper::~StartupHelper() {
if (pack_job_.get())
pack_job_->ClearClient();
}
} // namespace extensions
|