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"
9 #include "base/files/file_path.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_registry_simple.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/extension_service.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/common/pref_names.h"
19 #include "extensions/browser/api/app_runtime/app_runtime_api.h"
20 #include "extensions/browser/extension_system.h"
21 #include "extensions/common/constants.h"
23 using extensions::AppRuntimeEventRouter
;
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 AppRuntimeEventRouter::DispatchOnLaunchedEvent(
44 profile
, extension
, extensions::SOURCE_RESTART
);
49 void HandleAppLaunchForMetroRestart(Profile
* profile
) {
50 PrefService
* prefs
= g_browser_process
->local_state();
51 if (!prefs
->HasPrefPath(prefs::kAppLaunchForMetroRestartProfile
))
54 // This will be called for each profile that had a browser window open before
55 // relaunch. After checking that the preference is set, check that the
56 // profile that is starting up matches the profile that initially wanted to
58 base::FilePath profile_dir
= base::FilePath::FromUTF8Unsafe(
59 prefs
->GetString(prefs::kAppLaunchForMetroRestartProfile
));
60 if (profile_dir
.empty() || profile
->GetPath().BaseName() != profile_dir
)
63 prefs
->ClearPref(prefs::kAppLaunchForMetroRestartProfile
);
65 if (!prefs
->HasPrefPath(prefs::kAppLaunchForMetroRestart
))
68 std::string extension_id
= prefs
->GetString(prefs::kAppLaunchForMetroRestart
);
69 if (extension_id
.empty())
72 prefs
->ClearPref(prefs::kAppLaunchForMetroRestart
);
74 const int kRestartAppLaunchDelayMs
= 1000;
75 base::MessageLoop::current()->PostDelayedTask(
77 base::Bind(&LaunchAppWithId
, profile
, extension_id
),
78 base::TimeDelta::FromMilliseconds(kRestartAppLaunchDelayMs
));
81 void SetAppLaunchForMetroRestart(Profile
* profile
,
82 const std::string
& extension_id
) {
83 PrefService
* prefs
= g_browser_process
->local_state();
84 prefs
->SetString(prefs::kAppLaunchForMetroRestartProfile
,
85 profile
->GetPath().BaseName().MaybeAsASCII());
86 prefs
->SetString(prefs::kAppLaunchForMetroRestart
, extension_id
);
89 void RegisterPrefs(PrefRegistrySimple
* registry
) {
90 registry
->RegisterStringPref(prefs::kAppLaunchForMetroRestart
, "");
91 registry
->RegisterStringPref(prefs::kAppLaunchForMetroRestartProfile
, "");
94 } // namespace app_metro_launch