OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/app_launcher.h" | 5 #include "apps/app_launcher.h" |
6 | 6 |
| 7 #include "apps/pref_names.h" |
7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
8 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
9 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
11 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/pref_names.h" | |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 | 15 |
16 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
17 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | 17 #include "chrome/installer/launcher_support/chrome_launcher_support.h" |
18 #include "chrome/installer/util/browser_distribution.h" | 18 #include "chrome/installer/util/browser_distribution.h" |
19 #endif | 19 #endif |
20 | 20 |
21 namespace extensions { | 21 namespace apps { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 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 { | 25 enum AppLauncherState { |
46 APP_LAUNCHER_UNKNOWN, | 26 APP_LAUNCHER_UNKNOWN, |
47 APP_LAUNCHER_ENABLED, | 27 APP_LAUNCHER_ENABLED, |
48 APP_LAUNCHER_DISABLED, | 28 APP_LAUNCHER_DISABLED, |
49 }; | 29 }; |
50 | 30 |
51 AppLauncherState SynchronousAppLauncherChecks() { | 31 AppLauncherState SynchronousAppLauncherChecks() { |
52 #if defined(USE_ASH) | 32 #if defined(USE_ASH) |
53 return APP_LAUNCHER_ENABLED; | 33 return APP_LAUNCHER_ENABLED; |
54 #elif !defined(OS_WIN) | 34 #elif !defined(OS_WIN) |
55 return APP_LAUNCHER_DISABLED; | 35 return APP_LAUNCHER_DISABLED; |
56 #else | 36 #else |
57 if (CommandLine::ForCurrentProcess()->HasSwitch( | 37 if (CommandLine::ForCurrentProcess()->HasSwitch( |
58 switches::kShowAppListShortcut)) { | 38 switches::kShowAppListShortcut)) { |
59 return APP_LAUNCHER_ENABLED; | 39 return APP_LAUNCHER_ENABLED; |
60 } | 40 } |
61 | 41 |
62 if (!BrowserDistribution::GetDistribution()->AppHostIsSupported()) | 42 if (!BrowserDistribution::GetDistribution()->AppHostIsSupported()) |
63 return APP_LAUNCHER_DISABLED; | 43 return APP_LAUNCHER_DISABLED; |
64 | 44 |
65 return APP_LAUNCHER_UNKNOWN; | 45 return APP_LAUNCHER_UNKNOWN; |
66 #endif | 46 #endif |
67 } | 47 } |
68 | 48 |
69 void UpdateIsAppLauncherEnabled( | 49 #if defined(OS_WIN) |
| 50 void UpdatePrefAndCallCallbackOnUI( |
| 51 bool result, |
| 52 const OnAppLauncherEnabledCompleted& completion_callback) { |
| 53 PrefService* prefs = g_browser_process->local_state(); |
| 54 prefs->SetBoolean(prefs::kAppLauncherIsEnabled, result); |
| 55 completion_callback.Run(result); |
| 56 } |
| 57 |
| 58 void IsAppLauncherInstalledOnBlockingPool( |
| 59 const OnAppLauncherEnabledCompleted& completion_callback) { |
| 60 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 61 bool result = chrome_launcher_support::IsAppLauncherPresent(); |
| 62 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 63 base::Bind(UpdatePrefAndCallCallbackOnUI, result, completion_callback)); |
| 64 } |
| 65 #endif |
| 66 |
| 67 } // namespace |
| 68 |
| 69 bool MaybeIsAppLauncherEnabled() { |
| 70 return SynchronousAppLauncherChecks() == APP_LAUNCHER_ENABLED; |
| 71 } |
| 72 |
| 73 void GetIsAppLauncherEnabled( |
70 const OnAppLauncherEnabledCompleted& completion_callback) { | 74 const OnAppLauncherEnabledCompleted& completion_callback) { |
71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 75 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
72 | 76 |
73 AppLauncherState state = SynchronousAppLauncherChecks(); | 77 AppLauncherState state = SynchronousAppLauncherChecks(); |
74 | 78 |
75 if (state != APP_LAUNCHER_UNKNOWN) { | 79 if (state != APP_LAUNCHER_UNKNOWN) { |
76 bool is_enabled = state == APP_LAUNCHER_ENABLED; | 80 bool is_enabled = state == APP_LAUNCHER_ENABLED; |
77 PrefService* prefs = g_browser_process->local_state(); | 81 PrefService* prefs = g_browser_process->local_state(); |
78 prefs->SetBoolean(prefs::kAppLauncherIsEnabled, is_enabled); | 82 prefs->SetBoolean(prefs::kAppLauncherIsEnabled, is_enabled); |
79 completion_callback.Run(is_enabled); | 83 completion_callback.Run(is_enabled); |
80 return; | 84 return; |
81 } | 85 } |
82 | 86 |
83 #if defined(OS_WIN) | 87 #if defined(OS_WIN) |
84 content::BrowserThread::PostBlockingPoolTask( | 88 content::BrowserThread::PostBlockingPoolTask( |
85 FROM_HERE, | 89 FROM_HERE, |
86 base::Bind(&IsAppLauncherInstalledOnBlockingPool, | 90 base::Bind(&IsAppLauncherInstalledOnBlockingPool, |
87 completion_callback)); | 91 completion_callback)); |
88 #else | 92 #else |
89 // SynchronousAppLauncherChecks() never returns APP_LAUNCHER_UNKNOWN on | 93 // SynchronousAppLauncherChecks() never returns APP_LAUNCHER_UNKNOWN on |
90 // !defined(OS_WIN), so this path is never reached. | 94 // !defined(OS_WIN), so this path is never reached. |
91 NOTREACHED(); | 95 NOTREACHED(); |
92 #endif | 96 #endif |
93 } | 97 } |
94 | 98 |
95 bool IsAppLauncherEnabled() { | 99 bool WasAppLauncherEnabled() { |
96 PrefService* prefs = g_browser_process->local_state(); | 100 PrefService* prefs = g_browser_process->local_state(); |
97 // In some tests, the prefs aren't initialised, but the NTP still needs to | 101 // In some tests, the prefs aren't initialised. |
98 // work. | |
99 if (!prefs) | 102 if (!prefs) |
100 return SynchronousAppLauncherChecks() == APP_LAUNCHER_ENABLED; | 103 return SynchronousAppLauncherChecks() == APP_LAUNCHER_ENABLED; |
101 return prefs->GetBoolean(prefs::kAppLauncherIsEnabled); | 104 return prefs->GetBoolean(prefs::kAppLauncherIsEnabled); |
102 } | 105 } |
103 | 106 |
104 namespace app_launcher { | 107 } // namespace apps |
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 |