Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / api / system_private / system_private_api.cc
blobd98ab2e0e500f43acda4e221fbd245d8e1a14dd8
1 // Copyright (c) 2012 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/extensions/api/system_private/system_private_api.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/values.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/extensions/event_router_forwarder.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/extensions/api/system_private.h"
13 #include "chrome/common/pref_names.h"
14 #include "google_apis/google_api_keys.h"
16 #if defined(OS_CHROMEOS)
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/update_engine_client.h"
19 #else
20 #include "chrome/browser/upgrade_detector.h"
21 #endif
23 namespace {
25 // Maps prefs::kIncognitoModeAvailability values (0 = enabled, ...)
26 // to strings exposed to extensions.
27 const char* const kIncognitoModeAvailabilityStrings[] = {
28 "enabled",
29 "disabled",
30 "forced"
33 // Property keys.
34 const char kBrightnessKey[] = "brightness";
35 const char kDownloadProgressKey[] = "downloadProgress";
36 const char kIsVolumeMutedKey[] = "isVolumeMuted";
37 const char kStateKey[] = "state";
38 const char kUserInitiatedKey[] = "userInitiated";
39 const char kVolumeKey[] = "volume";
41 // System update states.
42 const char kNotAvailableState[] = "NotAvailable";
43 const char kNeedRestartState[] = "NeedRestart";
45 #if defined(OS_CHROMEOS)
46 const char kUpdatingState[] = "Updating";
47 #endif // defined(OS_CHROMEOS)
49 // Dispatches an extension event with |argument|
50 void DispatchEvent(extensions::events::HistogramValue histogram_value,
51 const std::string& event_name,
52 base::Value* argument) {
53 scoped_ptr<base::ListValue> list_args(new base::ListValue());
54 if (argument) {
55 list_args->Append(argument);
57 g_browser_process->extension_event_router_forwarder()
58 ->BroadcastEventToRenderers(histogram_value, event_name, list_args.Pass(),
59 GURL());
62 } // namespace
64 namespace extensions {
66 namespace system_private = api::system_private;
68 bool SystemPrivateGetIncognitoModeAvailabilityFunction::RunSync() {
69 PrefService* prefs = GetProfile()->GetPrefs();
70 int value = prefs->GetInteger(prefs::kIncognitoModeAvailability);
71 EXTENSION_FUNCTION_VALIDATE(
72 value >= 0 &&
73 value < static_cast<int>(arraysize(kIncognitoModeAvailabilityStrings)));
74 SetResult(new base::StringValue(kIncognitoModeAvailabilityStrings[value]));
75 return true;
78 bool SystemPrivateGetUpdateStatusFunction::RunSync() {
79 std::string state;
80 double download_progress = 0;
81 #if defined(OS_CHROMEOS)
82 // With UpdateEngineClient, we can provide more detailed information about
83 // system updates on ChromeOS.
84 const chromeos::UpdateEngineClient::Status status =
85 chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->
86 GetLastStatus();
87 // |download_progress| is set to 1 after download finishes
88 // (i.e. verify, finalize and need-reboot phase) to indicate the progress
89 // even though |status.download_progress| is 0 in these phases.
90 switch (status.status) {
91 case chromeos::UpdateEngineClient::UPDATE_STATUS_ERROR:
92 state = kNotAvailableState;
93 break;
94 case chromeos::UpdateEngineClient::UPDATE_STATUS_IDLE:
95 state = kNotAvailableState;
96 break;
97 case chromeos::UpdateEngineClient::UPDATE_STATUS_CHECKING_FOR_UPDATE:
98 state = kNotAvailableState;
99 break;
100 case chromeos::UpdateEngineClient::UPDATE_STATUS_UPDATE_AVAILABLE:
101 state = kUpdatingState;
102 break;
103 case chromeos::UpdateEngineClient::UPDATE_STATUS_DOWNLOADING:
104 state = kUpdatingState;
105 download_progress = status.download_progress;
106 break;
107 case chromeos::UpdateEngineClient::UPDATE_STATUS_VERIFYING:
108 state = kUpdatingState;
109 download_progress = 1;
110 break;
111 case chromeos::UpdateEngineClient::UPDATE_STATUS_FINALIZING:
112 state = kUpdatingState;
113 download_progress = 1;
114 break;
115 case chromeos::UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT:
116 state = kNeedRestartState;
117 download_progress = 1;
118 break;
119 case chromeos::UpdateEngineClient::UPDATE_STATUS_REPORTING_ERROR_EVENT:
120 case chromeos::UpdateEngineClient::UPDATE_STATUS_ATTEMPTING_ROLLBACK:
121 state = kNotAvailableState;
122 break;
124 #else
125 if (UpgradeDetector::GetInstance()->notify_upgrade()) {
126 state = kNeedRestartState;
127 download_progress = 1;
128 } else {
129 state = kNotAvailableState;
131 #endif
132 base::DictionaryValue* dict = new base::DictionaryValue();
133 dict->SetString(kStateKey, state);
134 dict->SetDouble(kDownloadProgressKey, download_progress);
135 SetResult(dict);
137 return true;
140 bool SystemPrivateGetApiKeyFunction::RunSync() {
141 SetResult(new base::StringValue(google_apis::GetAPIKey()));
142 return true;
145 void DispatchVolumeChangedEvent(double volume, bool is_volume_muted) {
146 base::DictionaryValue* dict = new base::DictionaryValue();
147 dict->SetDouble(kVolumeKey, volume);
148 dict->SetBoolean(kIsVolumeMutedKey, is_volume_muted);
149 DispatchEvent(extensions::events::SYSTEM_PRIVATE_ON_VOLUME_CHANGED,
150 system_private::OnVolumeChanged::kEventName, dict);
153 void DispatchBrightnessChangedEvent(int brightness, bool user_initiated) {
154 base::DictionaryValue* dict = new base::DictionaryValue();
155 dict->SetInteger(kBrightnessKey, brightness);
156 dict->SetBoolean(kUserInitiatedKey, user_initiated);
157 DispatchEvent(extensions::events::SYSTEM_PRIVATE_ON_BRIGHTNESS_CHANGED,
158 system_private::OnBrightnessChanged::kEventName, dict);
161 void DispatchScreenUnlockedEvent() {
162 DispatchEvent(extensions::events::SYSTEM_PRIVATE_ON_SCREEN_UNLOCKED,
163 system_private::OnScreenUnlocked::kEventName, NULL);
166 void DispatchWokeUpEvent() {
167 DispatchEvent(extensions::events::SYSTEM_PRIVATE_ON_WOKE_UP,
168 system_private::OnWokeUp::kEventName, NULL);
171 } // namespace extensions