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 "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_service.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/extension_system.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "win8/util/win8_util.h"
22 using extensions::Extension
;
23 using extensions::ExtensionSystem
;
29 void LaunchAppWithId(Profile
* profile
,
30 const std::string
& extension_id
) {
31 ExtensionService
* extension_service
=
32 ExtensionSystem::Get(profile
)->extension_service();
33 if (!extension_service
)
36 const Extension
* extension
=
37 extension_service
->GetExtensionById(extension_id
, false);
41 extensions::AppEventRouter::DispatchOnLaunchedEvent(profile
, extension
);
46 void HandleAppLaunchForMetroRestart(Profile
* profile
) {
47 PrefService
* prefs
= g_browser_process
->local_state();
48 if (!prefs
->HasPrefPath(prefs::kAppLaunchForMetroRestartProfile
))
51 // This will be called for each profile that had a browser window open before
52 // relaunch. After checking that the preference is set, check that the
53 // profile that is starting up matches the profile that initially wanted to
55 base::FilePath profile_dir
= base::FilePath::FromUTF8Unsafe(
56 prefs
->GetString(prefs::kAppLaunchForMetroRestartProfile
));
57 if (profile_dir
.empty() || profile
->GetPath().BaseName() != profile_dir
)
60 prefs
->ClearPref(prefs::kAppLaunchForMetroRestartProfile
);
62 if (!prefs
->HasPrefPath(prefs::kAppLaunchForMetroRestart
))
65 std::string extension_id
= prefs
->GetString(prefs::kAppLaunchForMetroRestart
);
66 if (extension_id
.empty())
69 prefs
->ClearPref(prefs::kAppLaunchForMetroRestart
);
71 if (win8::IsSingleWindowMetroMode()) {
72 // In this case we have relaunched with the correct profile, but we are not
73 // in Desktop mode, so can not launch apps. Leave the preferences cleared so
74 // there are no surprises later.
78 const int kRestartAppLaunchDelayMs
= 1000;
79 base::MessageLoop::current()->PostDelayedTask(
81 base::Bind(&LaunchAppWithId
, profile
, extension_id
),
82 base::TimeDelta::FromMilliseconds(kRestartAppLaunchDelayMs
));
85 void SetAppLaunchForMetroRestart(Profile
* profile
,
86 const std::string
& extension_id
) {
87 PrefService
* prefs
= g_browser_process
->local_state();
88 prefs
->SetString(prefs::kAppLaunchForMetroRestartProfile
,
89 profile
->GetPath().BaseName().MaybeAsASCII());
90 prefs
->SetString(prefs::kAppLaunchForMetroRestart
, extension_id
);