OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 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 #include "chromeos/dbus/arc_instance_client.h" | |
5 | |
6 #include "base/bind.h" | |
7 #include "base/logging.h" | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "dbus/bus.h" | |
10 #include "dbus/message.h" | |
11 #include "dbus/object_path.h" | |
12 #include "dbus/object_proxy.h" | |
13 | |
14 namespace chromeos { | |
15 | |
16 namespace { | |
17 | |
18 // todo(denniskempin): Move constants to the chromiumos platform | |
19 // service_constants.h | |
20 const char kArcInstanceServicePath[] = "/org/chromium/arc/instanceservice"; | |
21 const char kArcInstanceServiceName[] = "org.chromium.arc.InstanceService"; | |
22 const char kArcInstanceServiceInterface[] = "org.chromium.arc.InstanceService"; | |
23 | |
24 const char kStartInstanceMethod[] = "StartInstance"; | |
25 const char kStopInstanceMethod[] = "StopInstance"; | |
26 | |
27 void OnVoidDBusMethod(const VoidDBusMethodCallback& callback, | |
28 dbus::Response* response) { | |
29 callback.Run(response ? DBUS_METHOD_CALL_SUCCESS : DBUS_METHOD_CALL_FAILURE); | |
30 } | |
31 | |
32 void OnBoolDBusMethod(const BoolDBusMethodCallback& callback, | |
denniskempin (chromium)
2015/10/23 12:22:10
dbus already has a way of passing back errors, isn
Luis Héctor Chávez
2015/10/23 17:33:14
What I originally wanted was to have an extra flag
| |
33 dbus::Response* response) { | |
34 if (!response) { | |
35 callback.Run(DBUS_METHOD_CALL_FAILURE, false); | |
36 return; | |
37 } | |
38 dbus::MessageReader reader(response); | |
39 bool success = false; | |
40 if (!reader.PopBool(&success)) { | |
41 LOG(ERROR) << "Error reading response from ArcInstanceService"; | |
42 callback.Run(DBUS_METHOD_CALL_FAILURE, false); | |
43 return; | |
44 } | |
45 callback.Run(DBUS_METHOD_CALL_SUCCESS, success); | |
46 } | |
47 | |
48 class ArcInstanceClientImpl : public ArcInstanceClient { | |
49 public: | |
50 ArcInstanceClientImpl() : proxy_(nullptr), weak_ptr_factory_(this) {} | |
51 | |
52 ~ArcInstanceClientImpl() override {} | |
53 | |
54 // Start an instance of ARC. |callback| is called after the method call | |
55 // succeeds. | |
56 void StartInstance(const std::string& socket_path, | |
57 const BoolDBusMethodCallback& callback) override { | |
58 DCHECK(proxy_); | |
59 | |
60 dbus::MethodCall method_call(kArcInstanceServiceInterface, | |
61 kStartInstanceMethod); | |
62 dbus::MessageWriter builder(&method_call); | |
63 builder.AppendString(socket_path); | |
64 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
65 base::Bind(&OnBoolDBusMethod, callback)); | |
stevenjb
2015/10/23 16:34:45
To elaborate the above comment, typically we would
Luis Héctor Chávez
2015/10/23 17:33:14
I went with Dennis' suggestion and just return the
| |
66 } | |
67 | |
68 // Stop a currently running instance of ARC. |callback| is called after the | |
69 // method call succeeds. | |
70 void StopInstance(const VoidDBusMethodCallback& callback) override { | |
71 DCHECK(proxy_); | |
72 | |
73 dbus::MethodCall method_call(kArcInstanceServiceInterface, | |
74 kStopInstanceMethod); | |
75 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
76 base::Bind(&OnVoidDBusMethod, callback)); | |
77 } | |
78 | |
79 protected: | |
80 void Init(dbus::Bus* bus) override { | |
81 proxy_ = bus->GetObjectProxy(kArcInstanceServiceName, | |
82 dbus::ObjectPath(kArcInstanceServicePath)); | |
83 DCHECK(proxy_); | |
84 } | |
85 | |
86 private: | |
87 dbus::ObjectProxy* proxy_; | |
88 | |
89 // Note: This should remain the last member so it'll be destroyed and | |
90 // invalidate its weak pointers before any other members are destroyed. | |
91 base::WeakPtrFactory<ArcInstanceClientImpl> weak_ptr_factory_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(ArcInstanceClientImpl); | |
94 }; | |
95 | |
96 } // namespace | |
97 | |
98 //////////////////////////////////////////////////////////////////////////////// | |
99 // ArcInstanceClient | |
100 | |
101 ArcInstanceClient::ArcInstanceClient() {} | |
102 | |
103 ArcInstanceClient::~ArcInstanceClient() {} | |
104 | |
105 // static | |
106 ArcInstanceClient* ArcInstanceClient::Create() { | |
107 return new ArcInstanceClientImpl(); | |
108 } | |
109 | |
110 } // namespace chromeos | |
OLD | NEW |