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/files/file_path.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/strings/string_util.h" | |
15 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
16 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/extensions/extension_service.h" | |
18 #include "chrome/browser/extensions/webstore_startup_installer.h" | |
19 #include "chrome/browser/profiles/profile.h" | |
20 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/common/extensions/chrome_extensions_client.h" | 17 #include "chrome/common/extensions/chrome_extensions_client.h" |
22 #include "components/crx_file/id_util.h" | |
23 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/web_contents.h" | |
25 #include "extensions/browser/sandboxed_unpacker.h" | 19 #include "extensions/browser/sandboxed_unpacker.h" |
26 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
27 #include "ipc/ipc_message.h" | |
28 | |
29 #if defined(OS_WIN) | |
30 #include "extensions/browser/app_window/app_window.h" | |
31 #include "extensions/browser/app_window/app_window_registry.h" | |
32 #include "extensions/browser/extension_registry.h" | |
33 #include "extensions/browser/extension_util.h" | |
34 #endif | |
35 | 21 |
36 using content::BrowserThread; | 22 using content::BrowserThread; |
37 | 23 |
38 namespace extensions { | 24 namespace extensions { |
39 | 25 |
40 namespace { | 26 namespace { |
41 | 27 |
42 void PrintPackExtensionMessage(const std::string& message) { | 28 void PrintPackExtensionMessage(const std::string& message) { |
43 VLOG(1) << message; | 29 VLOG(1) << message; |
44 } | 30 } |
45 | 31 |
46 // On Windows, the jumplist action for installing an ephemeral app has to use | |
47 // the --install-ephemeral-app-from-webstore command line arg to initiate an | |
48 // install. | |
49 scoped_refptr<WebstoreStandaloneInstaller> CreateEphemeralAppInstaller( | |
50 Profile* profile, | |
51 const std::string& app_id, | |
52 WebstoreStandaloneInstaller::Callback callback) { | |
53 scoped_refptr<WebstoreStandaloneInstaller> installer; | |
54 | |
55 #if defined(OS_WIN) | |
56 ExtensionRegistry* registry = ExtensionRegistry::Get(profile); | |
57 DCHECK(registry); | |
58 if (!registry->GetExtensionById(app_id, ExtensionRegistry::EVERYTHING) || | |
59 !util::IsEphemeralApp(app_id, profile)) { | |
60 return installer; | |
61 } | |
62 | |
63 AppWindowRegistry* app_window_registry = AppWindowRegistry::Get(profile); | |
64 DCHECK(app_window_registry); | |
65 AppWindow* app_window = | |
66 app_window_registry->GetCurrentAppWindowForApp(app_id); | |
67 if (!app_window) | |
68 return installer; | |
69 | |
70 installer = new WebstoreInstallWithPrompt( | |
71 app_id, profile, app_window->GetNativeWindow(), callback); | |
72 #endif | |
73 | |
74 return installer; | |
75 } | |
76 | |
77 } // namespace | 32 } // namespace |
78 | 33 |
79 StartupHelper::StartupHelper() : pack_job_succeeded_(false) { | 34 StartupHelper::StartupHelper() : pack_job_succeeded_(false) { |
80 ExtensionsClient::Set(ChromeExtensionsClient::GetInstance()); | 35 ExtensionsClient::Set(ChromeExtensionsClient::GetInstance()); |
81 } | 36 } |
82 | 37 |
83 void StartupHelper::OnPackSuccess( | 38 void StartupHelper::OnPackSuccess( |
84 const base::FilePath& crx_path, | 39 const base::FilePath& crx_path, |
85 const base::FilePath& output_private_key_path) { | 40 const base::FilePath& output_private_key_path) { |
86 pack_job_succeeded_ = true; | 41 pack_job_succeeded_ = true; |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 helper->Start(); | 187 helper->Start(); |
233 if (!helper->finished()) | 188 if (!helper->finished()) |
234 run_loop.Run(); | 189 run_loop.Run(); |
235 | 190 |
236 bool success = helper->success(); | 191 bool success = helper->success(); |
237 if (!success) | 192 if (!success) |
238 *error = base::UTF16ToUTF8(helper->error()); | 193 *error = base::UTF16ToUTF8(helper->error()); |
239 return success; | 194 return success; |
240 } | 195 } |
241 | 196 |
242 namespace { | |
243 | |
244 class AppInstallHelper { | |
245 public: | |
246 // A callback for when the install process is done. | |
247 typedef base::Callback<void()> DoneCallback; | |
248 | |
249 AppInstallHelper(); | |
250 virtual ~AppInstallHelper(); | |
251 bool success() { return success_; } | |
252 const std::string& error() { return error_; } | |
253 void BeginInstall(Profile* profile, | |
254 const std::string& id, | |
255 bool show_prompt, | |
256 DoneCallback callback); | |
257 | |
258 private: | |
259 WebstoreStandaloneInstaller::Callback Callback(); | |
260 void OnAppInstallComplete(bool success, | |
261 const std::string& error, | |
262 webstore_install::Result result); | |
263 | |
264 DoneCallback done_callback_; | |
265 | |
266 // These hold on to the result of the app install when it is complete. | |
267 bool success_; | |
268 std::string error_; | |
269 | |
270 scoped_refptr<WebstoreStandaloneInstaller> installer_; | |
271 }; | |
272 | |
273 AppInstallHelper::AppInstallHelper() : success_(false) {} | |
274 | |
275 AppInstallHelper::~AppInstallHelper() {} | |
276 | |
277 WebstoreStandaloneInstaller::Callback AppInstallHelper::Callback() { | |
278 return base::Bind(&AppInstallHelper::OnAppInstallComplete, | |
279 base::Unretained(this)); | |
280 } | |
281 | |
282 void AppInstallHelper::BeginInstall( | |
283 Profile* profile, | |
284 const std::string& id, | |
285 bool show_prompt, | |
286 DoneCallback done_callback) { | |
287 done_callback_ = done_callback; | |
288 | |
289 WebstoreStandaloneInstaller::Callback callback = | |
290 base::Bind(&AppInstallHelper::OnAppInstallComplete, | |
291 base::Unretained(this)); | |
292 | |
293 installer_ = CreateEphemeralAppInstaller(profile, id, callback); | |
294 if (installer_.get()) { | |
295 installer_->BeginInstall(); | |
296 } else { | |
297 error_ = "Not a supported ephemeral app installation."; | |
298 done_callback_.Run(); | |
299 } | |
300 } | |
301 | |
302 void AppInstallHelper::OnAppInstallComplete(bool success, | |
303 const std::string& error, | |
304 webstore_install::Result result) { | |
305 success_ = success; | |
306 error_ = error; | |
307 done_callback_.Run(); | |
308 } | |
309 | |
310 } // namespace | |
311 | |
312 bool StartupHelper::InstallEphemeralApp(const base::CommandLine& cmd_line, | |
313 Profile* profile) { | |
314 std::string id = | |
315 cmd_line.GetSwitchValueASCII(switches::kInstallEphemeralAppFromWebstore); | |
316 if (!crx_file::id_util::IdIsValid(id)) { | |
317 LOG(ERROR) << "Invalid id for " | |
318 << switches::kInstallEphemeralAppFromWebstore << " : '" << id << "'"; | |
319 return false; | |
320 } | |
321 | |
322 AppInstallHelper helper; | |
323 base::RunLoop run_loop; | |
324 helper.BeginInstall(profile, id, true, run_loop.QuitClosure()); | |
325 run_loop.Run(); | |
326 | |
327 if (!helper.success()) | |
328 LOG(ERROR) << "InstallFromWebstore failed with error: " << helper.error(); | |
329 return helper.success(); | |
330 } | |
331 | |
332 StartupHelper::~StartupHelper() { | 197 StartupHelper::~StartupHelper() { |
333 if (pack_job_.get()) | 198 if (pack_job_.get()) |
334 pack_job_->ClearClient(); | 199 pack_job_->ClearClient(); |
335 } | 200 } |
336 | 201 |
337 } // namespace extensions | 202 } // namespace extensions |
OLD | NEW |