info_private_api.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/chromeos/extensions/info_private_api.h"
6
7#include "base/basictypes.h"
8#include "base/prefs/pref_service.h"
9#include "base/sys_info.h"
10#include "base/values.h"
11#include "chrome/browser/chromeos/login/startup_utils.h"
12#include "chrome/browser/chromeos/login/user_manager.h"
13#include "chrome/browser/chromeos/settings/cros_settings.h"
14#include "chrome/browser/chromeos/system/timezone_util.h"
15#include "chrome/browser/profiles/profile.h"
16#include "chrome/common/pref_names.h"
17#include "chromeos/network/device_state.h"
18#include "chromeos/network/network_handler.h"
19#include "chromeos/network/network_state_handler.h"
20#include "chromeos/network/shill_property_util.h"
21#include "chromeos/settings/cros_settings_names.h"
22#include "chromeos/system/statistics_provider.h"
23#include "extensions/common/error_utils.h"
24#include "third_party/cros_system_api/dbus/service_constants.h"
25
26using chromeos::NetworkHandler;
27
28namespace extensions {
29
30namespace {
31
32// Key which corresponds to the HWID setting.
33const char kPropertyHWID[] = "hwid";
34
35// Key which corresponds to the home provider property.
36const char kPropertyHomeProvider[] = "homeProvider";
37
38// Key which corresponds to the initial_locale property.
39const char kPropertyInitialLocale[] = "initialLocale";
40
41// Key which corresponds to the board property in JS.
42const char kPropertyBoard[] = "board";
43
44// Key which corresponds to the board property in JS.
45const char kPropertyOwner[] = "isOwner";
46
47// Key which corresponds to the timezone property in JS.
48const char kPropertyTimezone[] = "timezone";
49
50// Key which corresponds to the timezone property in JS.
51const char kPropertySupportedTimezones[] = "supportedTimezones";
52
53// Key which corresponds to the large cursor A11Y property in JS.
54const char kPropertyLargeCursorEnabled[] = "a11yLargeCursorEnabled";
55
56// Key which corresponds to the sticky keys A11Y property in JS.
57const char kPropertyStickyKeysEnabled[] = "a11yStickyKeysEnabled";
58
59// Key which corresponds to the spoken feedback A11Y property in JS.
60const char kPropertySpokenFeedbackEnabled[] = "a11ySpokenFeedbackEnabled";
61
62// Key which corresponds to the high contrast mode A11Y property in JS.
63const char kPropertyHighContrastEnabled[] = "a11yHighContrastEnabled";
64
65// Key which corresponds to the screen magnifier A11Y property in JS.
66const char kPropertyScreenMagnifierEnabled[] = "a11yScreenMagnifierEnabled";
67
68// Key which corresponds to the auto click A11Y property in JS.
69const char kPropertyAutoclickEnabled[] = "a11yAutoClickEnabled";
70
71// Key which corresponds to the auto click A11Y property in JS.
72const char kPropertyVirtualKeyboardEnabled[] = "a11yVirtualKeyboardEnabled";
73
74// Key which corresponds to the send-function-keys property in JS.
75const char kPropertySendFunctionsKeys[] = "sendFunctionKeys";
76
77// Property not found error message.
78const char kPropertyNotFound[] = "Property '*' does not exist.";
79
80const struct {
81  const char* api_name;
82  const char* preference_name;
83} kPreferencesMap[] = {
84      {kPropertyLargeCursorEnabled, prefs::kLargeCursorEnabled},
85      {kPropertyStickyKeysEnabled, prefs::kStickyKeysEnabled},
86      {kPropertySpokenFeedbackEnabled, prefs::kSpokenFeedbackEnabled},
87      {kPropertyHighContrastEnabled, prefs::kHighContrastEnabled},
88      {kPropertyScreenMagnifierEnabled, prefs::kScreenMagnifierEnabled},
89      {kPropertyAutoclickEnabled, prefs::kAutoclickEnabled},
90      {kPropertyVirtualKeyboardEnabled, prefs::kVirtualKeyboardEnabled},
91      {kPropertySendFunctionsKeys, prefs::kLanguageSendFunctionKeys}};
92
93const char* GetBoolPrefNameForApiProperty(const char* api_name) {
94  for (size_t i = 0;
95       i < (sizeof(kPreferencesMap)/sizeof(*kPreferencesMap));
96       i++) {
97    if (strcmp(kPreferencesMap[i].api_name, api_name) == 0)
98      return kPreferencesMap[i].preference_name;
99  }
100
101  return NULL;
102}
103
104}  // namespace
105
106ChromeosInfoPrivateGetFunction::ChromeosInfoPrivateGetFunction() {
107}
108
109ChromeosInfoPrivateGetFunction::~ChromeosInfoPrivateGetFunction() {
110}
111
112bool ChromeosInfoPrivateGetFunction::RunImpl() {
113  base::ListValue* list = NULL;
114  EXTENSION_FUNCTION_VALIDATE(args_->GetList(0, &list));
115  scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
116  for (size_t i = 0; i < list->GetSize(); ++i) {
117    std::string property_name;
118    EXTENSION_FUNCTION_VALIDATE(list->GetString(i, &property_name));
119    base::Value* value = GetValue(property_name);
120    if (value)
121      result->Set(property_name, value);
122  }
123  SetResult(result.release());
124  SendResponse(true);
125  return true;
126}
127
128base::Value* ChromeosInfoPrivateGetFunction::GetValue(
129    const std::string& property_name) {
130  if (property_name == kPropertyHWID) {
131    std::string hwid;
132    chromeos::system::StatisticsProvider* provider =
133        chromeos::system::StatisticsProvider::GetInstance();
134    provider->GetMachineStatistic(chromeos::system::kHardwareClassKey, &hwid);
135    return new base::StringValue(hwid);
136  } else if (property_name == kPropertyHomeProvider) {
137    const chromeos::DeviceState* cellular_device =
138        NetworkHandler::Get()->network_state_handler()->GetDeviceStateByType(
139            chromeos::NetworkTypePattern::Cellular());
140    std::string home_provider_id;
141    if (cellular_device)
142      home_provider_id = cellular_device->home_provider_id();
143    return new base::StringValue(home_provider_id);
144  } else if (property_name == kPropertyInitialLocale) {
145    return new base::StringValue(
146        chromeos::StartupUtils::GetInitialLocale());
147  } else if (property_name == kPropertyBoard) {
148    return new base::StringValue(base::SysInfo::GetLsbReleaseBoard());
149  } else if (property_name == kPropertyOwner) {
150    return base::Value::CreateBooleanValue(
151        chromeos::UserManager::Get()->IsCurrentUserOwner());
152  } else if (property_name == kPropertyTimezone) {
153    return chromeos::CrosSettings::Get()->GetPref(
154            chromeos::kSystemTimezone)->DeepCopy();
155  } else if (property_name == kPropertySupportedTimezones) {
156    scoped_ptr<base::ListValue> values = chromeos::system::GetTimezoneList();
157    return values.release();
158  } else {
159    const char* pref_name =
160        GetBoolPrefNameForApiProperty(property_name.c_str());
161    if (pref_name) {
162      return base::Value::CreateBooleanValue(
163          Profile::FromBrowserContext(context_)->GetPrefs()->GetBoolean(
164              pref_name));
165    }
166  }
167
168  DLOG(ERROR) << "Unknown property request: " << property_name;
169  return NULL;
170}
171
172ChromeosInfoPrivateSetFunction::ChromeosInfoPrivateSetFunction() {
173}
174
175ChromeosInfoPrivateSetFunction::~ChromeosInfoPrivateSetFunction() {
176}
177
178bool ChromeosInfoPrivateSetFunction::RunImpl() {
179  std::string param_name;
180  EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &param_name));
181  if (param_name == kPropertyTimezone) {
182    std::string param_value;
183    EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &param_value));
184    chromeos::CrosSettings::Get()->Set(chromeos::kSystemTimezone,
185                                       base::StringValue(param_value));
186  } else {
187    const char* pref_name = GetBoolPrefNameForApiProperty(param_name.c_str());
188    if (pref_name) {
189      bool param_value;
190      EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &param_value));
191      Profile::FromBrowserContext(context_)->GetPrefs()->SetBoolean(
192          pref_name,
193          param_value);
194    } else {
195      error_ = ErrorUtils::FormatErrorMessage(kPropertyNotFound, param_name);
196      return false;
197    }
198  }
199
200  return true;
201}
202
203}  // namespace extensions
204