blob: f3d45e155f45dd90f0ab78e0deb91bcd1cd03093 [file] [log] [blame]
[email protected]2a69b942013-05-31 09:37:531// Copyright 2013 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.
4
5#include "apps/app_load_service.h"
6
7#include "apps/app_load_service_factory.h"
8#include "chrome/browser/extensions/extension_prefs.h"
9#include "chrome/browser/extensions/extension_service.h"
10#include "chrome/browser/extensions/extension_system.h"
11#include "chrome/browser/extensions/platform_app_launcher.h"
12#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]7b9faeb72013-06-11 12:20:1713#include "chrome/browser/extensions/unpacked_installer.h"
14#include "chrome/browser/profiles/profile.h"
[email protected]2a69b942013-05-31 09:37:5315#include "chrome/common/chrome_notification_types.h"
16#include "chrome/common/extensions/extension.h"
17#include "chrome/common/extensions/extension_constants.h"
18#include "content/public/browser/notification_details.h"
19#include "content/public/browser/notification_service.h"
20#include "content/public/browser/notification_types.h"
21
22using extensions::Extension;
23using extensions::ExtensionPrefs;
24
[email protected]2a69b942013-05-31 09:37:5325namespace apps {
26
[email protected]7b9faeb72013-06-11 12:20:1727AppLoadService::PostReloadAction::PostReloadAction()
28 : command_line(CommandLine::NO_PROGRAM) {
29}
30
[email protected]2a69b942013-05-31 09:37:5331AppLoadService::AppLoadService(Profile* profile)
32 : profile_(profile) {
33 registrar_.Add(
34 this, chrome::NOTIFICATION_EXTENSION_LOADED,
35 content::NotificationService::AllSources());
36 registrar_.Add(
37 this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
38 content::NotificationService::AllSources());
39}
40
41AppLoadService::~AppLoadService() {}
42
43void AppLoadService::RestartApplication(const std::string& extension_id) {
[email protected]7b9faeb72013-06-11 12:20:1744 post_reload_actions_[extension_id].action_type = RESTART;
[email protected]2a69b942013-05-31 09:37:5345 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->
46 extension_service();
47 DCHECK(service);
48 service->ReloadExtension(extension_id);
49}
50
[email protected]7b9faeb72013-06-11 12:20:1751bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path,
52 const CommandLine& command_line,
53 const base::FilePath& current_dir) {
54 std::string extension_id;
55 if (!extensions::UnpackedInstaller::Create(profile_->GetExtensionService())->
56 LoadFromCommandLine(base::FilePath(extension_path), &extension_id)) {
57 return false;
58 }
59
60 // Schedule the app to be launched once loaded.
61 PostReloadAction& action = post_reload_actions_[extension_id];
62 action.action_type = LAUNCH_WITH_COMMAND_LINE;
63 action.command_line = command_line;
64 action.current_dir = current_dir;
65 return true;
[email protected]2a69b942013-05-31 09:37:5366}
67
68// static
69AppLoadService* AppLoadService::Get(Profile* profile) {
70 return apps::AppLoadServiceFactory::GetForProfile(profile);
71}
72
73void AppLoadService::Observe(int type,
74 const content::NotificationSource& source,
75 const content::NotificationDetails& details) {
76 switch (type) {
77 case chrome::NOTIFICATION_EXTENSION_LOADED: {
78 const Extension* extension = content::Details<Extension>(details).ptr();
[email protected]7b9faeb72013-06-11 12:20:1779 std::map<std::string, PostReloadAction>::iterator it =
80 post_reload_actions_.find(extension->id());
81 if (it == post_reload_actions_.end())
[email protected]2a69b942013-05-31 09:37:5382 break;
83
[email protected]7b9faeb72013-06-11 12:20:1784 switch (it->second.action_type) {
85 case LAUNCH:
[email protected]2a69b942013-05-31 09:37:5386 extensions::LaunchPlatformApp(profile_, extension);
87 break;
[email protected]7b9faeb72013-06-11 12:20:1788 case RESTART:
[email protected]2a69b942013-05-31 09:37:5389 extensions::RestartPlatformApp(profile_, extension);
90 break;
[email protected]7b9faeb72013-06-11 12:20:1791 case LAUNCH_WITH_COMMAND_LINE:
92 extensions::LaunchPlatformAppWithCommandLine(
93 profile_, extension, &it->second.command_line,
94 it->second.current_dir);
95 break;
[email protected]2a69b942013-05-31 09:37:5396 default:
97 NOTREACHED();
98 }
99
[email protected]7b9faeb72013-06-11 12:20:17100 post_reload_actions_.erase(it);
[email protected]2a69b942013-05-31 09:37:53101 break;
102 }
103 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
104 const extensions::UnloadedExtensionInfo* unload_info =
105 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
106 if (!unload_info->extension->is_platform_app())
107 break;
108
109 if (unload_info->reason == extension_misc::UNLOAD_REASON_DISABLE) {
110 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
111 if ((prefs->GetDisableReasons(unload_info->extension->id()) &
112 Extension::DISABLE_RELOAD) &&
113 !extensions::ShellWindowRegistry::Get(profile_)->
114 GetShellWindowsForApp(unload_info->extension->id()).empty()) {
[email protected]7b9faeb72013-06-11 12:20:17115 post_reload_actions_[unload_info->extension->id()].action_type =
116 LAUNCH;
[email protected]2a69b942013-05-31 09:37:53117 }
118 }
119 break;
120 }
121 default:
122 NOTREACHED();
123 }
124}
125
126} // namespace apps