summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/dbus/introspectable_client.cc
blob: 4a26dd2e911a28f5f2e6113e5fbe39b840a98366 (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
// 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.

#include "chrome/browser/chromeos/dbus/introspectable_client.h"

#include <string>
#include <vector>

#include "base/bind.h"
#include "base/logging.h"
#include "base/chromeos/chromeos_version.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"

namespace {

// D-Bus specification constants.
const char kIntrospectableInterface[] = "org.freedesktop.DBus.Introspectable";
const char kIntrospect[] = "Introspect";

}  // namespace

namespace chromeos {

// The IntrospectableClient implementation used in production.
class IntrospectableClientImpl : public IntrospectableClient {
 public:
  explicit IntrospectableClientImpl(dbus::Bus* bus)
      : weak_ptr_factory_(this),
        bus_(bus) {
    DVLOG(1) << "Creating IntrospectableClientImpl";
  }

  virtual ~IntrospectableClientImpl() {
  }

  // IntrospectableClient override.
  virtual void Introspect(const std::string& service_name,
                          const dbus::ObjectPath& object_path,
                          const IntrospectCallback& callback) OVERRIDE {
    dbus::MethodCall method_call(kIntrospectableInterface, kIntrospect);

    dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(service_name,
                                                           object_path);

    object_proxy->CallMethod(
        &method_call,
        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
        base::Bind(&IntrospectableClientImpl::OnIntrospect,
                   weak_ptr_factory_.GetWeakPtr(),
                   service_name, object_path, callback));
  }

 private:
  // Called by dbus:: when a response for Introspect() is recieved.
  void OnIntrospect(const std::string& service_name,
                    const dbus::ObjectPath& object_path,
                    const IntrospectCallback& callback,
                    dbus::Response* response) {
    // Parse response.
    bool success = false;
    std::string xml_data;
    if (response != NULL) {
      dbus::MessageReader reader(response);
      if (!reader.PopString(&xml_data)) {
        LOG(WARNING) << "Introspect response has incorrect paramters: "
                     << response->ToString();
      } else {
        success = true;
      }
    }

    // Notify client.
    callback.Run(service_name, object_path, xml_data, success);
  }

  // Weak pointer factory for generating 'this' pointers that might live longer
  // than we do.
  base::WeakPtrFactory<IntrospectableClientImpl> weak_ptr_factory_;

  dbus::Bus* bus_;

  DISALLOW_COPY_AND_ASSIGN(IntrospectableClientImpl);
};

// The IntrospectableClient implementation used on Linux desktop, which does
// nothing.
class IntrospectableClientStubImpl : public IntrospectableClient {
 public:
  // IntrospectableClient override.
  virtual void Introspect(const std::string& service_name,
                          const dbus::ObjectPath& object_path,
                          const IntrospectCallback& callback) OVERRIDE {
    VLOG(1) << "Introspect: " << service_name << " " << object_path.value();
    callback.Run(service_name, object_path, "", false);
  }
};

IntrospectableClient::IntrospectableClient() {
}

IntrospectableClient::~IntrospectableClient() {
}

// static
IntrospectableClient* IntrospectableClient::Create(dbus::Bus* bus) {
  if (base::chromeos::IsRunningOnChromeOS()) {
    return new IntrospectableClientImpl(bus);
  } else {
    return new IntrospectableClientStubImpl();
  }
}

}  // namespace chromeos