cros: Remove hard coded top row key mode for kiosk app.
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / info_private_api.cc
blob313700cf0be06571e9d9314fc710a5ba8527837d
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/chromeos/extensions/info_private_api.h"
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"
26 using chromeos::NetworkHandler;
28 namespace extensions {
30 namespace {
32 // Key which corresponds to the HWID setting.
33 const char kPropertyHWID[] = "hwid";
35 // Key which corresponds to the home provider property.
36 const char kPropertyHomeProvider[] = "homeProvider";
38 // Key which corresponds to the initial_locale property.
39 const char kPropertyInitialLocale[] = "initialLocale";
41 // Key which corresponds to the board property in JS.
42 const char kPropertyBoard[] = "board";
44 // Key which corresponds to the board property in JS.
45 const char kPropertyOwner[] = "isOwner";
47 // Key which corresponds to the timezone property in JS.
48 const char kPropertyTimezone[] = "timezone";
50 // Key which corresponds to the timezone property in JS.
51 const char kPropertySupportedTimezones[] = "supportedTimezones";
53 // Key which corresponds to the large cursor A11Y property in JS.
54 const char kPropertyLargeCursorEnabled[] = "a11yLargeCursorEnabled";
56 // Key which corresponds to the sticky keys A11Y property in JS.
57 const char kPropertyStickyKeysEnabled[] = "a11yStickyKeysEnabled";
59 // Key which corresponds to the spoken feedback A11Y property in JS.
60 const char kPropertySpokenFeedbackEnabled[] = "a11ySpokenFeedbackEnabled";
62 // Key which corresponds to the high contrast mode A11Y property in JS.
63 const char kPropertyHighContrastEnabled[] = "a11yHighContrastEnabled";
65 // Key which corresponds to the screen magnifier A11Y property in JS.
66 const char kPropertyScreenMagnifierEnabled[] = "a11yScreenMagnifierEnabled";
68 // Key which corresponds to the auto click A11Y property in JS.
69 const char kPropertyAutoclickEnabled[] = "a11yAutoClickEnabled";
71 // Key which corresponds to the send-function-keys property in JS.
72 const char kPropertySendFunctionsKeys[] = "sendFunctionKeys";
74 // Property not found error message.
75 const char kPropertyNotFound[] = "Property '*' does not exist.";
77 const struct {
78 const char* api_name;
79 const char* preference_name;
80 } kPreferencesMap[] = {
81 { kPropertyLargeCursorEnabled, prefs::kLargeCursorEnabled },
82 { kPropertyStickyKeysEnabled, prefs::kStickyKeysEnabled },
83 { kPropertySpokenFeedbackEnabled, prefs::kSpokenFeedbackEnabled },
84 { kPropertyHighContrastEnabled, prefs::kHighContrastEnabled },
85 { kPropertyScreenMagnifierEnabled, prefs::kScreenMagnifierEnabled },
86 { kPropertyAutoclickEnabled, prefs::kAutoclickEnabled },
87 { kPropertySendFunctionsKeys, prefs::kLanguageSendFunctionKeys }
90 const char* GetBoolPrefNameForApiProperty(const char* api_name) {
91 for (size_t i = 0;
92 i < (sizeof(kPreferencesMap)/sizeof(*kPreferencesMap));
93 i++) {
94 if (strcmp(kPreferencesMap[i].api_name, api_name) == 0)
95 return kPreferencesMap[i].preference_name;
98 return NULL;
101 } // namespace
103 ChromeosInfoPrivateGetFunction::ChromeosInfoPrivateGetFunction() {
106 ChromeosInfoPrivateGetFunction::~ChromeosInfoPrivateGetFunction() {
109 bool ChromeosInfoPrivateGetFunction::RunImpl() {
110 base::ListValue* list = NULL;
111 EXTENSION_FUNCTION_VALIDATE(args_->GetList(0, &list));
112 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
113 for (size_t i = 0; i < list->GetSize(); ++i) {
114 std::string property_name;
115 EXTENSION_FUNCTION_VALIDATE(list->GetString(i, &property_name));
116 base::Value* value = GetValue(property_name);
117 if (value)
118 result->Set(property_name, value);
120 SetResult(result.release());
121 SendResponse(true);
122 return true;
125 base::Value* ChromeosInfoPrivateGetFunction::GetValue(
126 const std::string& property_name) {
127 if (property_name == kPropertyHWID) {
128 std::string hwid;
129 chromeos::system::StatisticsProvider* provider =
130 chromeos::system::StatisticsProvider::GetInstance();
131 provider->GetMachineStatistic(chromeos::system::kHardwareClassKey, &hwid);
132 return new base::StringValue(hwid);
133 } else if (property_name == kPropertyHomeProvider) {
134 const chromeos::DeviceState* cellular_device =
135 NetworkHandler::Get()->network_state_handler()->GetDeviceStateByType(
136 chromeos::NetworkTypePattern::Cellular());
137 std::string home_provider_id;
138 if (cellular_device)
139 home_provider_id = cellular_device->home_provider_id();
140 return new base::StringValue(home_provider_id);
141 } else if (property_name == kPropertyInitialLocale) {
142 return new base::StringValue(
143 chromeos::StartupUtils::GetInitialLocale());
144 } else if (property_name == kPropertyBoard) {
145 return new base::StringValue(base::SysInfo::GetLsbReleaseBoard());
146 } else if (property_name == kPropertyOwner) {
147 return base::Value::CreateBooleanValue(
148 chromeos::UserManager::Get()->IsCurrentUserOwner());
149 } else if (property_name == kPropertyTimezone) {
150 return chromeos::CrosSettings::Get()->GetPref(
151 chromeos::kSystemTimezone)->DeepCopy();
152 } else if (property_name == kPropertySupportedTimezones) {
153 scoped_ptr<base::ListValue> values = chromeos::system::GetTimezoneList();
154 return values.release();
155 } else {
156 const char* pref_name =
157 GetBoolPrefNameForApiProperty(property_name.c_str());
158 if (pref_name) {
159 return base::Value::CreateBooleanValue(
160 Profile::FromBrowserContext(context_)->GetPrefs()->GetBoolean(
161 pref_name));
165 DLOG(ERROR) << "Unknown property request: " << property_name;
166 return NULL;
169 ChromeosInfoPrivateSetFunction::ChromeosInfoPrivateSetFunction() {
172 ChromeosInfoPrivateSetFunction::~ChromeosInfoPrivateSetFunction() {
175 bool ChromeosInfoPrivateSetFunction::RunImpl() {
176 std::string param_name;
177 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &param_name));
178 if (param_name == kPropertyTimezone) {
179 std::string param_value;
180 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &param_value));
181 chromeos::CrosSettings::Get()->Set(chromeos::kSystemTimezone,
182 base::StringValue(param_value));
183 } else {
184 const char* pref_name = GetBoolPrefNameForApiProperty(param_name.c_str());
185 if (pref_name) {
186 bool param_value;
187 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &param_value));
188 Profile::FromBrowserContext(context_)->GetPrefs()->SetBoolean(
189 pref_name,
190 param_value);
191 } else {
192 error_ = ErrorUtils::FormatErrorMessage(kPropertyNotFound, param_name);
193 return false;
197 return true;
200 } // namespace extensions