blob: 2b102743326b8c45dbf873587c21dfb1bb84a60f [file] [log] [blame]
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001// 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.
4
5#include "apps/app_launch_for_metro_restart_win.h"
6
Ben Murdochbb1529c2013-08-08 10:24:53 +01007#include "apps/launcher.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00008#include "apps/pref_names.h"
9#include "base/bind.h"
10#include "base/files/file_path.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010011#include "base/message_loop/message_loop.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000012#include "base/prefs/pref_service.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010013#include "base/time/time.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000014#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"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000018#include "chrome/browser/profiles/profile.h"
19#include "chrome/browser/profiles/profile_manager.h"
20#include "win8/util/win8_util.h"
21
22using extensions::Extension;
23using extensions::ExtensionSystem;
24
25namespace apps {
26
27namespace {
28
29void 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;
35
36 const Extension* extension =
37 extension_service->GetExtensionById(extension_id, false);
38 if (!extension)
39 return;
40
41 extensions::AppEventRouter::DispatchOnLaunchedEvent(profile, extension);
42}
43
44} // namespace
45
46void HandleAppLaunchForMetroRestart(Profile* profile) {
47 PrefService* prefs = g_browser_process->local_state();
48 if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestartProfile))
49 return;
50
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;
59
60 prefs->ClearPref(prefs::kAppLaunchForMetroRestartProfile);
61
62 if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestart))
63 return;
64
65 std::string extension_id = prefs->GetString(prefs::kAppLaunchForMetroRestart);
66 if (extension_id.empty())
67 return;
68
69 prefs->ClearPref(prefs::kAppLaunchForMetroRestart);
70
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;
76 }
77
78 const int kRestartAppLaunchDelayMs = 1000;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010079 base::MessageLoop::current()->PostDelayedTask(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000080 FROM_HERE,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010081 base::Bind(&LaunchAppWithId, profile, extension_id),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000082 base::TimeDelta::FromMilliseconds(kRestartAppLaunchDelayMs));
83}
84
85void 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);
91}
92
93} // namespace apps