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_profile_client_stub.h"
     13 #include "chromeos/dbus/shill_property_changed_observer.h"
     14 #include "dbus/bus.h"
     15 #include "dbus/message.h"
     16 #include "dbus/object_path.h"
     17 #include "dbus/values_util.h"
     18 #include "third_party/cros_system_api/dbus/service_constants.h"
     19 
     20 namespace chromeos {
     21 
     22 namespace {
     23 
     24 class ShillProfileClientImpl : public ShillProfileClient {
     25  public:
     26   explicit ShillProfileClientImpl(dbus::Bus* bus);
     27 
     28   // ShillProfileClient overrides.
     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   virtual void GetProperties(
     41       const dbus::ObjectPath& profile_path,
     42       const DictionaryValueCallbackWithoutStatus& callback,
     43       const ErrorCallback& error_callback) OVERRIDE;
     44   virtual void GetEntry(const dbus::ObjectPath& profile_path,
     45                         const std::string& entry_path,
     46                         const DictionaryValueCallbackWithoutStatus& callback,
     47                         const ErrorCallback& error_callback) OVERRIDE;
     48   virtual void DeleteEntry(const dbus::ObjectPath& profile_path,
     49                            const std::string& entry_path,
     50                            const base::Closure& callback,
     51                            const ErrorCallback& error_callback) OVERRIDE;
     52 
     53   virtual TestInterface* GetTestInterface() OVERRIDE {
     54     return NULL;
     55   }
     56 
     57  private:
     58   typedef std::map<std::string, ShillClientHelper*> HelperMap;
     59 
     60   // Returns the corresponding ShillClientHelper for the profile.
     61   ShillClientHelper* GetHelper(const dbus::ObjectPath& profile_path);
     62 
     63   dbus::Bus* bus_;
     64   HelperMap helpers_;
     65   STLValueDeleter<HelperMap> helpers_deleter_;
     66 
     67   DISALLOW_COPY_AND_ASSIGN(ShillProfileClientImpl);
     68 };
     69 
     70 ShillProfileClientImpl::ShillProfileClientImpl(dbus::Bus* bus)
     71     : bus_(bus),
     72       helpers_deleter_(&helpers_) {
     73 }
     74 
     75 ShillClientHelper* ShillProfileClientImpl::GetHelper(
     76     const dbus::ObjectPath& profile_path) {
     77   HelperMap::iterator it = helpers_.find(profile_path.value());
     78   if (it != helpers_.end())
     79     return it->second;
     80 
     81   // There is no helper for the profile, create it.
     82   dbus::ObjectProxy* object_proxy =
     83       bus_->GetObjectProxy(flimflam::kFlimflamServiceName, profile_path);
     84   ShillClientHelper* helper = new ShillClientHelper(bus_, object_proxy);
     85   helper->MonitorPropertyChanged(flimflam::kFlimflamProfileInterface);
     86   helpers_.insert(HelperMap::value_type(profile_path.value(), helper));
     87   return helper;
     88 }
     89 
     90 void ShillProfileClientImpl::GetProperties(
     91     const dbus::ObjectPath& profile_path,
     92     const DictionaryValueCallbackWithoutStatus& callback,
     93     const ErrorCallback& error_callback) {
     94   dbus::MethodCall method_call(flimflam::kFlimflamProfileInterface,
     95                                flimflam::kGetPropertiesFunction);
     96   GetHelper(profile_path)->CallDictionaryValueMethodWithErrorCallback(
     97       &method_call, callback, error_callback);
     98 }
     99 
    100 void ShillProfileClientImpl::GetEntry(
    101     const dbus::ObjectPath& profile_path,
    102     const std::string& entry_path,
    103     const DictionaryValueCallbackWithoutStatus& callback,
    104     const ErrorCallback& error_callback) {
    105   dbus::MethodCall method_call(flimflam::kFlimflamProfileInterface,
    106                                flimflam::kGetEntryFunction);
    107   dbus::MessageWriter writer(&method_call);
    108   writer.AppendString(entry_path);
    109   GetHelper(profile_path)->CallDictionaryValueMethodWithErrorCallback(
    110       &method_call, callback, error_callback);
    111 }
    112 
    113 void ShillProfileClientImpl::DeleteEntry(
    114     const dbus::ObjectPath& profile_path,
    115     const std::string& entry_path,
    116     const base::Closure& callback,
    117     const ErrorCallback& error_callback) {
    118   dbus::MethodCall method_call(flimflam::kFlimflamProfileInterface,
    119                                flimflam::kDeleteEntryFunction);
    120   dbus::MessageWriter writer(&method_call);
    121   writer.AppendString(entry_path);
    122   GetHelper(profile_path)->CallVoidMethodWithErrorCallback(
    123       &method_call, callback, error_callback);
    124 }
    125 
    126 }  // namespace
    127 
    128 ShillProfileClient::ShillProfileClient() {}
    129 
    130 ShillProfileClient::~ShillProfileClient() {}
    131 
    132 // static
    133 ShillProfileClient* ShillProfileClient::Create(
    134     DBusClientImplementationType type,
    135     dbus::Bus* bus) {
    136   if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
    137     return new ShillProfileClientImpl(bus);
    138   DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
    139   return new ShillProfileClientStub();
    140 }
    141 
    142 }  // namespace chromeos
    143