- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / apps / app_launch_for_metro_restart_win.cc
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.
4
5 #include "chrome/browser/apps/app_launch_for_metro_restart_win.h"
6
7 #include "apps/launcher.h"
8 #include "apps/pref_names.h"
9 #include "base/bind.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"
23
24 using extensions::Extension;
25 using extensions::ExtensionSystem;
26
27 namespace app_metro_launch {
28
29 namespace {
30
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)
36     return;
37
38   const Extension* extension =
39       extension_service->GetExtensionById(extension_id, false);
40   if (!extension)
41     return;
42
43   extensions::AppEventRouter::DispatchOnLaunchedEvent(profile, extension);
44 }
45
46 }  // namespace
47
48 void HandleAppLaunchForMetroRestart(Profile* profile) {
49   PrefService* prefs = g_browser_process->local_state();
50   if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestartProfile))
51     return;
52
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
56   // launch the app.
57   base::FilePath profile_dir = base::FilePath::FromUTF8Unsafe(
58       prefs->GetString(prefs::kAppLaunchForMetroRestartProfile));
59   if (profile_dir.empty() || profile->GetPath().BaseName() != profile_dir)
60     return;
61
62   prefs->ClearPref(prefs::kAppLaunchForMetroRestartProfile);
63
64   if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestart))
65     return;
66
67   std::string extension_id = prefs->GetString(prefs::kAppLaunchForMetroRestart);
68   if (extension_id.empty())
69     return;
70
71   prefs->ClearPref(prefs::kAppLaunchForMetroRestart);
72
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.
77     return;
78   }
79
80   const int kRestartAppLaunchDelayMs = 1000;
81   base::MessageLoop::current()->PostDelayedTask(
82       FROM_HERE,
83       base::Bind(&LaunchAppWithId, profile, extension_id),
84       base::TimeDelta::FromMilliseconds(kRestartAppLaunchDelayMs));
85 }
86
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);
93 }
94
95 void RegisterPrefs(PrefRegistrySimple* registry) {
96   registry->RegisterStringPref(prefs::kAppLaunchForMetroRestart, "");
97   registry->RegisterStringPref(prefs::kAppLaunchForMetroRestartProfile, "");
98 }
99
100 }  // namespace app_metro_launch