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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/dbus/component_updater_service_provider.h"
#include <utility>
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "chrome/browser/component_updater/cros_component_installer_chromeos.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace ash {
namespace {
const char kErrorInvalidArgs[] = "org.freedesktop.DBus.Error.InvalidArgs";
const char kErrorInternalError[] = "org.freedesktop.DBus.Error.InternalError";
std::string ErrorToString(component_updater::ComponentManagerAsh::Error error) {
switch (error) {
case component_updater::ComponentManagerAsh::Error::NONE:
return "NONE";
case component_updater::ComponentManagerAsh::Error::UNKNOWN_COMPONENT:
return "UNKNOWN_COMPONENT";
case component_updater::ComponentManagerAsh::Error::INSTALL_FAILURE:
return "INSTALL_FAILURE";
case component_updater::ComponentManagerAsh::Error::MOUNT_FAILURE:
return "MOUNT_FAILURE";
case component_updater::ComponentManagerAsh::Error::
COMPATIBILITY_CHECK_FAILED:
return "COMPATIBILITY_CHECK_FAILED";
case component_updater::ComponentManagerAsh::Error::NOT_FOUND:
return "NOT_FOUND";
case component_updater::ComponentManagerAsh::Error::UPDATE_IN_PROGRESS:
return "UPDATE_IN_PROGRESS";
}
return "Unknown error code";
}
} // namespace
ComponentUpdaterServiceProvider::ComponentUpdaterServiceProvider(
component_updater::ComponentManagerAsh* cros_component_manager) {
DCHECK(cros_component_manager);
cros_component_manager_ = cros_component_manager;
cros_component_manager_->SetDelegate(this);
}
ComponentUpdaterServiceProvider::~ComponentUpdaterServiceProvider() {
cros_component_manager_->SetDelegate(nullptr);
}
void ComponentUpdaterServiceProvider::Start(
scoped_refptr<dbus::ExportedObject> exported_object) {
exported_object->ExportMethod(
chromeos::kComponentUpdaterServiceInterface,
chromeos::kComponentUpdaterServiceLoadComponentMethod,
base::BindRepeating(&ComponentUpdaterServiceProvider::LoadComponent,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&ComponentUpdaterServiceProvider::OnExported,
weak_ptr_factory_.GetWeakPtr()));
exported_object->ExportMethod(
chromeos::kComponentUpdaterServiceInterface,
chromeos::kComponentUpdaterServiceUnloadComponentMethod,
base::BindRepeating(&ComponentUpdaterServiceProvider::UnloadComponent,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&ComponentUpdaterServiceProvider::OnExported,
weak_ptr_factory_.GetWeakPtr()));
exported_object_ = exported_object;
}
void ComponentUpdaterServiceProvider::EmitInstalledSignal(
const std::string& component) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
&ComponentUpdaterServiceProvider::EmitInstalledSignalInternal,
weak_ptr_factory_.GetWeakPtr(), component));
}
void ComponentUpdaterServiceProvider::OnExported(
const std::string& interface_name,
const std::string& method_name,
bool success) {
if (!success) {
LOG(ERROR) << "Failed to export " << interface_name << "." << method_name;
}
}
void ComponentUpdaterServiceProvider::LoadComponent(
dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
dbus::MessageReader reader(method_call);
std::string component_name;
// |mount| is an optional parameter, and by default is true.
bool mount = true;
if (reader.PopString(&component_name)) {
// dbus::MessageReader::PopBool sets its out-param to false on failure.
// Resets |mount| to its default value on failure.
if (!reader.PopBool(&mount))
mount = true;
cros_component_manager_->Load(
component_name,
mount ? component_updater::ComponentManagerAsh::MountPolicy::kMount
: component_updater::ComponentManagerAsh::MountPolicy::kDontMount,
component_updater::ComponentManagerAsh::UpdatePolicy::kDontForce,
base::BindOnce(&ComponentUpdaterServiceProvider::OnLoadComponent,
weak_ptr_factory_.GetWeakPtr(), method_call,
std::move(response_sender)));
} else {
std::unique_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs,
"Need a string and a boolean parameter.");
std::move(response_sender).Run(std::move(error_response));
}
}
void ComponentUpdaterServiceProvider::OnLoadComponent(
dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender,
component_updater::ComponentManagerAsh::Error error,
const base::FilePath& result) {
if (error != component_updater::ComponentManagerAsh::Error::NONE) {
LOG(ERROR) << "Component updater Load API error: " << ErrorToString(error);
}
std::unique_ptr<dbus::Response> response =
dbus::Response::FromMethodCall(method_call);
dbus::MessageWriter writer(response.get());
writer.AppendString(result.value());
std::move(response_sender).Run(std::move(response));
}
void ComponentUpdaterServiceProvider::UnloadComponent(
dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
dbus::MessageReader reader(method_call);
std::string component_name;
if (reader.PopString(&component_name)) {
if (cros_component_manager_->Unload(component_name)) {
std::move(response_sender)
.Run(dbus::Response::FromMethodCall(method_call));
} else {
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInternalError, "Failed to unload component"));
}
} else {
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs,
"Missing component name string argument."));
}
}
void ComponentUpdaterServiceProvider::EmitInstalledSignalInternal(
const std::string& component) {
DCHECK(exported_object_);
dbus::Signal signal(
chromeos::kComponentUpdaterServiceInterface,
chromeos::kComponentUpdaterServiceComponentInstalledSignal);
dbus::MessageWriter writer(&signal);
writer.AppendString(component);
exported_object_->SendSignal(&signal);
}
} // namespace ash
|