Home | History | Annotate | Download | only in dbus
      1 // Copyright (c) 2013 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/fake_bluetooth_profile_manager_client.h"
      6 
      7 #include <map>
      8 #include <string>
      9 
     10 #include "base/bind.h"
     11 #include "base/logging.h"
     12 #include "chromeos/dbus/fake_bluetooth_profile_service_provider.h"
     13 #include "dbus/bus.h"
     14 #include "dbus/message.h"
     15 #include "dbus/object_path.h"
     16 #include "dbus/object_proxy.h"
     17 #include "third_party/cros_system_api/dbus/service_constants.h"
     18 
     19 namespace chromeos {
     20 
     21 const char FakeBluetoothProfileManagerClient::kL2capUuid[] =
     22     "4d995052-33cc-4fdf-b446-75f32942a076";
     23 const char FakeBluetoothProfileManagerClient::kRfcommUuid[] =
     24     "3f6d6dbf-a6ad-45fc-9653-47dc912ef70e";
     25 
     26 FakeBluetoothProfileManagerClient::FakeBluetoothProfileManagerClient() {
     27 }
     28 
     29 FakeBluetoothProfileManagerClient::~FakeBluetoothProfileManagerClient() {
     30 }
     31 
     32 void FakeBluetoothProfileManagerClient::Init(dbus::Bus* bus) {
     33 }
     34 
     35 void FakeBluetoothProfileManagerClient::RegisterProfile(
     36     const dbus::ObjectPath& profile_path,
     37     const std::string& uuid,
     38     const Options& options,
     39     const base::Closure& callback,
     40     const ErrorCallback& error_callback) {
     41   VLOG(1) << "RegisterProfile: " << profile_path.value() << ": " << uuid;
     42 
     43   // check options for channel & psm
     44 
     45   ServiceProviderMap::iterator iter = service_provider_map_.find(profile_path);
     46   if (iter == service_provider_map_.end()) {
     47     error_callback.Run(bluetooth_profile_manager::kErrorInvalidArguments,
     48                        "No profile created");
     49   } else {
     50     ProfileMap::iterator piter = profile_map_.find(uuid);
     51     if (piter != profile_map_.end()) {
     52       error_callback.Run(bluetooth_profile_manager::kErrorAlreadyExists,
     53                          "Profile already registered");
     54     } else {
     55       profile_map_[uuid] = profile_path;
     56       callback.Run();
     57     }
     58   }
     59 }
     60 
     61 void FakeBluetoothProfileManagerClient::UnregisterProfile(
     62     const dbus::ObjectPath& profile_path,
     63     const base::Closure& callback,
     64     const ErrorCallback& error_callback) {
     65   VLOG(1) << "UnregisterProfile: " << profile_path.value();
     66 
     67   ServiceProviderMap::iterator iter = service_provider_map_.find(profile_path);
     68   if (iter == service_provider_map_.end()) {
     69     error_callback.Run(bluetooth_profile_manager::kErrorInvalidArguments,
     70                        "Profile not registered");
     71   } else {
     72     for (ProfileMap::iterator piter = profile_map_.begin();
     73          piter != profile_map_.end(); ++piter) {
     74       if (piter->second == profile_path) {
     75         profile_map_.erase(piter);
     76         break;
     77       }
     78     }
     79 
     80     callback.Run();
     81   }
     82 }
     83 
     84 void FakeBluetoothProfileManagerClient::RegisterProfileServiceProvider(
     85     FakeBluetoothProfileServiceProvider* service_provider) {
     86   service_provider_map_[service_provider->object_path_] = service_provider;
     87 }
     88 
     89 void FakeBluetoothProfileManagerClient::UnregisterProfileServiceProvider(
     90     FakeBluetoothProfileServiceProvider* service_provider) {
     91   ServiceProviderMap::iterator iter =
     92       service_provider_map_.find(service_provider->object_path_);
     93   if (iter != service_provider_map_.end() && iter->second == service_provider)
     94     service_provider_map_.erase(iter);
     95 }
     96 
     97 FakeBluetoothProfileServiceProvider*
     98 FakeBluetoothProfileManagerClient::GetProfileServiceProvider(
     99     const std::string& uuid) {
    100   ProfileMap::iterator iter = profile_map_.find(uuid);
    101   if (iter == profile_map_.end())
    102     return NULL;
    103   return service_provider_map_[iter->second];
    104 }
    105 
    106 }  // namespace chromeos
    107