Add DumpAccessibilityTree tests for modal dialogs.
[chromium-blink-merge.git] / chromeos / dbus / image_burner_client.cc
bloba807f07c2514facfffd7861fcd776f1e574edff5
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.
5 #include "chromeos/dbus/image_burner_client.h"
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "chromeos/dbus/fake_image_burner_client.h"
10 #include "dbus/bus.h"
11 #include "dbus/message.h"
12 #include "dbus/object_path.h"
13 #include "dbus/object_proxy.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
16 namespace chromeos {
18 namespace {
20 // The ImageBurnerClient implementation.
21 class ImageBurnerClientImpl : public ImageBurnerClient {
22 public:
23 ImageBurnerClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {}
25 virtual ~ImageBurnerClientImpl() {}
27 // ImageBurnerClient override.
28 virtual void BurnImage(const std::string& from_path,
29 const std::string& to_path,
30 const ErrorCallback& error_callback) OVERRIDE {
31 dbus::MethodCall method_call(imageburn::kImageBurnServiceInterface,
32 imageburn::kBurnImage);
33 dbus::MessageWriter writer(&method_call);
34 writer.AppendString(from_path);
35 writer.AppendString(to_path);
36 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
37 base::Bind(&ImageBurnerClientImpl::OnBurnImage,
38 weak_ptr_factory_.GetWeakPtr(),
39 error_callback));
42 // ImageBurnerClient override.
43 virtual void SetEventHandlers(
44 const BurnFinishedHandler& burn_finished_handler,
45 const BurnProgressUpdateHandler& burn_progress_update_handler) OVERRIDE {
46 burn_finished_handler_ = burn_finished_handler;
47 burn_progress_update_handler_ = burn_progress_update_handler;
50 // ImageBurnerClient override.
51 virtual void ResetEventHandlers() OVERRIDE {
52 burn_finished_handler_.Reset();
53 burn_progress_update_handler_.Reset();
56 protected:
57 virtual void Init(dbus::Bus* bus) OVERRIDE {
58 proxy_ =
59 bus->GetObjectProxy(imageburn::kImageBurnServiceName,
60 dbus::ObjectPath(imageburn::kImageBurnServicePath));
61 proxy_->ConnectToSignal(
62 imageburn::kImageBurnServiceInterface,
63 imageburn::kSignalBurnFinishedName,
64 base::Bind(&ImageBurnerClientImpl::OnBurnFinished,
65 weak_ptr_factory_.GetWeakPtr()),
66 base::Bind(&ImageBurnerClientImpl::OnSignalConnected,
67 weak_ptr_factory_.GetWeakPtr()));
68 proxy_->ConnectToSignal(
69 imageburn::kImageBurnServiceInterface,
70 imageburn::kSignalBurnUpdateName,
71 base::Bind(&ImageBurnerClientImpl::OnBurnProgressUpdate,
72 weak_ptr_factory_.GetWeakPtr()),
73 base::Bind(&ImageBurnerClientImpl::OnSignalConnected,
74 weak_ptr_factory_.GetWeakPtr()));
77 private:
78 // Called when a response for BurnImage is received
79 void OnBurnImage(ErrorCallback error_callback, dbus::Response* response) {
80 if (!response) {
81 error_callback.Run();
82 return;
86 // Handles burn_finished signal and calls |handler|.
87 void OnBurnFinished(dbus::Signal* signal) {
88 dbus::MessageReader reader(signal);
89 std::string target_path;
90 bool success;
91 std::string error;
92 if (!reader.PopString(&target_path) ||
93 !reader.PopBool(&success) ||
94 !reader.PopString(&error)) {
95 LOG(ERROR) << "Invalid signal: " << signal->ToString();
96 return;
98 if (!burn_finished_handler_.is_null())
99 burn_finished_handler_.Run(target_path, success, error);
102 // Handles burn_progress_udpate signal and calls |handler|.
103 void OnBurnProgressUpdate(dbus::Signal* signal) {
104 dbus::MessageReader reader(signal);
105 std::string target_path;
106 int64 num_bytes_burnt;
107 int64 total_size;
108 if (!reader.PopString(&target_path) ||
109 !reader.PopInt64(&num_bytes_burnt) ||
110 !reader.PopInt64(&total_size)) {
111 LOG(ERROR) << "Invalid signal: " << signal->ToString();
112 return;
114 if (!burn_progress_update_handler_.is_null())
115 burn_progress_update_handler_.Run(target_path, num_bytes_burnt,
116 total_size);
119 // Handles the result of signal connection setup.
120 void OnSignalConnected(const std::string& interface,
121 const std::string& signal,
122 bool succeeded) {
123 LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " <<
124 signal << " failed.";
127 dbus::ObjectProxy* proxy_;
128 BurnFinishedHandler burn_finished_handler_;
129 BurnProgressUpdateHandler burn_progress_update_handler_;
131 // Note: This should remain the last member so it'll be destroyed and
132 // invalidate its weak pointers before any other members are destroyed.
133 base::WeakPtrFactory<ImageBurnerClientImpl> weak_ptr_factory_;
135 DISALLOW_COPY_AND_ASSIGN(ImageBurnerClientImpl);
138 } // namespace
140 ImageBurnerClient::ImageBurnerClient() {
143 ImageBurnerClient::~ImageBurnerClient() {
146 // static
147 ImageBurnerClient* ImageBurnerClient::Create(
148 DBusClientImplementationType type) {
149 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
150 return new ImageBurnerClientImpl();
151 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
152 return new FakeImageBurnerClient();
155 } // namespace chromeos