Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / chrome / browser / apps / app_launch_for_metro_restart_win.cc
blob5522327b98a32f5b9de0b69f3f58f38d8cdc0862
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"
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"
22 using extensions::AppRuntimeEventRouter;
23 using extensions::Extension;
24 using extensions::ExtensionSystem;
26 namespace app_metro_launch {
28 namespace {
30 void LaunchAppWithId(Profile* profile,
31 const std::string& extension_id) {
32 ExtensionService* extension_service =
33 ExtensionSystem::Get(profile)->extension_service();
34 if (!extension_service)
35 return;
37 const Extension* extension =
38 extension_service->GetExtensionById(extension_id, false);
39 if (!extension)
40 return;
42 AppRuntimeEventRouter::DispatchOnLaunchedEvent(profile, extension);
45 } // namespace
47 void HandleAppLaunchForMetroRestart(Profile* profile) {
48 PrefService* prefs = g_browser_process->local_state();
49 if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestartProfile))
50 return;
52 // This will be called for each profile that had a browser window open before
53 // relaunch. After checking that the preference is set, check that the
54 // profile that is starting up matches the profile that initially wanted to
55 // launch the app.
56 base::FilePath profile_dir = base::FilePath::FromUTF8Unsafe(
57 prefs->GetString(prefs::kAppLaunchForMetroRestartProfile));
58 if (profile_dir.empty() || profile->GetPath().BaseName() != profile_dir)
59 return;
61 prefs->ClearPref(prefs::kAppLaunchForMetroRestartProfile);
63 if (!prefs->HasPrefPath(prefs::kAppLaunchForMetroRestart))
64 return;
66 std::string extension_id = prefs->GetString(prefs::kAppLaunchForMetroRestart);
67 if (extension_id.empty())
68 return;
70 prefs->ClearPref(prefs::kAppLaunchForMetroRestart);
72 const int kRestartAppLaunchDelayMs = 1000;
73 base::MessageLoop::current()->PostDelayedTask(
74 FROM_HERE,
75 base::Bind(&LaunchAppWithId, profile, extension_id),
76 base::TimeDelta::FromMilliseconds(kRestartAppLaunchDelayMs));
79 void SetAppLaunchForMetroRestart(Profile* profile,
80 const std::string& extension_id) {
81 PrefService* prefs = g_browser_process->local_state();
82 prefs->SetString(prefs::kAppLaunchForMetroRestartProfile,
83 profile->GetPath().BaseName().MaybeAsASCII());
84 prefs->SetString(prefs::kAppLaunchForMetroRestart, extension_id);
87 void RegisterPrefs(PrefRegistrySimple* registry) {
88 registry->RegisterStringPref(prefs::kAppLaunchForMetroRestart, "");
89 registry->RegisterStringPref(prefs::kAppLaunchForMetroRestartProfile, "");
92 } // namespace app_metro_launch