| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/dbus/image_burner_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "dbus/bus.h" | |
| 10 #include "dbus/message.h" | |
| 11 #include "dbus/object_path.h" | |
| 12 #include "dbus/object_proxy.h" | |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 14 | |
| 15 namespace chromeos { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // The ImageBurnerClient implementation. | |
| 20 class ImageBurnerClientImpl : public ImageBurnerClient { | |
| 21 public: | |
| 22 explicit ImageBurnerClientImpl(dbus::Bus* bus) | |
| 23 : proxy_(NULL), | |
| 24 weak_ptr_factory_(this) { | |
| 25 proxy_ = bus->GetObjectProxy( | |
| 26 imageburn::kImageBurnServiceName, | |
| 27 dbus::ObjectPath(imageburn::kImageBurnServicePath)); | |
| 28 proxy_->ConnectToSignal( | |
| 29 imageburn::kImageBurnServiceInterface, | |
| 30 imageburn::kSignalBurnFinishedName, | |
| 31 base::Bind(&ImageBurnerClientImpl::OnBurnFinished, | |
| 32 weak_ptr_factory_.GetWeakPtr()), | |
| 33 base::Bind(&ImageBurnerClientImpl::OnSignalConnected, | |
| 34 weak_ptr_factory_.GetWeakPtr())); | |
| 35 proxy_->ConnectToSignal( | |
| 36 imageburn::kImageBurnServiceInterface, | |
| 37 imageburn::kSignalBurnUpdateName, | |
| 38 base::Bind(&ImageBurnerClientImpl::OnBurnProgressUpdate, | |
| 39 weak_ptr_factory_.GetWeakPtr()), | |
| 40 base::Bind(&ImageBurnerClientImpl::OnSignalConnected, | |
| 41 weak_ptr_factory_.GetWeakPtr())); | |
| 42 } | |
| 43 virtual ~ImageBurnerClientImpl() {} | |
| 44 | |
| 45 // ImageBurnerClient override. | |
| 46 virtual void BurnImage(const std::string& from_path, | |
| 47 const std::string& to_path, | |
| 48 ErrorCallback error_callback) OVERRIDE { | |
| 49 dbus::MethodCall method_call(imageburn::kImageBurnServiceInterface, | |
| 50 imageburn::kBurnImage); | |
| 51 dbus::MessageWriter writer(&method_call); | |
| 52 writer.AppendString(from_path); | |
| 53 writer.AppendString(to_path); | |
| 54 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 55 base::Bind(&ImageBurnerClientImpl::OnBurnImage, | |
| 56 weak_ptr_factory_.GetWeakPtr(), | |
| 57 error_callback)); | |
| 58 } | |
| 59 | |
| 60 // ImageBurnerClient override. | |
| 61 virtual void SetEventHandlers( | |
| 62 BurnFinishedHandler burn_finished_handler, | |
| 63 BurnProgressUpdateHandler burn_progress_update_handler) OVERRIDE { | |
| 64 burn_finished_handler_ = burn_finished_handler; | |
| 65 burn_progress_update_handler_ = burn_progress_update_handler; | |
| 66 } | |
| 67 | |
| 68 // ImageBurnerClient override. | |
| 69 virtual void ResetEventHandlers() OVERRIDE { | |
| 70 burn_finished_handler_.Reset(); | |
| 71 burn_progress_update_handler_.Reset(); | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 // Called when a response for BurnImage is received | |
| 76 void OnBurnImage(ErrorCallback error_callback, dbus::Response* response) { | |
| 77 if (!response) { | |
| 78 error_callback.Run(); | |
| 79 return; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 // Handles burn_finished signal and calls |handler|. | |
| 84 void OnBurnFinished(dbus::Signal* signal) { | |
| 85 dbus::MessageReader reader(signal); | |
| 86 std::string target_path; | |
| 87 bool success; | |
| 88 std::string error; | |
| 89 if (!reader.PopString(&target_path) || | |
| 90 !reader.PopBool(&success) || | |
| 91 !reader.PopString(&error)) { | |
| 92 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
| 93 return; | |
| 94 } | |
| 95 if (!burn_finished_handler_.is_null()) | |
| 96 burn_finished_handler_.Run(target_path, success, error); | |
| 97 } | |
| 98 | |
| 99 // Handles burn_progress_udpate signal and calls |handler|. | |
| 100 void OnBurnProgressUpdate(dbus::Signal* signal) { | |
| 101 dbus::MessageReader reader(signal); | |
| 102 std::string target_path; | |
| 103 int64 num_bytes_burnt; | |
| 104 int64 total_size; | |
| 105 if (!reader.PopString(&target_path) || | |
| 106 !reader.PopInt64(&num_bytes_burnt) || | |
| 107 !reader.PopInt64(&total_size)) { | |
| 108 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
| 109 return; | |
| 110 } | |
| 111 if (!burn_progress_update_handler_.is_null()) | |
| 112 burn_progress_update_handler_.Run(target_path, num_bytes_burnt, | |
| 113 total_size); | |
| 114 } | |
| 115 | |
| 116 // Handles the result of signal connection setup. | |
| 117 void OnSignalConnected(const std::string& interface, | |
| 118 const std::string& signal, | |
| 119 bool successed) { | |
| 120 LOG_IF(ERROR, !successed) << "Connect to " << interface << " " << | |
| 121 signal << " failed."; | |
| 122 } | |
| 123 | |
| 124 dbus::ObjectProxy* proxy_; | |
| 125 base::WeakPtrFactory<ImageBurnerClientImpl> weak_ptr_factory_; | |
| 126 BurnFinishedHandler burn_finished_handler_; | |
| 127 BurnProgressUpdateHandler burn_progress_update_handler_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(ImageBurnerClientImpl); | |
| 130 }; | |
| 131 | |
| 132 // A stub implementaion of ImageBurnerClient. | |
| 133 class ImageBurnerClientStubImpl : public ImageBurnerClient { | |
| 134 public: | |
| 135 ImageBurnerClientStubImpl() {} | |
| 136 virtual ~ImageBurnerClientStubImpl() {} | |
| 137 virtual void BurnImage(const std::string& from_path, | |
| 138 const std::string& to_path, | |
| 139 ErrorCallback error_callback) OVERRIDE {} | |
| 140 virtual void SetEventHandlers( | |
| 141 BurnFinishedHandler burn_finished_handler, | |
| 142 BurnProgressUpdateHandler burn_progress_update_handler) OVERRIDE {} | |
| 143 virtual void ResetEventHandlers() OVERRIDE {} | |
| 144 | |
| 145 private: | |
| 146 DISALLOW_COPY_AND_ASSIGN(ImageBurnerClientStubImpl); | |
| 147 }; | |
| 148 | |
| 149 } // namespace | |
| 150 | |
| 151 ImageBurnerClient::ImageBurnerClient() { | |
| 152 } | |
| 153 | |
| 154 ImageBurnerClient::~ImageBurnerClient() { | |
| 155 } | |
| 156 | |
| 157 // static | |
| 158 ImageBurnerClient* ImageBurnerClient::Create(DBusClientImplementationType type, | |
| 159 dbus::Bus* bus) { | |
| 160 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
| 161 return new ImageBurnerClientImpl(bus); | |
| 162 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
| 163 return new ImageBurnerClientStubImpl(); | |
| 164 } | |
| 165 | |
| 166 } // chromeos | |
| OLD | NEW |