info_private_api.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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/sys_info.h"
8#include "base/values.h"
9#include "chrome/browser/chromeos/login/startup_utils.h"
10#include "chrome/browser/chromeos/login/user_manager.h"
11#include "chrome/browser/chromeos/settings/cros_settings.h"
12#include "chrome/browser/chromeos/system/timezone_util.h"
13#include "chromeos/network/device_state.h"
14#include "chromeos/network/network_handler.h"
15#include "chromeos/network/network_state_handler.h"
16#include "chromeos/network/shill_property_util.h"
17#include "chromeos/settings/cros_settings_names.h"
18#include "chromeos/system/statistics_provider.h"
19#include "extensions/common/error_utils.h"
20#include "third_party/cros_system_api/dbus/service_constants.h"
21
22using chromeos::NetworkHandler;
23
24namespace extensions {
25
26namespace {
27
28// Key which corresponds to the HWID setting.
29const char kPropertyHWID[] = "hwid";
30
31// Key which corresponds to the home provider property.
32const char kPropertyHomeProvider[] = "homeProvider";
33
34// Key which corresponds to the initial_locale property.
35const char kPropertyInitialLocale[] = "initialLocale";
36
37// Key which corresponds to the board property in JS.
38const char kPropertyBoard[] = "board";
39
40// Key which corresponds to the board property in JS.
41const char kPropertyOwner[] = "isOwner";
42
43// Key which corresponds to the timezone property in JS.
44const char kPropertyTimezone[] = "timezone";
45
46// Key which corresponds to the timezone property in JS.
47const char kPropertySupportedTimezones[] = "supportedTimezones";
48
49// Property not found error message.
50const char kPropertyNotFound[] = "Property '*' does not exist.";
51
52}  // namespace
53
54ChromeosInfoPrivateGetFunction::ChromeosInfoPrivateGetFunction() {
55}
56
57ChromeosInfoPrivateGetFunction::~ChromeosInfoPrivateGetFunction() {
58}
59
60bool ChromeosInfoPrivateGetFunction::RunImpl() {
61  ListValue* list = NULL;
62  EXTENSION_FUNCTION_VALIDATE(args_->GetList(0, &list));
63  scoped_ptr<DictionaryValue> result(new DictionaryValue());
64  for (size_t i = 0; i < list->GetSize(); ++i) {
65    std::string property_name;
66    EXTENSION_FUNCTION_VALIDATE(list->GetString(i, &property_name));
67    Value* value = GetValue(property_name);
68    if (value)
69      result->Set(property_name, value);
70  }
71  SetResult(result.release());
72  SendResponse(true);
73  return true;
74}
75
76base::Value* ChromeosInfoPrivateGetFunction::GetValue(
77    const std::string& property_name) {
78  if (property_name == kPropertyHWID) {
79    std::string hwid;
80    chromeos::system::StatisticsProvider* provider =
81        chromeos::system::StatisticsProvider::GetInstance();
82    provider->GetMachineStatistic(chromeos::system::kHardwareClassKey, &hwid);
83    return new base::StringValue(hwid);
84  } else if (property_name == kPropertyHomeProvider) {
85    const chromeos::DeviceState* cellular_device =
86        NetworkHandler::Get()->network_state_handler()->GetDeviceStateByType(
87            chromeos::NetworkTypePattern::Cellular());
88    std::string home_provider_id;
89    if (cellular_device)
90      home_provider_id = cellular_device->home_provider_id();
91    return new base::StringValue(home_provider_id);
92  } else if (property_name == kPropertyInitialLocale) {
93    return new base::StringValue(
94        chromeos::StartupUtils::GetInitialLocale());
95  } else if (property_name == kPropertyBoard) {
96    return new base::StringValue(base::SysInfo::GetLsbReleaseBoard());
97  } else if (property_name == kPropertyOwner) {
98    return Value::CreateBooleanValue(
99        chromeos::UserManager::Get()->IsCurrentUserOwner());
100  } else if (property_name == kPropertyTimezone) {
101    return chromeos::CrosSettings::Get()->GetPref(
102            chromeos::kSystemTimezone)->DeepCopy();
103  } else if (property_name == kPropertySupportedTimezones) {
104    scoped_ptr<base::ListValue> values = chromeos::system::GetTimezoneList();
105    return values.release();
106  }
107
108  DLOG(ERROR) << "Unknown property request: " << property_name;
109  return NULL;
110}
111
112
113ChromeosInfoPrivateSetFunction::ChromeosInfoPrivateSetFunction() {
114}
115
116ChromeosInfoPrivateSetFunction::~ChromeosInfoPrivateSetFunction() {
117}
118
119bool ChromeosInfoPrivateSetFunction::RunImpl() {
120  std::string param_name;
121  EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &param_name));
122  if (param_name == kPropertyTimezone) {
123    std::string param_value;
124    EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &param_value));
125    chromeos::CrosSettings::Get()->Set(chromeos::kSystemTimezone,
126                                       base::StringValue(param_value));
127  } else {
128    error_ = ErrorUtils::FormatErrorMessage(kPropertyNotFound, param_name);
129    return false;
130  }
131
132  return true;
133}
134
135}  // namespace extensions
136