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