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/launcher.h"
8 #include "apps/pref_names.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/api/app_runtime/app_runtime_api.h"
17 #include "chrome/browser/extensions/extension_service.h"
18 #include "chrome/browser/extensions/extension_system.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/common/pref_names.h"
22 #include "win8/util/win8_util.h"
24 using extensions::Extension
;
25 using extensions::ExtensionSystem
;
27 namespace app_metro_launch
{
31 void LaunchAppWithId(Profile
* profile
,
32 const std::string
& extension_id
) {
33 ExtensionService
* extension_service
=
34 ExtensionSystem::Get(profile
)->extension_service();
35 if (!extension_service
)
38 const Extension
* extension
=
39 extension_service
->GetExtensionById(extension_id
, false);
43 extensions::AppEventRouter::DispatchOnLaunchedEvent(profile
, extension
);
48 void HandleAppLaunchForMetroRestart(Profile
* profile
) {
49 PrefService
* prefs
= g_browser_process
->local_state();
50 if (!prefs
->HasPrefPath(prefs::kAppLaunchForMetroRestartProfile
))
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
57 base::FilePath profile_dir
= base::FilePath::FromUTF8Unsafe(
58 prefs
->GetString(prefs::kAppLaunchForMetroRestartProfile
));
59 if (profile_dir
.empty() || profile
->GetPath().BaseName() != profile_dir
)
62 prefs
->ClearPref(prefs::kAppLaunchForMetroRestartProfile
);
64 if (!prefs
->HasPrefPath(prefs::kAppLaunchForMetroRestart
))
67 std::string extension_id
= prefs
->GetString(prefs::kAppLaunchForMetroRestart
);
68 if (extension_id
.empty())
71 prefs
->ClearPref(prefs::kAppLaunchForMetroRestart
);
73 if (win8::IsSingleWindowMetroMode()) {
74 // In this case we have relaunched with the correct profile, but we are not
75 // in Desktop mode, so can not launch apps. Leave the preferences cleared so
76 // there are no surprises later.
80 const int kRestartAppLaunchDelayMs
= 1000;
81 base::MessageLoop::current()->PostDelayedTask(
83 base::Bind(&LaunchAppWithId
, profile
, extension_id
),
84 base::TimeDelta::FromMilliseconds(kRestartAppLaunchDelayMs
));
87 void SetAppLaunchForMetroRestart(Profile
* profile
,
88 const std::string
& extension_id
) {
89 PrefService
* prefs
= g_browser_process
->local_state();
90 prefs
->SetString(prefs::kAppLaunchForMetroRestartProfile
,
91 profile
->GetPath().BaseName().MaybeAsASCII());
92 prefs
->SetString(prefs::kAppLaunchForMetroRestart
, extension_id
);
95 void RegisterPrefs(PrefRegistrySimple
* registry
) {
96 registry
->RegisterStringPref(prefs::kAppLaunchForMetroRestart
, "");
97 registry
->RegisterStringPref(prefs::kAppLaunchForMetroRestartProfile
, "");
100 } // namespace app_metro_launch