Home | History | Annotate | Download | only in dbus
      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 "chromeos/dbus/shill_profile_client.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/message_loop/message_loop.h"
      9 #include "base/stl_util.h"
     10 #include "base/values.h"
     11 #include "chromeos/dbus/dbus_thread_manager.h"
     12 #include "chromeos/dbus/shill_property_changed_observer.h"
     13 #include "dbus/bus.h"
     14 #include "dbus/message.h"
     15 #include "dbus/object_path.h"
     16 #include "dbus/values_util.h"
     17 #include "third_party/cros_system_api/dbus/service_constants.h"
     18 
     19 namespace chromeos {
     20 
     21 namespace {
     22 
     23 const char kSharedProfilePath[] = "/profile/default";
     24 
     25 class ShillProfileClientImpl : public ShillProfileClient {
     26  public:
     27   ShillProfileClientImpl();
     28 
     29   virtual void AddPropertyChangedObserver(
     30       const dbus::ObjectPath& profile_path,
     31       ShillPropertyChangedObserver* observer) OVERRIDE {
     32     GetHelper(profile_path)->AddPropertyChangedObserver(observer);
     33   }
     34 
     35   virtual void RemovePropertyChangedObserver(
     36       const dbus::ObjectPath& profile_path,
     37       ShillPropertyChangedObserver* observer) OVERRIDE {
     38     GetHelper(profile_path)->RemovePropertyChangedObserver(observer);
     39   }
     40 
     41   virtual void GetProperties(
     42       const dbus::ObjectPath& profile_path,
     43       const DictionaryValueCallbackWithoutStatus& callback,
     44       const ErrorCallback& error_callback) OVERRIDE;
     45   virtual void GetEntry(const dbus::ObjectPath& profile_path,
     46                         const std::string& entry_path,
     47                         const DictionaryValueCallbackWithoutStatus& callback,
     48                         const ErrorCallback& error_callback) OVERRIDE;
     49   virtual void DeleteEntry(const dbus::ObjectPath& profile_path,
     50                            const std::string& entry_path,
     51                            const base::Closure& callback,
     52                            const ErrorCallback& error_callback) OVERRIDE;
     53 
     54   virtual TestInterface* GetTestInterface() OVERRIDE {
     55     return NULL;
     56   }
     57 
     58  protected:
     59   virtual void Init(dbus::Bus* bus) OVERRIDE {
     60     bus_ = bus;
     61   }
     62 
     63  private:
     64   typedef std::map<std::string, ShillClientHelper*> HelperMap;
     65 
     66   // Returns the corresponding ShillClientHelper for the profile.
     67   ShillClientHelper* GetHelper(const dbus::ObjectPath& profile_path);
     68 
     69   dbus::Bus* bus_;
     70   HelperMap helpers_;
     71   STLValueDeleter<HelperMap> helpers_deleter_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(ShillProfileClientImpl);
     74 };
     75 
     76 ShillProfileClientImpl::ShillProfileClientImpl()
     77     : bus_(NULL),
     78       helpers_deleter_(&helpers_) {
     79 }
     80 
     81 ShillClientHelper* ShillProfileClientImpl::GetHelper(
     82     const dbus::ObjectPath& profile_path) {
     83   HelperMap::iterator it = helpers_.find(profile_path.value());
     84   if (it != helpers_.end())
     85     return it->second;
     86 
     87   // There is no helper for the profile, create it.
     88   dbus::ObjectProxy* object_proxy =
     89       bus_->GetObjectProxy(shill::kFlimflamServiceName, profile_path);
     90   ShillClientHelper* helper = new ShillClientHelper(object_proxy);
     91   helper->MonitorPropertyChanged(shill::kFlimflamProfileInterface);
     92   helpers_.insert(HelperMap::value_type(profile_path.value(), helper));
     93   return helper;
     94 }
     95 
     96 void ShillProfileClientImpl::GetProperties(
     97     const dbus::ObjectPath& profile_path,
     98     const DictionaryValueCallbackWithoutStatus& callback,
     99     const ErrorCallback& error_callback) {
    100   dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
    101                                shill::kGetPropertiesFunction);
    102   GetHelper(profile_path)->CallDictionaryValueMethodWithErrorCallback(
    103       &method_call, callback, error_callback);
    104 }
    105 
    106 void ShillProfileClientImpl::GetEntry(
    107     const dbus::ObjectPath& profile_path,
    108     const std::string& entry_path,
    109     const DictionaryValueCallbackWithoutStatus& callback,
    110     const ErrorCallback& error_callback) {
    111   dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
    112                                shill::kGetEntryFunction);
    113   dbus::MessageWriter writer(&method_call);
    114   writer.AppendString(entry_path);
    115   GetHelper(profile_path)->CallDictionaryValueMethodWithErrorCallback(
    116       &method_call, callback, error_callback);
    117 }
    118 
    119 void ShillProfileClientImpl::DeleteEntry(
    120     const dbus::ObjectPath& profile_path,
    121     const std::string& entry_path,
    122     const base::Closure& callback,
    123     const ErrorCallback& error_callback) {
    124   dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
    125                                shill::kDeleteEntryFunction);
    126   dbus::MessageWriter writer(&method_call);
    127   writer.AppendString(entry_path);
    128   GetHelper(profile_path)->CallVoidMethodWithErrorCallback(
    129       &method_call, callback, error_callback);
    130 }
    131 
    132 }  // namespace
    133 
    134 ShillProfileClient::ShillProfileClient() {}
    135 
    136 ShillProfileClient::~ShillProfileClient() {}
    137 
    138 // static
    139 ShillProfileClient* ShillProfileClient::Create() {
    140   return new ShillProfileClientImpl();
    141 }
    142 
    143 // static
    144 std::string ShillProfileClient::GetSharedProfilePath() {
    145   return std::string(kSharedProfilePath);
    146 }
    147 
    148 }  // namespace chromeos
    149