system_private_api.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#include "chrome/browser/extensions/api/system_private/system_private_api.h"
6
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/pref_names.h"
13
14#if defined(OS_CHROMEOS)
15#include "chromeos/dbus/dbus_thread_manager.h"
16#include "chromeos/dbus/update_engine_client.h"
17#else
18#include "chrome/browser/upgrade_detector.h"
19#endif
20
21namespace {
22
23// Maps prefs::kIncognitoModeAvailability values (0 = enabled, ...)
24// to strings exposed to extensions.
25const char* kIncognitoModeAvailabilityStrings[] = {
26  "enabled",
27  "disabled",
28  "forced"
29};
30
31// Property keys.
32const char kBrightnessKey[] = "brightness";
33const char kDownloadProgressKey[] = "downloadProgress";
34const char kIsVolumeMutedKey[] = "isVolumeMuted";
35const char kStateKey[] = "state";
36const char kUserInitiatedKey[] = "userInitiated";
37const char kVolumeKey[] = "volume";
38
39// System update states.
40const char kNotAvailableState[] = "NotAvailable";
41const char kUpdatingState[] = "Updating";
42const char kNeedRestartState[] = "NeedRestart";
43
44// Event names.
45const char kOnBrightnessChanged[] = "systemPrivate.onBrightnessChanged";
46const char kOnVolumeChanged[] = "systemPrivate.onVolumeChanged";
47const char kOnScreenUnlocked[] = "systemPrivate.onScreenUnlocked";
48const char kOnWokeUp[] = "systemPrivate.onWokeUp";
49
50// Dispatches an extension event with |argument|
51void DispatchEvent(const std::string& event_name, base::Value* argument) {
52  scoped_ptr<base::ListValue> list_args(new base::ListValue());
53  if (argument) {
54    list_args->Append(argument);
55  }
56  g_browser_process->extension_event_router_forwarder()->
57      BroadcastEventToRenderers(event_name, list_args.Pass(), GURL());
58}
59
60}  // namespace
61
62namespace extensions {
63
64bool SystemPrivateGetIncognitoModeAvailabilityFunction::RunImpl() {
65  PrefService* prefs = profile_->GetPrefs();
66  int value = prefs->GetInteger(prefs::kIncognitoModeAvailability);
67  EXTENSION_FUNCTION_VALIDATE(
68      value >= 0 &&
69      value < static_cast<int>(arraysize(kIncognitoModeAvailabilityStrings)));
70  SetResult(Value::CreateStringValue(kIncognitoModeAvailabilityStrings[value]));
71  return true;
72}
73
74bool SystemPrivateGetUpdateStatusFunction::RunImpl() {
75  std::string state;
76  double download_progress = 0;
77#if defined(OS_CHROMEOS)
78  // With UpdateEngineClient, we can provide more detailed information about
79  // system updates on ChromeOS.
80  const chromeos::UpdateEngineClient::Status status =
81      chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->
82      GetLastStatus();
83  // |download_progress| is set to 1 after download finishes
84  // (i.e. verify, finalize and need-reboot phase) to indicate the progress
85  // even though |status.download_progress| is 0 in these phases.
86  switch (status.status) {
87    case chromeos::UpdateEngineClient::UPDATE_STATUS_ERROR:
88      state = kNotAvailableState;
89      break;
90    case chromeos::UpdateEngineClient::UPDATE_STATUS_IDLE:
91      state = kNotAvailableState;
92      break;
93    case chromeos::UpdateEngineClient::UPDATE_STATUS_CHECKING_FOR_UPDATE:
94      state = kNotAvailableState;
95      break;
96    case chromeos::UpdateEngineClient::UPDATE_STATUS_UPDATE_AVAILABLE:
97      state = kUpdatingState;
98      break;
99    case chromeos::UpdateEngineClient::UPDATE_STATUS_DOWNLOADING:
100      state = kUpdatingState;
101      download_progress = status.download_progress;
102      break;
103    case chromeos::UpdateEngineClient::UPDATE_STATUS_VERIFYING:
104      state = kUpdatingState;
105      download_progress = 1;
106      break;
107    case chromeos::UpdateEngineClient::UPDATE_STATUS_FINALIZING:
108      state = kUpdatingState;
109      download_progress = 1;
110      break;
111    case chromeos::UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT:
112      state = kNeedRestartState;
113      download_progress = 1;
114      break;
115    case chromeos::UpdateEngineClient::UPDATE_STATUS_REPORTING_ERROR_EVENT:
116      state = kNotAvailableState;
117      break;
118  }
119#else
120  if (UpgradeDetector::GetInstance()->notify_upgrade()) {
121    state = kNeedRestartState;
122    download_progress = 1;
123  } else {
124    state = kNotAvailableState;
125  }
126#endif
127  DictionaryValue* dict = new DictionaryValue();
128  dict->SetString(kStateKey, state);
129  dict->SetDouble(kDownloadProgressKey, download_progress);
130  SetResult(dict);
131
132  return true;
133}
134
135void DispatchVolumeChangedEvent(double volume, bool is_volume_muted) {
136  DictionaryValue* dict = new DictionaryValue();
137  dict->SetDouble(kVolumeKey, volume);
138  dict->SetBoolean(kIsVolumeMutedKey, is_volume_muted);
139  DispatchEvent(kOnVolumeChanged, dict);
140}
141
142void DispatchBrightnessChangedEvent(int brightness, bool user_initiated) {
143  DictionaryValue* dict = new DictionaryValue();
144  dict->SetInteger(kBrightnessKey, brightness);
145  dict->SetBoolean(kUserInitiatedKey, user_initiated);
146  DispatchEvent(kOnBrightnessChanged, dict);
147}
148
149void DispatchScreenUnlockedEvent() {
150  DispatchEvent(kOnScreenUnlocked, NULL);
151}
152
153void DispatchWokeUpEvent() {
154  DispatchEvent(kOnWokeUp, NULL);
155}
156
157}  // namespace extensions
158