Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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 "apps/app_launch_for_metro_restart_win.h" |
| 6 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 7 | #include "apps/launcher.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 8 | #include "apps/pref_names.h" |
| 9 | #include "base/bind.h" |
| 10 | #include "base/files/file_path.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 12 | #include "base/prefs/pref_service.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 13 | #include "base/time/time.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 14 | #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) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 18 | #include "chrome/browser/profiles/profile.h" |
| 19 | #include "chrome/browser/profiles/profile_manager.h" |
| 20 | #include "win8/util/win8_util.h" |
| 21 | |
| 22 | using extensions::Extension; |
| 23 | using extensions::ExtensionSystem; |
| 24 | |
| 25 | namespace apps { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 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; |
| 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 | |
| 46 | void 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) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 79 | base::MessageLoop::current()->PostDelayedTask( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 80 | FROM_HERE, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 81 | base::Bind(&LaunchAppWithId, profile, extension_id), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 82 | base::TimeDelta::FromMilliseconds(kRestartAppLaunchDelayMs)); |
| 83 | } |
| 84 | |
| 85 | void 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 |