Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: chrome/browser/extensions/api/image_writer_private/operation_manager.cc

Issue 149313003: Significantly cleans up the ImageWriter Operation class and subclasses. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes cross-compilation and test issues. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/lazy_instance.h" 5 #include "base/lazy_instance.h"
6 #include "chrome/browser/browser_process.h" 6 #include "chrome/browser/browser_process.h"
7 #include "chrome/browser/chrome_notification_types.h" 7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/api/image_writer_private/destroy_partitions_ operation.h" 8 #include "chrome/browser/extensions/api/image_writer_private/destroy_partitions_ operation.h"
9 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" 9 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h"
10 #include "chrome/browser/extensions/api/image_writer_private/operation.h" 10 #include "chrome/browser/extensions/api/image_writer_private/operation.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 BrowserThread::PostTask(BrowserThread::FILE, 52 BrowserThread::PostTask(BrowserThread::FILE,
53 FROM_HERE, 53 FROM_HERE,
54 base::Bind(&Operation::Abort, 54 base::Bind(&Operation::Abort,
55 iter->second)); 55 iter->second));
56 } 56 }
57 } 57 }
58 58
59 void OperationManager::StartWriteFromUrl( 59 void OperationManager::StartWriteFromUrl(
60 const ExtensionId& extension_id, 60 const ExtensionId& extension_id,
61 GURL url, 61 GURL url,
62 content::RenderViewHost* rvh,
63 const std::string& hash, 62 const std::string& hash,
64 bool saveImageAsDownload, 63 const std::string& device_path,
65 const std::string& storage_unit_id,
66 const Operation::StartWriteCallback& callback) { 64 const Operation::StartWriteCallback& callback) {
67 OperationMap::iterator existing_operation = operations_.find(extension_id); 65 OperationMap::iterator existing_operation = operations_.find(extension_id);
68 66
69 if (existing_operation != operations_.end()) { 67 if (existing_operation != operations_.end()) {
70 return callback.Run(false, error::kOperationAlreadyInProgress); 68 return callback.Run(false, error::kOperationAlreadyInProgress);
71 } 69 }
72 70
73 scoped_refptr<Operation> operation( 71 scoped_refptr<Operation> operation(
74 new WriteFromUrlOperation(weak_factory_.GetWeakPtr(), 72 new WriteFromUrlOperation(weak_factory_.GetWeakPtr(),
75 extension_id, 73 extension_id,
76 rvh, 74 profile_->GetRequestContext(),
77 url, 75 url,
78 hash, 76 hash,
79 saveImageAsDownload, 77 device_path));
80 storage_unit_id));
81 operations_[extension_id] = operation; 78 operations_[extension_id] = operation;
82 BrowserThread::PostTask(BrowserThread::FILE, 79 BrowserThread::PostTask(BrowserThread::FILE,
83 FROM_HERE, 80 FROM_HERE,
84 base::Bind(&Operation::Start, operation)); 81 base::Bind(&Operation::Start, operation));
85 callback.Run(true, ""); 82 callback.Run(true, "");
86 } 83 }
87 84
88 void OperationManager::StartWriteFromFile( 85 void OperationManager::StartWriteFromFile(
89 const ExtensionId& extension_id, 86 const ExtensionId& extension_id,
90 const base::FilePath& path, 87 const base::FilePath& path,
91 const std::string& storage_unit_id, 88 const std::string& device_path,
92 const Operation::StartWriteCallback& callback) { 89 const Operation::StartWriteCallback& callback) {
93 OperationMap::iterator existing_operation = operations_.find(extension_id); 90 OperationMap::iterator existing_operation = operations_.find(extension_id);
94 91
95 if (existing_operation != operations_.end()) { 92 if (existing_operation != operations_.end()) {
96 return callback.Run(false, error::kOperationAlreadyInProgress); 93 return callback.Run(false, error::kOperationAlreadyInProgress);
97 } 94 }
98 95
99 scoped_refptr<Operation> operation( 96 scoped_refptr<Operation> operation(new WriteFromFileOperation(
100 new WriteFromFileOperation(weak_factory_.GetWeakPtr(), 97 weak_factory_.GetWeakPtr(), extension_id, path, device_path));
101 extension_id,
102 path,
103 storage_unit_id));
104 operations_[extension_id] = operation; 98 operations_[extension_id] = operation;
105 BrowserThread::PostTask(BrowserThread::FILE, 99 BrowserThread::PostTask(BrowserThread::FILE,
106 FROM_HERE, 100 FROM_HERE,
107 base::Bind(&Operation::Start, operation)); 101 base::Bind(&Operation::Start, operation));
108 callback.Run(true, ""); 102 callback.Run(true, "");
109 } 103 }
110 104
111 void OperationManager::CancelWrite( 105 void OperationManager::CancelWrite(
112 const ExtensionId& extension_id, 106 const ExtensionId& extension_id,
113 const Operation::CancelWriteCallback& callback) { 107 const Operation::CancelWriteCallback& callback) {
114 Operation* existing_operation = GetOperation(extension_id); 108 Operation* existing_operation = GetOperation(extension_id);
115 109
116 if (existing_operation == NULL) { 110 if (existing_operation == NULL) {
117 callback.Run(false, error::kNoOperationInProgress); 111 callback.Run(false, error::kNoOperationInProgress);
118 } else { 112 } else {
119 BrowserThread::PostTask(BrowserThread::FILE, 113 BrowserThread::PostTask(BrowserThread::FILE,
120 FROM_HERE, 114 FROM_HERE,
121 base::Bind(&Operation::Cancel, existing_operation)); 115 base::Bind(&Operation::Cancel, existing_operation));
122 DeleteOperation(extension_id); 116 DeleteOperation(extension_id);
123 callback.Run(true, ""); 117 callback.Run(true, "");
124 } 118 }
125 } 119 }
126 120
127 void OperationManager::DestroyPartitions( 121 void OperationManager::DestroyPartitions(
128 const ExtensionId& extension_id, 122 const ExtensionId& extension_id,
129 const std::string& storage_unit_id, 123 const std::string& device_path,
130 const Operation::StartWriteCallback& callback) { 124 const Operation::StartWriteCallback& callback) {
131 OperationMap::iterator existing_operation = operations_.find(extension_id); 125 OperationMap::iterator existing_operation = operations_.find(extension_id);
132 126
133 if (existing_operation != operations_.end()) { 127 if (existing_operation != operations_.end()) {
134 return callback.Run(false, error::kOperationAlreadyInProgress); 128 return callback.Run(false, error::kOperationAlreadyInProgress);
135 } 129 }
136 130
137 scoped_refptr<Operation> operation( 131 scoped_refptr<Operation> operation(new DestroyPartitionsOperation(
138 new DestroyPartitionsOperation(weak_factory_.GetWeakPtr(), 132 weak_factory_.GetWeakPtr(), extension_id, device_path));
139 extension_id,
140 storage_unit_id));
141 operations_[extension_id] = operation; 133 operations_[extension_id] = operation;
142 BrowserThread::PostTask(BrowserThread::FILE, 134 BrowserThread::PostTask(BrowserThread::FILE,
143 FROM_HERE, 135 FROM_HERE,
144 base::Bind(&Operation::Start, operation)); 136 base::Bind(&Operation::Start, operation));
145 callback.Run(true, ""); 137 callback.Run(true, "");
146 } 138 }
147 139
148 void OperationManager::OnProgress(const ExtensionId& extension_id, 140 void OperationManager::OnProgress(const ExtensionId& extension_id,
149 image_writer_api::Stage stage, 141 image_writer_api::Stage stage,
150 int progress) { 142 int progress) {
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
152 DVLOG(2) << "progress - " << stage << " at " << progress << "%";
153 144
154 image_writer_api::ProgressInfo info; 145 image_writer_api::ProgressInfo info;
155 info.stage = stage; 146 info.stage = stage;
156 info.percent_complete = progress; 147 info.percent_complete = progress;
157 148
158 scoped_ptr<base::ListValue> args( 149 scoped_ptr<base::ListValue> args(
159 image_writer_api::OnWriteProgress::Create(info)); 150 image_writer_api::OnWriteProgress::Create(info));
160 scoped_ptr<Event> event(new Event( 151 scoped_ptr<Event> event(new Event(
161 image_writer_api::OnWriteProgress::kEventName, args.Pass())); 152 image_writer_api::OnWriteProgress::kEventName, args.Pass()));
162 153
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 g_factory = LAZY_INSTANCE_INITIALIZER; 248 g_factory = LAZY_INSTANCE_INITIALIZER;
258 249
259 ProfileKeyedAPIFactory<OperationManager>* 250 ProfileKeyedAPIFactory<OperationManager>*
260 OperationManager::GetFactoryInstance() { 251 OperationManager::GetFactoryInstance() {
261 return g_factory.Pointer(); 252 return g_factory.Pointer();
262 } 253 }
263 254
264 255
265 } // namespace image_writer 256 } // namespace image_writer
266 } // namespace extensions 257 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698