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 "chrome/browser/apps/app_launch_for_metro_restart_win.h"
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"
23 using extensions::Extension
;
24 using extensions::ExtensionSystem
;
26 namespace app_metro_launch
{
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
)
37 const Extension
* extension
=
38 extension_service
->GetExtensionById(extension_id
, false);
42 apps::AppEventRouter::DispatchOnLaunchedEvent(profile
, extension
);
47 void HandleAppLaunchForMetroRestart(Profile
* profile
) {
48 PrefService
* prefs
= g_browser_process
->local_state();
49 if (!prefs
->HasPrefPath(prefs::kAppLaunchForMetroRestartProfile
))
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
56 base::FilePath profile_dir
= base::FilePath::FromUTF8Unsafe(
57 prefs
->GetString(prefs::kAppLaunchForMetroRestartProfile
));
58 if (profile_dir
.empty() || profile
->GetPath().BaseName() != profile_dir
)
61 prefs
->ClearPref(prefs::kAppLaunchForMetroRestartProfile
);
63 if (!prefs
->HasPrefPath(prefs::kAppLaunchForMetroRestart
))
66 std::string extension_id
= prefs
->GetString(prefs::kAppLaunchForMetroRestart
);
67 if (extension_id
.empty())
70 prefs
->ClearPref(prefs::kAppLaunchForMetroRestart
);
72 const int kRestartAppLaunchDelayMs
= 1000;
73 base::MessageLoop::current()->PostDelayedTask(
75 base::Bind(&LaunchAppWithId
, profile
, extension_id
),
76 base::TimeDelta::FromMilliseconds(kRestartAppLaunchDelayMs
));
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
);
87 void RegisterPrefs(PrefRegistrySimple
* registry
) {
88 registry
->RegisterStringPref(prefs::kAppLaunchForMetroRestart
, "");
89 registry
->RegisterStringPref(prefs::kAppLaunchForMetroRestartProfile
, "");
92 } // namespace app_metro_launch