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
3 // found in the LICENSE file.
5 #include "base/lazy_instance.h"
6 #include "chrome/browser/browser_process.h"
7 #include "chrome/browser/extensions/api/image_writer_private/destroy_partitions_operation.h"
8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h"
9 #include "chrome/browser/extensions/api/image_writer_private/operation.h"
10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
11 #include "chrome/browser/extensions/api/image_writer_private/write_from_file_operation.h"
12 #include "chrome/browser/extensions/api/image_writer_private/write_from_url_operation.h"
13 #include "chrome/browser/extensions/event_router_forwarder.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/notification_service.h"
18 #include "extensions/browser/event_router.h"
19 #include "extensions/browser/extension_host.h"
20 #include "extensions/browser/extension_registry.h"
21 #include "extensions/browser/notification_types.h"
23 namespace image_writer_api
= extensions::api::image_writer_private
;
25 namespace extensions
{
26 namespace image_writer
{
28 using content::BrowserThread
;
30 OperationManager::OperationManager(content::BrowserContext
* context
)
31 : browser_context_(context
),
32 extension_registry_observer_(this),
34 extension_registry_observer_
.Add(ExtensionRegistry::Get(browser_context_
));
35 Profile
* profile
= Profile::FromBrowserContext(browser_context_
);
37 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED
,
38 content::Source
<Profile
>(profile
));
40 extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE
,
41 content::Source
<Profile
>(profile
));
43 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
44 content::Source
<Profile
>(profile
));
47 OperationManager::~OperationManager() {
50 void OperationManager::Shutdown() {
51 for (OperationMap::iterator iter
= operations_
.begin();
52 iter
!= operations_
.end();
54 BrowserThread::PostTask(BrowserThread::FILE,
56 base::Bind(&Operation::Abort
,
61 void OperationManager::StartWriteFromUrl(
62 const ExtensionId
& extension_id
,
64 const std::string
& hash
,
65 const std::string
& device_path
,
66 const Operation::StartWriteCallback
& callback
) {
67 #if defined(OS_CHROMEOS)
68 // Chrome OS can only support a single operation at a time.
69 if (operations_
.size() > 0) {
71 OperationMap::iterator existing_operation
= operations_
.find(extension_id
);
73 if (existing_operation
!= operations_
.end()) {
75 return callback
.Run(false, error::kOperationAlreadyInProgress
);
78 scoped_refptr
<Operation
> operation(
79 new WriteFromUrlOperation(weak_factory_
.GetWeakPtr(),
81 browser_context_
->GetRequestContext(),
85 operations_
[extension_id
] = operation
;
86 BrowserThread::PostTask(BrowserThread::FILE,
88 base::Bind(&Operation::Start
, operation
));
89 callback
.Run(true, "");
92 void OperationManager::StartWriteFromFile(
93 const ExtensionId
& extension_id
,
94 const base::FilePath
& path
,
95 const std::string
& device_path
,
96 const Operation::StartWriteCallback
& callback
) {
97 #if defined(OS_CHROMEOS)
98 // Chrome OS can only support a single operation at a time.
99 if (operations_
.size() > 0) {
101 OperationMap::iterator existing_operation
= operations_
.find(extension_id
);
103 if (existing_operation
!= operations_
.end()) {
105 return callback
.Run(false, error::kOperationAlreadyInProgress
);
108 scoped_refptr
<Operation
> operation(new WriteFromFileOperation(
109 weak_factory_
.GetWeakPtr(), extension_id
, path
, device_path
));
110 operations_
[extension_id
] = operation
;
111 BrowserThread::PostTask(BrowserThread::FILE,
113 base::Bind(&Operation::Start
, operation
));
114 callback
.Run(true, "");
117 void OperationManager::CancelWrite(
118 const ExtensionId
& extension_id
,
119 const Operation::CancelWriteCallback
& callback
) {
120 Operation
* existing_operation
= GetOperation(extension_id
);
122 if (existing_operation
== NULL
) {
123 callback
.Run(false, error::kNoOperationInProgress
);
125 BrowserThread::PostTask(BrowserThread::FILE,
127 base::Bind(&Operation::Cancel
, existing_operation
));
128 DeleteOperation(extension_id
);
129 callback
.Run(true, "");
133 void OperationManager::DestroyPartitions(
134 const ExtensionId
& extension_id
,
135 const std::string
& device_path
,
136 const Operation::StartWriteCallback
& callback
) {
137 OperationMap::iterator existing_operation
= operations_
.find(extension_id
);
139 if (existing_operation
!= operations_
.end()) {
140 return callback
.Run(false, error::kOperationAlreadyInProgress
);
143 scoped_refptr
<Operation
> operation(new DestroyPartitionsOperation(
144 weak_factory_
.GetWeakPtr(), extension_id
, device_path
));
145 operations_
[extension_id
] = operation
;
146 BrowserThread::PostTask(BrowserThread::FILE,
148 base::Bind(&Operation::Start
, operation
));
149 callback
.Run(true, "");
152 void OperationManager::OnProgress(const ExtensionId
& extension_id
,
153 image_writer_api::Stage stage
,
155 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
157 image_writer_api::ProgressInfo info
;
159 info
.percent_complete
= progress
;
161 scoped_ptr
<base::ListValue
> args(
162 image_writer_api::OnWriteProgress::Create(info
));
163 scoped_ptr
<Event
> event(new Event(
164 image_writer_api::OnWriteProgress::kEventName
, args
.Pass()));
166 EventRouter::Get(browser_context_
)
167 ->DispatchEventToExtension(extension_id
, event
.Pass());
170 void OperationManager::OnComplete(const ExtensionId
& extension_id
) {
171 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
173 scoped_ptr
<base::ListValue
> args(image_writer_api::OnWriteComplete::Create());
174 scoped_ptr
<Event
> event(new Event(
175 image_writer_api::OnWriteComplete::kEventName
, args
.Pass()));
177 EventRouter::Get(browser_context_
)
178 ->DispatchEventToExtension(extension_id
, event
.Pass());
180 DeleteOperation(extension_id
);
183 void OperationManager::OnError(const ExtensionId
& extension_id
,
184 image_writer_api::Stage stage
,
186 const std::string
& error_message
) {
187 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
188 image_writer_api::ProgressInfo info
;
190 DLOG(ERROR
) << "ImageWriter error: " << error_message
;
193 info
.percent_complete
= progress
;
195 scoped_ptr
<base::ListValue
> args(
196 image_writer_api::OnWriteError::Create(info
, error_message
));
197 scoped_ptr
<Event
> event(new Event(
198 image_writer_api::OnWriteError::kEventName
, args
.Pass()));
200 EventRouter::Get(browser_context_
)
201 ->DispatchEventToExtension(extension_id
, event
.Pass());
203 DeleteOperation(extension_id
);
206 Operation
* OperationManager::GetOperation(const ExtensionId
& extension_id
) {
207 OperationMap::iterator existing_operation
= operations_
.find(extension_id
);
209 if (existing_operation
== operations_
.end())
211 return existing_operation
->second
.get();
214 void OperationManager::DeleteOperation(const ExtensionId
& extension_id
) {
215 OperationMap::iterator existing_operation
= operations_
.find(extension_id
);
216 if (existing_operation
!= operations_
.end()) {
217 operations_
.erase(existing_operation
);
221 void OperationManager::OnExtensionUnloaded(
222 content::BrowserContext
* browser_context
,
223 const Extension
* extension
,
224 UnloadedExtensionInfo::Reason reason
) {
225 DeleteOperation(extension
->id());
228 void OperationManager::Observe(int type
,
229 const content::NotificationSource
& source
,
230 const content::NotificationDetails
& details
) {
232 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED
: {
233 DeleteOperation(content::Details
<const Extension
>(details
).ptr()->id());
236 case extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE
: {
238 content::Details
<ExtensionHost
>(details
)->extension()->id());
241 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED
: {
243 content::Details
<ExtensionHost
>(details
)->extension()->id());
253 OperationManager
* OperationManager::Get(content::BrowserContext
* context
) {
254 return BrowserContextKeyedAPIFactory
<OperationManager
>::Get(context
);
257 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<OperationManager
> >
258 g_factory
= LAZY_INSTANCE_INITIALIZER
;
260 BrowserContextKeyedAPIFactory
<OperationManager
>*
261 OperationManager::GetFactoryInstance() {
262 return g_factory
.Pointer();
266 } // namespace image_writer
267 } // namespace extensions