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