[Mac] Cancel obsolete autoClose calls in DownloadShelfController.
[chromium-blink-merge.git] / apps / app_launch_for_metro_restart_win.cc
blob1fa0cc9e36ce044a8fe7fa3c38ceb6122953d36d
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/pref_names.h"
8 #include "base/bind.h"
9 #include "base/files/file_path.h"
10 #include "base/message_loop.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/time.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_system.h"
17 #include "chrome/browser/extensions/platform_app_launcher.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;
25 namespace apps {
27 namespace {
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)
34 return;
36 const Extension* extension =
37 extension_service->GetExtensionById(extension_id, false);
38 if (!extension)
39 return;
41 extensions::AppEventRouter::DispatchOnLaunchedEvent(profile, extension);
44 } // namespace
46 void HandleAppLaunchForMetroRestart(Profile* profile) {
47 PrefService* prefs = g_browser_process->local_state();
48 if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestartProfile))
49 return;
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
54 // launch the app.
55 base::FilePath profile_dir = base::FilePath::FromUTF8Unsafe(
56 prefs->GetString(prefs::kAppLaunchForMetroRestartProfile));
57 if (profile_dir.empty() || profile->GetPath().BaseName() != profile_dir)
58 return;
60 prefs->ClearPref(prefs::kAppLaunchForMetroRestartProfile);
62 if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestart))
63 return;
65 std::string extension_id = prefs->GetString(prefs::kAppLaunchForMetroRestart);
66 if (extension_id.empty())
67 return;
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.
75 return;
78 const int kRestartAppLaunchDelayMs = 1000;
79 MessageLoop::current()->PostDelayedTask(
80 FROM_HERE,
81 base::Bind(&LaunchAppWithId,
82 profile,
83 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 } // namespace apps