Home | History | Annotate | Download | only in extensions
      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/values.h"
      8 #include "chrome/browser/chromeos/login/startup_utils.h"
      9 #include "chrome/browser/chromeos/login/user_manager.h"
     10 #include "chrome/browser/chromeos/system/statistics_provider.h"
     11 #include "chromeos/network/device_state.h"
     12 #include "chromeos/network/network_handler.h"
     13 #include "chromeos/network/network_state_handler.h"
     14 #include "third_party/cros_system_api/dbus/service_constants.h"
     15 
     16 using chromeos::NetworkHandler;
     17 
     18 namespace extensions {
     19 
     20 namespace {
     21 
     22 // Key which corresponds to the HWID setting.
     23 const char kPropertyHWID[] = "hwid";
     24 
     25 // Key which corresponds to the home provider property.
     26 const char kPropertyHomeProvider[] = "homeProvider";
     27 
     28 // Key which corresponds to the initial_locale property.
     29 const char kPropertyInitialLocale[] = "initialLocale";
     30 
     31 // Key which corresponds to the board property in JS.
     32 const char kPropertyBoard[] = "board";
     33 
     34 // Key which corresponds to the board property in JS.
     35 const char kPropertyOwner[] = "isOwner";
     36 
     37 }  // namespace
     38 
     39 ChromeosInfoPrivateGetFunction::ChromeosInfoPrivateGetFunction() {
     40 }
     41 
     42 ChromeosInfoPrivateGetFunction::~ChromeosInfoPrivateGetFunction() {
     43 }
     44 
     45 bool ChromeosInfoPrivateGetFunction::RunImpl() {
     46   ListValue* list = NULL;
     47   EXTENSION_FUNCTION_VALIDATE(args_->GetList(0, &list));
     48   scoped_ptr<DictionaryValue> result(new DictionaryValue());
     49   for (size_t i = 0; i < list->GetSize(); ++i) {
     50     std::string property_name;
     51     EXTENSION_FUNCTION_VALIDATE(list->GetString(i, &property_name));
     52     Value* value = GetValue(property_name);
     53     if (value)
     54       result->Set(property_name, value);
     55   }
     56   SetResult(result.release());
     57   SendResponse(true);
     58   return true;
     59 }
     60 
     61 base::Value* ChromeosInfoPrivateGetFunction::GetValue(
     62     const std::string& property_name) {
     63   if (property_name == kPropertyHWID) {
     64     std::string hwid;
     65     chromeos::system::StatisticsProvider* provider =
     66         chromeos::system::StatisticsProvider::GetInstance();
     67     provider->GetMachineStatistic(chromeos::system::kHardwareClass, &hwid);
     68     return new base::StringValue(hwid);
     69   } else if (property_name == kPropertyHomeProvider) {
     70     const chromeos::DeviceState* cellular_device =
     71         NetworkHandler::Get()->network_state_handler()->GetDeviceStateByType(
     72             flimflam::kTypeCellular);
     73     std::string home_provider_id;
     74     if (cellular_device)
     75       home_provider_id = cellular_device->home_provider_id();
     76     return new base::StringValue(home_provider_id);
     77   } else if (property_name == kPropertyInitialLocale) {
     78     return new base::StringValue(
     79         chromeos::StartupUtils::GetInitialLocale());
     80   } else if (property_name == kPropertyBoard) {
     81     std::string board;
     82     chromeos::system::StatisticsProvider* provider =
     83         chromeos::system::StatisticsProvider::GetInstance();
     84     provider->GetMachineStatistic(chromeos::system::kMachineInfoBoard, &board);
     85     return new base::StringValue(board);
     86   } else if (property_name == kPropertyOwner) {
     87     return Value::CreateBooleanValue(
     88         chromeos::UserManager::Get()->IsCurrentUserOwner());
     89   }
     90 
     91   DLOG(ERROR) << "Unknown property request: " << property_name;
     92   return NULL;
     93 }
     94 
     95 }  // namespace extensions
     96