1// Copyright (c) 2011 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 "extension_info_private_api_chromeos.h"
6
7#include "base/values.h"
8#include "chrome/browser/chromeos/cros/cros_library.h"
9#include "chrome/browser/chromeos/cros/network_library.h"
10#include "chrome/browser/chromeos/system_access.h"
11
12using chromeos::CrosLibrary;
13using chromeos::NetworkLibrary;
14
15namespace {
16
17// Key which corresponds to the HWID setting.
18const char kPropertyHWID[] = "hwid";
19
20// Key which corresponds to the home provider property.
21const char kPropertyHomeProvider[] = "homeProvider";
22
23}  // namespace
24
25GetChromeosInfoFunction::GetChromeosInfoFunction() {
26}
27
28GetChromeosInfoFunction::~GetChromeosInfoFunction() {
29}
30
31bool GetChromeosInfoFunction::RunImpl() {
32  ListValue* list = NULL;
33  EXTENSION_FUNCTION_VALIDATE(args_->GetList(0, &list));
34  scoped_ptr<DictionaryValue> result(new DictionaryValue());
35  for (size_t i = 0; i < list->GetSize(); ++i) {
36    std::string property_name;
37    EXTENSION_FUNCTION_VALIDATE(list->GetString(i, &property_name));
38    std::string value;
39    if (GetValue(property_name, &value))
40      result->Set(property_name, Value::CreateStringValue(value));
41  }
42  result_.reset(result.release());
43  SendResponse(true);
44  return true;
45}
46
47bool GetChromeosInfoFunction::GetValue(const std::string& property_name,
48                                       std::string* value) {
49  value->clear();
50  if (property_name == kPropertyHWID) {
51    chromeos::SystemAccess* system = chromeos::SystemAccess::GetInstance();
52    system->GetMachineStatistic(kPropertyHWID, value);
53  } else if (property_name == kPropertyHomeProvider) {
54    if (CrosLibrary::Get()->EnsureLoaded()) {
55      NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary();
56      (*value) = netlib->GetCellularHomeCarrierId();
57    } else {
58      LOG(ERROR) << "CrosLibrary can't be loaded.";
59    }
60  } else {
61    LOG(ERROR) << "Unknown property request: " << property_name;
62    return false;
63  }
64  return true;
65}
66