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