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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
// Copyright 2013 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/extensions/api/image_writer_private/operation_manager.h"
#include <utility>
#include "base/lazy_instance.h"
#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/image_writer_private/destroy_partitions_operation.h"
#include "chrome/browser/extensions/api/image_writer_private/error_constants.h"
#include "chrome/browser/extensions/api/image_writer_private/operation.h"
#include "chrome/browser/extensions/api/image_writer_private/write_from_file_operation.h"
#include "chrome/browser/extensions/api/image_writer_private/write_from_url_operation.h"
#include "chrome/browser/extensions/event_router_forwarder.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_host.h"
#include "extensions/common/extension_id.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/file_manager/path_util.h"
#endif
namespace image_writer_api = extensions::api::image_writer_private;
namespace extensions {
namespace image_writer {
using content::BrowserThread;
OperationManager::OperationManager(content::BrowserContext* context)
: browser_context_(context) {
extension_registry_observation_.Observe(
ExtensionRegistry::Get(browser_context_));
process_manager_observation_.Observe(ProcessManager::Get(browser_context_));
}
OperationManager::~OperationManager() = default;
void OperationManager::Shutdown() {
for (auto& id_and_operation : operations_) {
scoped_refptr<Operation> operation = id_and_operation.second;
operation->PostTask(base::BindOnce(&Operation::Abort, operation));
}
}
void OperationManager::StartWriteFromUrl(
const ExtensionId& extension_id,
GURL url,
const std::string& hash,
const std::string& device_path,
Operation::StartWriteCallback callback) {
#if BUILDFLAG(IS_CHROMEOS)
// Chrome OS can only support a single operation at a time.
if (operations_.size() > 0) {
#else
auto existing_operation = operations_.find(extension_id);
if (existing_operation != operations_.end()) {
#endif
std::move(callback).Run(false, error::kOperationAlreadyInProgress);
return;
}
mojo::PendingRemote<network::mojom::URLLoaderFactory>
url_loader_factory_remote;
browser_context_->GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess()
->Clone(url_loader_factory_remote.InitWithNewPipeAndPassReceiver());
auto operation = base::MakeRefCounted<WriteFromUrlOperation>(
weak_factory_.GetWeakPtr(), extension_id,
std::move(url_loader_factory_remote), url, hash, device_path,
GetAssociatedDownloadFolder());
operations_[extension_id] = operation;
operation->PostTask(base::BindOnce(&Operation::Start, operation));
std::move(callback).Run(true, "");
}
void OperationManager::StartWriteFromFile(
const ExtensionId& extension_id,
const base::FilePath& path,
const std::string& device_path,
Operation::StartWriteCallback callback) {
#if BUILDFLAG(IS_CHROMEOS)
// Chrome OS can only support a single operation at a time.
if (operations_.size() > 0) {
#else
auto existing_operation = operations_.find(extension_id);
if (existing_operation != operations_.end()) {
#endif
std::move(callback).Run(false, error::kOperationAlreadyInProgress);
return;
}
auto operation = base::MakeRefCounted<WriteFromFileOperation>(
weak_factory_.GetWeakPtr(), extension_id, path, device_path,
GetAssociatedDownloadFolder());
operations_[extension_id] = operation;
operation->PostTask(base::BindOnce(&Operation::Start, operation));
std::move(callback).Run(true, "");
}
void OperationManager::CancelWrite(const ExtensionId& extension_id,
Operation::CancelWriteCallback callback) {
Operation* existing_operation = GetOperation(extension_id);
if (existing_operation == nullptr) {
std::move(callback).Run(false, error::kNoOperationInProgress);
} else {
existing_operation->PostTask(
base::BindOnce(&Operation::Cancel, existing_operation));
DeleteOperation(extension_id);
std::move(callback).Run(true, "");
}
}
void OperationManager::DestroyPartitions(
const ExtensionId& extension_id,
const std::string& device_path,
Operation::StartWriteCallback callback) {
auto existing_operation = operations_.find(extension_id);
if (existing_operation != operations_.end()) {
std::move(callback).Run(false, error::kOperationAlreadyInProgress);
return;
}
auto operation = base::MakeRefCounted<DestroyPartitionsOperation>(
weak_factory_.GetWeakPtr(), extension_id, device_path,
GetAssociatedDownloadFolder());
operations_[extension_id] = operation;
operation->PostTask(base::BindOnce(&Operation::Start, operation));
std::move(callback).Run(true, "");
}
void OperationManager::OnProgress(const ExtensionId& extension_id,
image_writer_api::Stage stage,
int progress) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
image_writer_api::ProgressInfo info;
info.stage = stage;
info.percent_complete = progress;
auto args(image_writer_api::OnWriteProgress::Create(info));
std::unique_ptr<Event> event(new Event(
events::IMAGE_WRITER_PRIVATE_ON_WRITE_PROGRESS,
image_writer_api::OnWriteProgress::kEventName, std::move(args)));
EventRouter::Get(browser_context_)
->DispatchEventToExtension(extension_id, std::move(event));
}
void OperationManager::OnComplete(const ExtensionId& extension_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto args(image_writer_api::OnWriteComplete::Create());
std::unique_ptr<Event> event(new Event(
events::IMAGE_WRITER_PRIVATE_ON_WRITE_COMPLETE,
image_writer_api::OnWriteComplete::kEventName, std::move(args)));
EventRouter::Get(browser_context_)
->DispatchEventToExtension(extension_id, std::move(event));
DeleteOperation(extension_id);
}
void OperationManager::OnError(const ExtensionId& extension_id,
image_writer_api::Stage stage,
int progress,
const std::string& error_message) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
image_writer_api::ProgressInfo info;
DLOG(ERROR) << "ImageWriter error: " << error_message;
info.stage = stage;
info.percent_complete = progress;
auto args(image_writer_api::OnWriteError::Create(info, error_message));
std::unique_ptr<Event> event(
new Event(events::IMAGE_WRITER_PRIVATE_ON_WRITE_ERROR,
image_writer_api::OnWriteError::kEventName, std::move(args)));
EventRouter::Get(browser_context_)
->DispatchEventToExtension(extension_id, std::move(event));
DeleteOperation(extension_id);
}
base::FilePath OperationManager::GetAssociatedDownloadFolder() {
#if BUILDFLAG(IS_CHROMEOS)
Profile* profile = Profile::FromBrowserContext(browser_context_);
return file_manager::util::GetDownloadsFolderForProfile(profile);
#else
return base::FilePath();
#endif
}
Operation* OperationManager::GetOperation(const ExtensionId& extension_id) {
auto existing_operation = operations_.find(extension_id);
if (existing_operation == operations_.end())
return nullptr;
return existing_operation->second.get();
}
void OperationManager::DeleteOperation(const ExtensionId& extension_id) {
auto existing_operation = operations_.find(extension_id);
if (existing_operation != operations_.end()) {
operations_.erase(existing_operation);
}
}
void OperationManager::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) {
DeleteOperation(extension->id());
}
void OperationManager::OnShutdown(ExtensionRegistry* registry) {
DCHECK(extension_registry_observation_.IsObservingSource(registry));
extension_registry_observation_.Reset();
}
void OperationManager::OnBackgroundHostClose(const ExtensionId& extension_id) {
DeleteOperation(extension_id);
}
void OperationManager::OnProcessManagerShutdown(ProcessManager* manager) {
DCHECK(process_manager_observation_.IsObservingSource(manager));
process_manager_observation_.Reset();
}
void OperationManager::OnExtensionProcessTerminated(
const Extension* extension) {
DeleteOperation(extension->id());
}
OperationManager* OperationManager::Get(content::BrowserContext* context) {
return BrowserContextKeyedAPIFactory<OperationManager>::Get(context);
}
static base::LazyInstance<BrowserContextKeyedAPIFactory<OperationManager>>::
DestructorAtExit g_operation_manager_factory = LAZY_INSTANCE_INITIALIZER;
BrowserContextKeyedAPIFactory<OperationManager>*
OperationManager::GetFactoryInstance() {
return g_operation_manager_factory.Pointer();
}
} // namespace image_writer
} // namespace extensions
|