| 12
 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
 
 | // Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/lazy_instance.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/image_writer_private/destroy_partitions_operation.h"
#include "chrome/browser/extensions/api/image_writer_private/error_messages.h"
#include "chrome/browser/extensions/api/image_writer_private/operation.h"
#include "chrome/browser/extensions/api/image_writer_private/operation_manager.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/notification_service.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_registry.h"
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_observer_(this),
      weak_factory_(this) {
  extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
  Profile* profile = Profile::FromBrowserContext(browser_context_);
  registrar_.Add(this,
                 chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
                 content::Source<Profile>(profile));
  registrar_.Add(this,
                 chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE,
                 content::Source<Profile>(profile));
  registrar_.Add(this,
                 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
                 content::Source<Profile>(profile));
}
OperationManager::~OperationManager() {
}
void OperationManager::Shutdown() {
  for (OperationMap::iterator iter = operations_.begin();
       iter != operations_.end();
       iter++) {
    BrowserThread::PostTask(BrowserThread::FILE,
                            FROM_HERE,
                            base::Bind(&Operation::Abort,
                                       iter->second));
  }
}
void OperationManager::StartWriteFromUrl(
    const ExtensionId& extension_id,
    GURL url,
    const std::string& hash,
    const std::string& device_path,
    const Operation::StartWriteCallback& callback) {
#if defined(OS_CHROMEOS)
  // Chrome OS can only support a single operation at a time.
  if (operations_.size() > 0) {
#else
  OperationMap::iterator existing_operation = operations_.find(extension_id);
  if (existing_operation != operations_.end()) {
#endif
    return callback.Run(false, error::kOperationAlreadyInProgress);
  }
  scoped_refptr<Operation> operation(
      new WriteFromUrlOperation(weak_factory_.GetWeakPtr(),
                                extension_id,
                                browser_context_->GetRequestContext(),
                                url,
                                hash,
                                device_path));
  operations_[extension_id] = operation;
  BrowserThread::PostTask(BrowserThread::FILE,
                          FROM_HERE,
                          base::Bind(&Operation::Start, operation));
  callback.Run(true, "");
}
void OperationManager::StartWriteFromFile(
    const ExtensionId& extension_id,
    const base::FilePath& path,
    const std::string& device_path,
    const Operation::StartWriteCallback& callback) {
#if defined(OS_CHROMEOS)
  // Chrome OS can only support a single operation at a time.
  if (operations_.size() > 0) {
#else
  OperationMap::iterator existing_operation = operations_.find(extension_id);
  if (existing_operation != operations_.end()) {
#endif
    return callback.Run(false, error::kOperationAlreadyInProgress);
  }
  scoped_refptr<Operation> operation(new WriteFromFileOperation(
      weak_factory_.GetWeakPtr(), extension_id, path, device_path));
  operations_[extension_id] = operation;
  BrowserThread::PostTask(BrowserThread::FILE,
                          FROM_HERE,
                          base::Bind(&Operation::Start, operation));
  callback.Run(true, "");
}
void OperationManager::CancelWrite(
    const ExtensionId& extension_id,
    const Operation::CancelWriteCallback& callback) {
  Operation* existing_operation = GetOperation(extension_id);
  if (existing_operation == NULL) {
    callback.Run(false, error::kNoOperationInProgress);
  } else {
    BrowserThread::PostTask(BrowserThread::FILE,
                            FROM_HERE,
                            base::Bind(&Operation::Cancel, existing_operation));
    DeleteOperation(extension_id);
    callback.Run(true, "");
  }
}
void OperationManager::DestroyPartitions(
    const ExtensionId& extension_id,
    const std::string& device_path,
    const Operation::StartWriteCallback& callback) {
  OperationMap::iterator existing_operation = operations_.find(extension_id);
  if (existing_operation != operations_.end()) {
    return callback.Run(false, error::kOperationAlreadyInProgress);
  }
  scoped_refptr<Operation> operation(new DestroyPartitionsOperation(
      weak_factory_.GetWeakPtr(), extension_id, device_path));
  operations_[extension_id] = operation;
  BrowserThread::PostTask(BrowserThread::FILE,
                          FROM_HERE,
                          base::Bind(&Operation::Start, operation));
  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;
  scoped_ptr<base::ListValue> args(
      image_writer_api::OnWriteProgress::Create(info));
  scoped_ptr<Event> event(new Event(
      image_writer_api::OnWriteProgress::kEventName, args.Pass()));
  EventRouter::Get(browser_context_)
      ->DispatchEventToExtension(extension_id, event.Pass());
}
void OperationManager::OnComplete(const ExtensionId& extension_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  scoped_ptr<base::ListValue> args(image_writer_api::OnWriteComplete::Create());
  scoped_ptr<Event> event(new Event(
      image_writer_api::OnWriteComplete::kEventName, args.Pass()));
  EventRouter::Get(browser_context_)
      ->DispatchEventToExtension(extension_id, event.Pass());
  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;
  scoped_ptr<base::ListValue> args(
      image_writer_api::OnWriteError::Create(info, error_message));
  scoped_ptr<Event> event(new Event(
      image_writer_api::OnWriteError::kEventName, args.Pass()));
  EventRouter::Get(browser_context_)
      ->DispatchEventToExtension(extension_id, event.Pass());
  DeleteOperation(extension_id);
}
Operation* OperationManager::GetOperation(const ExtensionId& extension_id) {
  OperationMap::iterator existing_operation = operations_.find(extension_id);
  if (existing_operation == operations_.end())
    return NULL;
  return existing_operation->second.get();
}
void OperationManager::DeleteOperation(const ExtensionId& extension_id) {
  OperationMap::iterator 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,
    UnloadedExtensionInfo::Reason reason) {
  DeleteOperation(extension->id());
}
void OperationManager::Observe(int type,
                               const content::NotificationSource& source,
                               const content::NotificationDetails& details) {
  switch (type) {
    case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
      DeleteOperation(content::Details<const Extension>(details).ptr()->id());
      break;
    }
    case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: {
      DeleteOperation(
        content::Details<ExtensionHost>(details)->extension()->id());
      break;
    }
    case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
      DeleteOperation(
        content::Details<ExtensionHost>(details)->extension()->id());
      break;
    }
    default: {
      NOTREACHED();
      break;
    }
  }
}
OperationManager* OperationManager::Get(content::BrowserContext* context) {
  return BrowserContextKeyedAPIFactory<OperationManager>::Get(context);
}
static base::LazyInstance<BrowserContextKeyedAPIFactory<OperationManager> >
    g_factory = LAZY_INSTANCE_INITIALIZER;
BrowserContextKeyedAPIFactory<OperationManager>*
OperationManager::GetFactoryInstance() {
  return g_factory.Pointer();
}
}  // namespace image_writer
}  // namespace extensions
 |