summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/flimflam_client_helper.h
blob: 859d95e9c4061d9ed0ddfa3082f12767f5eb21f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_DBUS_FLIMFLAM_CLIENT_HELPER_H_
#define CHROMEOS_DBUS_FLIMFLAM_CLIENT_HELPER_H_

#include <string>

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/dbus/blocking_method_caller.h"
#include "chromeos/dbus/dbus_method_call_status.h"

namespace base {

class Value;
class DictionaryValue;

}  // namespace base

namespace dbus {

class Bus;
class ErrorResponse;
class MessageWriter;
class MethodCall;
class ObjectPath;
class ObjectProxy;
class Response;
class Signal;

}  // namespace dbus

namespace chromeos {

// A class to help implement Flimflam clients.
class FlimflamClientHelper {
 public:
  // A callback to handle PropertyChanged signals.
  typedef base::Callback<void(const std::string& name,
                              const base::Value& value)> PropertyChangedHandler;

  // A callback to handle responses for methods without results.
  typedef base::Callback<void(DBusMethodCallStatus call_status)> VoidCallback;

  // A callback to handle responses for methods with ObjectPath results.
  typedef base::Callback<void(
      DBusMethodCallStatus call_status,
      const dbus::ObjectPath& result)> ObjectPathCallback;

  // A callback to handle responses for methods with DictionaryValue results.
  typedef base::Callback<void(
      DBusMethodCallStatus call_status,
      const base::DictionaryValue& result)> DictionaryValueCallback;

  // A callback to handle erros for method call.
  typedef base::Callback<void(const std::string& error_name,
                              const std::string& error_message)> ErrorCallback;

  FlimflamClientHelper(dbus::Bus* bus, dbus::ObjectProxy* proxy);

  virtual ~FlimflamClientHelper();

  // Sets PropertyChanged signal handler.
  void SetPropertyChangedHandler(const PropertyChangedHandler& handler);

  // Resets PropertyChanged signal handler.
  void ResetPropertyChangedHandler();

  // Starts monitoring PropertyChanged signal.
  void MonitorPropertyChanged(const std::string& interface_name);

  // Calls a method without results.
  void CallVoidMethod(dbus::MethodCall* method_call,
                      const VoidCallback& callback);

  // Calls a method with an object path result.
  void CallObjectPathMethod(dbus::MethodCall* method_call,
                            const ObjectPathCallback& callback);

  // Calls a method with a dictionary value result.
  void CallDictionaryValueMethod(dbus::MethodCall* method_call,
                                 const DictionaryValueCallback& callback);

  // Calls a method without results with error callback.
  void CallVoidMethodWithErrorCallback(dbus::MethodCall* method_call,
                                       const base::Closure& callback,
                                       const ErrorCallback& error_callback);

  // DEPRECATED DO NOT USE: Calls a method without results.
  bool CallVoidMethodAndBlock(dbus::MethodCall* method_call);

  // DEPRECATED DO NOT USE: Calls a method with an object path result.
  dbus::ObjectPath CallObjectPathMethodAndBlock(dbus::MethodCall* method_call);

  // DEPRECATED DO NOT USE: Calls a method with a dictionary value result.
  // The caller is responsible to delete the result.
  // This method returns NULL when method call fails.
  base::DictionaryValue* CallDictionaryValueMethodAndBlock(
      dbus::MethodCall* method_call);

  // Appends the value (basic types and string-to-string dictionary) to the
  // writer as a variant.
  static void AppendValueDataAsVariant(dbus::MessageWriter* writer,
                                       const base::Value& value);

 private:
  // Handles the result of signal connection setup.
  void OnSignalConnected(const std::string& interface,
                         const std::string& signal,
                         bool success);

  // Handles PropertyChanged signal.
  void OnPropertyChanged(dbus::Signal* signal);

  // Handles responses for methods without results.
  void OnVoidMethod(const VoidCallback& callback, dbus::Response* response);

  // Handles responses for methods with ObjectPath results.
  void OnObjectPathMethod(const ObjectPathCallback& callback,
                          dbus::Response* response);

  // Handles responses for methods with DictionaryValue results.
  void OnDictionaryValueMethod(const DictionaryValueCallback& callback,
                               dbus::Response* response);

  // Handles responses for methods without results.
  // Used by CallVoidMethodWithErrorCallback().
  void OnVoidMethodWithErrorCallback(const base::Closure& callback,
                                     dbus::Response* response);

  // Handles errors for method calls.
  void OnError(const ErrorCallback& error_callback,
               dbus::ErrorResponse* response);

  base::WeakPtrFactory<FlimflamClientHelper> weak_ptr_factory_;
  // TODO(hashimoto): Remove this when we no longer need to make blocking calls.
  BlockingMethodCaller blocking_method_caller_;
  dbus::ObjectProxy* proxy_;
  PropertyChangedHandler property_changed_handler_;

  DISALLOW_COPY_AND_ASSIGN(FlimflamClientHelper);
};

}  // namespace chromeos

#endif  // CHROMEOS_DBUS_FLIMFLAM_CLIENT_HELPER_H_