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 "chrome/browser/apps/app_launch_for_metro_restart_win.h"
      6 
      7 #include "apps/browser/api/app_runtime/app_runtime_api.h"
      8 #include "apps/launcher.h"
      9 #include "apps/pref_names.h"
     10 #include "base/bind.h"
     11 #include "base/files/file_path.h"
     12 #include "base/message_loop/message_loop.h"
     13 #include "base/prefs/pref_registry_simple.h"
     14 #include "base/prefs/pref_service.h"
     15 #include "base/time/time.h"
     16 #include "chrome/browser/browser_process.h"
     17 #include "chrome/browser/extensions/extension_service.h"
     18 #include "chrome/browser/profiles/profile.h"
     19 #include "chrome/browser/profiles/profile_manager.h"
     20 #include "chrome/common/pref_names.h"
     21 #include "extensions/browser/extension_system.h"
     22 
     23 using extensions::Extension;
     24 using extensions::ExtensionSystem;
     25 
     26 namespace app_metro_launch {
     27 
     28 namespace {
     29 
     30 void LaunchAppWithId(Profile* profile,
     31                      const std::string& extension_id) {
     32   ExtensionService* extension_service =
     33       ExtensionSystem::Get(profile)->extension_service();
     34   if (!extension_service)
     35     return;
     36 
     37   const Extension* extension =
     38       extension_service->GetExtensionById(extension_id, false);
     39   if (!extension)
     40     return;
     41 
     42   apps::AppEventRouter::DispatchOnLaunchedEvent(profile, extension);
     43 }
     44 
     45 }  // namespace
     46 
     47 void HandleAppLaunchForMetroRestart(Profile* profile) {
     48   PrefService* prefs = g_browser_process->local_state();
     49   if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestartProfile))
     50     return;
     51 
     52   // This will be called for each profile that had a browser window open before
     53   // relaunch.  After checking that the preference is set, check that the
     54   // profile that is starting up matches the profile that initially wanted to
     55   // launch the app.
     56   base::FilePath profile_dir = base::FilePath::FromUTF8Unsafe(
     57       prefs->GetString(prefs::kAppLaunchForMetroRestartProfile));
     58   if (profile_dir.empty() || profile->GetPath().BaseName() != profile_dir)
     59     return;
     60 
     61   prefs->ClearPref(prefs::kAppLaunchForMetroRestartProfile);
     62 
     63   if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestart))
     64     return;
     65 
     66   std::string extension_id = prefs->GetString(prefs::kAppLaunchForMetroRestart);
     67   if (extension_id.empty())
     68     return;
     69 
     70   prefs->ClearPref(prefs::kAppLaunchForMetroRestart);
     71 
     72   const int kRestartAppLaunchDelayMs = 1000;
     73   base::MessageLoop::current()->PostDelayedTask(
     74       FROM_HERE,
     75       base::Bind(&LaunchAppWithId, profile, extension_id),
     76       base::TimeDelta::FromMilliseconds(kRestartAppLaunchDelayMs));
     77 }
     78 
     79 void SetAppLaunchForMetroRestart(Profile* profile,
     80                                  const std::string& extension_id) {
     81   PrefService* prefs = g_browser_process->local_state();
     82   prefs->SetString(prefs::kAppLaunchForMetroRestartProfile,
     83                    profile->GetPath().BaseName().MaybeAsASCII());
     84   prefs->SetString(prefs::kAppLaunchForMetroRestart, extension_id);
     85 }
     86 
     87 void RegisterPrefs(PrefRegistrySimple* registry) {
     88   registry->RegisterStringPref(prefs::kAppLaunchForMetroRestart, "");
     89   registry->RegisterStringPref(prefs::kAppLaunchForMetroRestartProfile, "");
     90 }
     91 
     92 }  // namespace app_metro_launch
     93