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 "apps/app_launcher.h"
7 #include "apps/pref_names.h"
8 #include "apps/switches.h"
9 #include "base/command_line.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/ui/host_desktop.h"
15 #include "content/public/browser/browser_thread.h"
18 #include "chrome/installer/launcher_support/chrome_launcher_support.h"
19 #include "chrome/installer/util/browser_distribution.h"
26 enum AppLauncherState
{
29 APP_LAUNCHER_DISABLED
,
32 AppLauncherState
SynchronousAppLauncherChecks() {
33 #if defined(USE_ASH) && !defined(OS_WIN)
34 return APP_LAUNCHER_ENABLED
;
35 #elif !defined(OS_WIN)
36 return APP_LAUNCHER_DISABLED
;
38 if (CommandLine::ForCurrentProcess()->HasSwitch(
39 switches::kShowAppListShortcut
)) {
40 return APP_LAUNCHER_ENABLED
;
44 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH
)
45 return APP_LAUNCHER_ENABLED
;
48 if (!BrowserDistribution::GetDistribution()->AppHostIsSupported())
49 return APP_LAUNCHER_DISABLED
;
51 return APP_LAUNCHER_UNKNOWN
;
56 void UpdatePrefAndCallCallbackOnUI(
58 const OnAppLauncherEnabledCompleted
& completion_callback
) {
59 PrefService
* prefs
= g_browser_process
->local_state();
60 prefs
->SetBoolean(prefs::kAppLauncherIsEnabled
, result
);
61 completion_callback
.Run(result
);
64 void IsAppLauncherInstalledOnBlockingPool(
65 const OnAppLauncherEnabledCompleted
& completion_callback
) {
66 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
67 bool result
= chrome_launcher_support::IsAppLauncherPresent();
68 content::BrowserThread::PostTask(content::BrowserThread::UI
, FROM_HERE
,
69 base::Bind(UpdatePrefAndCallCallbackOnUI
, result
, completion_callback
));
75 bool MaybeIsAppLauncherEnabled() {
76 return SynchronousAppLauncherChecks() == APP_LAUNCHER_ENABLED
;
79 void GetIsAppLauncherEnabled(
80 const OnAppLauncherEnabledCompleted
& completion_callback
) {
81 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
83 AppLauncherState state
= SynchronousAppLauncherChecks();
85 if (state
!= APP_LAUNCHER_UNKNOWN
) {
86 bool is_enabled
= state
== APP_LAUNCHER_ENABLED
;
87 PrefService
* prefs
= g_browser_process
->local_state();
88 prefs
->SetBoolean(prefs::kAppLauncherIsEnabled
, is_enabled
);
89 completion_callback
.Run(is_enabled
);
94 content::BrowserThread::PostBlockingPoolTask(
96 base::Bind(&IsAppLauncherInstalledOnBlockingPool
,
97 completion_callback
));
99 // SynchronousAppLauncherChecks() never returns APP_LAUNCHER_UNKNOWN on
100 // !defined(OS_WIN), so this path is never reached.
105 bool WasAppLauncherEnabled() {
106 PrefService
* prefs
= g_browser_process
->local_state();
107 // In some tests, the prefs aren't initialised.
109 return SynchronousAppLauncherChecks() == APP_LAUNCHER_ENABLED
;
110 return prefs
->GetBoolean(prefs::kAppLauncherIsEnabled
);