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 {
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 auto click A11Y property in JS.
72 const char kPropertyVirtualKeyboardEnabled[] = "a11yVirtualKeyboardEnabled";
74 // Key which corresponds to the send-function-keys property in JS.
75 const char kPropertySendFunctionsKeys[] = "sendFunctionKeys";
77 // Property not found error message.
78 const char kPropertyNotFound[] = "Property '*' does not exist.";
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}};
93 const char* GetBoolPrefNameForApiProperty(const char* api_name) {
95 i < (sizeof(kPreferencesMap)/sizeof(*kPreferencesMap));
97 if (strcmp(kPreferencesMap[i].api_name, api_name) == 0)
98 return kPreferencesMap[i].preference_name;
106 ChromeosInfoPrivateGetFunction::ChromeosInfoPrivateGetFunction() {
109 ChromeosInfoPrivateGetFunction::~ChromeosInfoPrivateGetFunction() {
112 bool 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);
121 result->Set(property_name, value);
123 SetResult(result.release());
128 base::Value* ChromeosInfoPrivateGetFunction::GetValue(
129 const std::string& property_name) {
130 if (property_name == kPropertyHWID) {
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;
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();
159 const char* pref_name =
160 GetBoolPrefNameForApiProperty(property_name.c_str());
162 return base::Value::CreateBooleanValue(
163 Profile::FromBrowserContext(context_)->GetPrefs()->GetBoolean(
168 DLOG(ERROR) << "Unknown property request: " << property_name;
172 ChromeosInfoPrivateSetFunction::ChromeosInfoPrivateSetFunction() {
175 ChromeosInfoPrivateSetFunction::~ChromeosInfoPrivateSetFunction() {
178 bool ChromeosInfoPrivateSetFunction::RunImpl() {
179 std::string param_name;
180 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, ¶m_name));
181 if (param_name == kPropertyTimezone) {
182 std::string param_value;
183 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, ¶m_value));
184 chromeos::CrosSettings::Get()->Set(chromeos::kSystemTimezone,
185 base::StringValue(param_value));
187 const char* pref_name = GetBoolPrefNameForApiProperty(param_name.c_str());
190 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, ¶m_value));
191 Profile::FromBrowserContext(context_)->GetPrefs()->SetBoolean(
195 error_ = ErrorUtils::FormatErrorMessage(kPropertyNotFound, param_name);
203 } // namespace extensions