Update To 11.40.268.0
[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 "base/bind.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"
22
23 using extensions::AppRuntimeEventRouter;
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   AppRuntimeEventRouter::DispatchOnLaunchedEvent(
44       profile, extension, extensions::SOURCE_RESTART);
45 }
46
47 }  // namespace
48
49 void HandleAppLaunchForMetroRestart(Profile* profile) {
50   PrefService* prefs = g_browser_process->local_state();
51   if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestartProfile))
52     return;
53
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
57   // launch the app.
58   base::FilePath profile_dir = base::FilePath::FromUTF8Unsafe(
59       prefs->GetString(prefs::kAppLaunchForMetroRestartProfile));
60   if (profile_dir.empty() || profile->GetPath().BaseName() != profile_dir)
61     return;
62
63   prefs->ClearPref(prefs::kAppLaunchForMetroRestartProfile);
64
65   if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestart))
66     return;
67
68   std::string extension_id = prefs->GetString(prefs::kAppLaunchForMetroRestart);
69   if (extension_id.empty())
70     return;
71
72   prefs->ClearPref(prefs::kAppLaunchForMetroRestart);
73
74   const int kRestartAppLaunchDelayMs = 1000;
75   base::MessageLoop::current()->PostDelayedTask(
76       FROM_HERE,
77       base::Bind(&LaunchAppWithId, profile, extension_id),
78       base::TimeDelta::FromMilliseconds(kRestartAppLaunchDelayMs));
79 }
80
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);
87 }
88
89 void RegisterPrefs(PrefRegistrySimple* registry) {
90   registry->RegisterStringPref(prefs::kAppLaunchForMetroRestart, "");
91   registry->RegisterStringPref(prefs::kAppLaunchForMetroRestartProfile, "");
92 }
93
94 }  // namespace app_metro_launch