OLD | NEW |
| (Empty) |
1 // Copyright (c) 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/extensions/app_launcher.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/prefs/pref_registry_simple.h" | |
9 #include "base/prefs/pref_service.h" | |
10 #include "base/threading/sequenced_worker_pool.h" | |
11 #include "chrome/browser/browser_process.h" | |
12 #include "chrome/common/chrome_switches.h" | |
13 #include "chrome/common/pref_names.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 | |
16 #if defined(OS_WIN) | |
17 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | |
18 #include "chrome/installer/util/browser_distribution.h" | |
19 #endif | |
20 | |
21 namespace extensions { | |
22 | |
23 namespace { | |
24 | |
25 #if defined(OS_WIN) | |
26 void UpdatePrefAndCallCallbackOnUI( | |
27 bool result, | |
28 const OnAppLauncherEnabledCompleted& completion_callback) { | |
29 PrefService* prefs = g_browser_process->local_state(); | |
30 prefs->SetBoolean(prefs::kAppLauncherIsEnabled, result); | |
31 completion_callback.Run(result); | |
32 } | |
33 | |
34 void IsAppLauncherInstalledOnBlockingPool( | |
35 const OnAppLauncherEnabledCompleted& completion_callback) { | |
36 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
37 bool result = chrome_launcher_support::IsAppLauncherPresent(); | |
38 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
39 base::Bind(UpdatePrefAndCallCallbackOnUI, result, completion_callback)); | |
40 } | |
41 #endif | |
42 | |
43 } // namespace | |
44 | |
45 enum AppLauncherState { | |
46 APP_LAUNCHER_UNKNOWN, | |
47 APP_LAUNCHER_ENABLED, | |
48 APP_LAUNCHER_DISABLED, | |
49 }; | |
50 | |
51 AppLauncherState SynchronousAppLauncherChecks() { | |
52 #if defined(USE_ASH) | |
53 return APP_LAUNCHER_ENABLED; | |
54 #elif !defined(OS_WIN) | |
55 return APP_LAUNCHER_DISABLED; | |
56 #else | |
57 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
58 switches::kShowAppListShortcut)) { | |
59 return APP_LAUNCHER_ENABLED; | |
60 } | |
61 | |
62 if (!BrowserDistribution::GetDistribution()->AppHostIsSupported()) | |
63 return APP_LAUNCHER_DISABLED; | |
64 | |
65 return APP_LAUNCHER_UNKNOWN; | |
66 #endif | |
67 } | |
68 | |
69 void UpdateIsAppLauncherEnabled( | |
70 const OnAppLauncherEnabledCompleted& completion_callback) { | |
71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
72 | |
73 AppLauncherState state = SynchronousAppLauncherChecks(); | |
74 | |
75 if (state != APP_LAUNCHER_UNKNOWN) { | |
76 bool is_enabled = state == APP_LAUNCHER_ENABLED; | |
77 PrefService* prefs = g_browser_process->local_state(); | |
78 prefs->SetBoolean(prefs::kAppLauncherIsEnabled, is_enabled); | |
79 completion_callback.Run(is_enabled); | |
80 return; | |
81 } | |
82 | |
83 #if defined(OS_WIN) | |
84 content::BrowserThread::PostBlockingPoolTask( | |
85 FROM_HERE, | |
86 base::Bind(&IsAppLauncherInstalledOnBlockingPool, | |
87 completion_callback)); | |
88 #else | |
89 // SynchronousAppLauncherChecks() never returns APP_LAUNCHER_UNKNOWN on | |
90 // !defined(OS_WIN), so this path is never reached. | |
91 NOTREACHED(); | |
92 #endif | |
93 } | |
94 | |
95 bool IsAppLauncherEnabled() { | |
96 PrefService* prefs = g_browser_process->local_state(); | |
97 // In some tests, the prefs aren't initialised, but the NTP still needs to | |
98 // work. | |
99 if (!prefs) | |
100 return SynchronousAppLauncherChecks() == APP_LAUNCHER_ENABLED; | |
101 return prefs->GetBoolean(prefs::kAppLauncherIsEnabled); | |
102 } | |
103 | |
104 namespace app_launcher { | |
105 | |
106 void RegisterPrefs(PrefRegistrySimple* registry) { | |
107 // If it is impossible to synchronously determine whether the app launcher is | |
108 // enabled, assume it is disabled. Anything that needs to know the absolute | |
109 // truth should call UpdateIsAppLauncherEnabled(). | |
110 // | |
111 // This pref is just a cache of the value from the registry from last time | |
112 // Chrome ran. To avoid having the NTP block on a registry check, it guesses | |
113 // that the value hasn't changed since last time it was checked, using this | |
114 // preference. | |
115 bool is_enabled = SynchronousAppLauncherChecks() == APP_LAUNCHER_ENABLED; | |
116 registry->RegisterBooleanPref(prefs::kAppLauncherIsEnabled, is_enabled); | |
117 } | |
118 | |
119 } // namespace app_launcher | |
120 | |
121 } // namespace extensions | |
OLD | NEW |