Home | History | Annotate | Download | only in apps
      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.
      4 
      5 #include "apps/app_load_service.h"
      6 
      7 #include "apps/app_load_service_factory.h"
      8 #include "apps/app_window_registry.h"
      9 #include "apps/launcher.h"
     10 #include "chrome/browser/chrome_notification_types.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/common/extension.h"
     21 
     22 using extensions::Extension;
     23 using extensions::ExtensionPrefs;
     24 using extensions::ExtensionSystem;
     25 
     26 namespace apps {
     27 
     28 AppLoadService::PostReloadAction::PostReloadAction()
     29     : action_type(LAUNCH),
     30       command_line(CommandLine::NO_PROGRAM) {
     31 }
     32 
     33 AppLoadService::AppLoadService(Profile* profile)
     34     : profile_(profile) {
     35   registrar_.Add(
     36       this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
     37       content::NotificationService::AllSources());
     38   registrar_.Add(
     39       this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
     40       content::NotificationService::AllSources());
     41 }
     42 
     43 AppLoadService::~AppLoadService() {}
     44 
     45 void AppLoadService::RestartApplication(const std::string& extension_id) {
     46   post_reload_actions_[extension_id].action_type = RESTART;
     47   ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->
     48       extension_service();
     49   DCHECK(service);
     50   service->ReloadExtension(extension_id);
     51 }
     52 
     53 bool AppLoadService::LoadAndLaunch(const base::FilePath& extension_path,
     54                                    const CommandLine& command_line,
     55                                    const base::FilePath& current_dir) {
     56   ExtensionService* extension_service =
     57       ExtensionSystem::Get(profile_)->extension_service();
     58   std::string extension_id;
     59   if (!extensions::UnpackedInstaller::Create(extension_service)->
     60           LoadFromCommandLine(base::FilePath(extension_path), &extension_id)) {
     61     return false;
     62   }
     63 
     64   // Schedule the app to be launched once loaded.
     65   PostReloadAction& action = post_reload_actions_[extension_id];
     66   action.action_type = LAUNCH_WITH_COMMAND_LINE;
     67   action.command_line = command_line;
     68   action.current_dir = current_dir;
     69   return true;
     70 }
     71 
     72 // static
     73 AppLoadService* AppLoadService::Get(Profile* profile) {
     74   return apps::AppLoadServiceFactory::GetForProfile(profile);
     75 }
     76 
     77 void AppLoadService::Observe(int type,
     78                              const content::NotificationSource& source,
     79                              const content::NotificationDetails& details) {
     80   switch (type) {
     81     case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
     82       extensions::ExtensionHost* host =
     83           content::Details<extensions::ExtensionHost>(details).ptr();
     84       const Extension* extension = host->extension();
     85       // It is possible for an extension to be unloaded before it stops loading.
     86       if (!extension)
     87         break;
     88       std::map<std::string, PostReloadAction>::iterator it =
     89           post_reload_actions_.find(extension->id());
     90       if (it == post_reload_actions_.end())
     91         break;
     92 
     93       switch (it->second.action_type) {
     94         case LAUNCH:
     95           LaunchPlatformApp(profile_, extension);
     96           break;
     97         case RESTART:
     98           RestartPlatformApp(profile_, extension);
     99           break;
    100         case LAUNCH_WITH_COMMAND_LINE:
    101           LaunchPlatformAppWithCommandLine(
    102               profile_, extension, it->second.command_line,
    103               it->second.current_dir);
    104           break;
    105         default:
    106           NOTREACHED();
    107       }
    108 
    109       post_reload_actions_.erase(it);
    110       break;
    111     }
    112     case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
    113       const extensions::UnloadedExtensionInfo* unload_info =
    114           content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
    115       if (!unload_info->extension->is_platform_app())
    116         break;
    117 
    118       extensions::ExtensionPrefs* extension_prefs =
    119           extensions::ExtensionPrefs::Get(profile_);
    120       if (WasUnloadedForReload(*unload_info) &&
    121           extension_prefs->IsActive(unload_info->extension->id()) &&
    122           !HasPostReloadAction(unload_info->extension->id())) {
    123         post_reload_actions_[unload_info->extension->id()].action_type = LAUNCH;
    124       }
    125       break;
    126     }
    127     default:
    128       NOTREACHED();
    129   }
    130 }
    131 
    132 bool AppLoadService::WasUnloadedForReload(
    133     const extensions::UnloadedExtensionInfo& unload_info) {
    134   if (unload_info.reason == extensions::UnloadedExtensionInfo::REASON_DISABLE) {
    135     ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
    136      return (prefs->GetDisableReasons(unload_info.extension->id()) &
    137         Extension::DISABLE_RELOAD) != 0;
    138   }
    139   return false;
    140 }
    141 
    142 bool AppLoadService::HasPostReloadAction(const std::string& extension_id) {
    143   return post_reload_actions_.find(extension_id) != post_reload_actions_.end();
    144 }
    145 
    146 }  // namespace apps
    147