Remove implicit conversions from scoped_refptr to T* in cc/trees/
[chromium-blink-merge.git] / apps / app_load_service.cc
blob95608514cdc56ea89e50355c166b916502cbc04b
1 // 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.
5 #include "apps/app_load_service.h"
7 #include "apps/app_load_service_factory.h"
8 #include "apps/app_restore_service.h"
9 #include "apps/app_window_registry.h"
10 #include "apps/launcher.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/unpacked_installer.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "content/public/browser/notification_details.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_types.h"
17 #include "extensions/browser/extension_host.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/browser/notification_types.h"
21 #include "extensions/common/extension.h"
23 using extensions::Extension;
24 using extensions::ExtensionPrefs;
25 using extensions::ExtensionSystem;
27 namespace apps {
29 AppLoadService::PostReloadAction::PostReloadAction()
30 : action_type(LAUNCH),
31 command_line(CommandLine::NO_PROGRAM) {
34 AppLoadService::AppLoadService(Profile* profile)
35 : profile_(profile) {
36 registrar_.Add(this,
37 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
38 content::NotificationService::AllSources());
39 registrar_.Add(this,
40 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
41 content::NotificationService::AllSources());
44 AppLoadService::~AppLoadService() {}
46 void AppLoadService::RestartApplication(const std::string& extension_id) {
47 post_reload_actions_[extension_id].action_type = RESTART;
48 ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->
49 extension_service();
50 DCHECK(service);
51 service->ReloadExtension(extension_id);
54 void AppLoadService::RestartApplicationIfRunning(
55 const std::string& extension_id) {
56 if (apps::AppRestoreService::Get(profile_)->IsAppRestorable(extension_id))
57 RestartApplication(extension_id);
60 bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path,
61 const CommandLine& command_line,
62 const base::FilePath& current_dir) {
63 ExtensionService* extension_service =
64 ExtensionSystem::Get(profile_)->extension_service();
65 std::string extension_id;
66 if (!extensions::UnpackedInstaller::Create(extension_service)->
67 LoadFromCommandLine(base::FilePath(extension_path), &extension_id)) {
68 return false;
71 // Schedule the app to be launched once loaded.
72 PostReloadAction& action = post_reload_actions_[extension_id];
73 action.action_type = LAUNCH_WITH_COMMAND_LINE;
74 action.command_line = command_line;
75 action.current_dir = current_dir;
76 return true;
79 // static
80 AppLoadService* AppLoadService::Get(Profile* profile) {
81 return apps::AppLoadServiceFactory::GetForProfile(profile);
84 void AppLoadService::Observe(int type,
85 const content::NotificationSource& source,
86 const content::NotificationDetails& details) {
87 switch (type) {
88 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
89 extensions::ExtensionHost* host =
90 content::Details<extensions::ExtensionHost>(details).ptr();
91 const Extension* extension = host->extension();
92 // It is possible for an extension to be unloaded before it stops loading.
93 if (!extension)
94 break;
95 std::map<std::string, PostReloadAction>::iterator it =
96 post_reload_actions_.find(extension->id());
97 if (it == post_reload_actions_.end())
98 break;
100 switch (it->second.action_type) {
101 case LAUNCH:
102 LaunchPlatformApp(profile_, extension);
103 break;
104 case RESTART:
105 RestartPlatformApp(profile_, extension);
106 break;
107 case LAUNCH_WITH_COMMAND_LINE:
108 LaunchPlatformAppWithCommandLine(
109 profile_, extension, it->second.command_line,
110 it->second.current_dir);
111 break;
112 default:
113 NOTREACHED();
116 post_reload_actions_.erase(it);
117 break;
119 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
120 const extensions::UnloadedExtensionInfo* unload_info =
121 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
122 if (!unload_info->extension->is_platform_app())
123 break;
125 extensions::ExtensionPrefs* extension_prefs =
126 extensions::ExtensionPrefs::Get(profile_);
127 if (WasUnloadedForReload(*unload_info) &&
128 extension_prefs->IsActive(unload_info->extension->id()) &&
129 !HasPostReloadAction(unload_info->extension->id())) {
130 post_reload_actions_[unload_info->extension->id()].action_type = LAUNCH;
132 break;
134 default:
135 NOTREACHED();
139 bool AppLoadService::WasUnloadedForReload(
140 const extensions::UnloadedExtensionInfo& unload_info) {
141 if (unload_info.reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) {
142 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
143 return (prefs->GetDisableReasons(unload_info.extension->id()) &
144 Extension::DISABLE_RELOAD) != 0;
146 return false;
149 bool AppLoadService::HasPostReloadAction(const std::string& extension_id) {
150 return post_reload_actions_.find(extension_id) != post_reload_actions_.end();
153 } // namespace apps