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 #ifndef CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_MANAGER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_MANAGER_H_
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/scoped_observer.h"
14 #include "base/stl_util.h"
15 #include "chrome/browser/extensions/api/image_writer_private/image_writer_private_api.h"
16 #include "chrome/browser/extensions/api/image_writer_private/operation.h"
17 #include "chrome/common/extensions/api/image_writer_private.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "extensions/browser/browser_context_keyed_api_factory.h"
21 #include "extensions/browser/extension_registry_observer.h"
24 namespace image_writer_api
= extensions::api::image_writer_private
;
32 namespace extensions
{
33 class ExtensionRegistry
;
35 namespace image_writer
{
39 // Manages image writer operations for the current profile. Including clean-up
40 // and message routing.
41 class OperationManager
: public BrowserContextKeyedAPI
,
42 public content::NotificationObserver
,
43 public extensions::ExtensionRegistryObserver
,
44 public base::SupportsWeakPtr
<OperationManager
> {
46 typedef std::string ExtensionId
;
48 explicit OperationManager(content::BrowserContext
* context
);
49 ~OperationManager() override
;
51 void Shutdown() override
;
53 // Starts a WriteFromUrl operation.
54 void StartWriteFromUrl(const ExtensionId
& extension_id
,
56 const std::string
& hash
,
57 const std::string
& device_path
,
58 const Operation::StartWriteCallback
& callback
);
60 // Starts a WriteFromFile operation.
61 void StartWriteFromFile(const ExtensionId
& extension_id
,
62 const base::FilePath
& path
,
63 const std::string
& device_path
,
64 const Operation::StartWriteCallback
& callback
);
66 // Cancels the extensions current operation if any.
67 void CancelWrite(const ExtensionId
& extension_id
,
68 const Operation::CancelWriteCallback
& callback
);
70 // Starts a write that removes the partition table.
71 void DestroyPartitions(const ExtensionId
& extension_id
,
72 const std::string
& device_path
,
73 const Operation::StartWriteCallback
& callback
);
75 // Callback for progress events.
76 virtual void OnProgress(const ExtensionId
& extension_id
,
77 image_writer_api::Stage stage
,
79 // Callback for completion events.
80 virtual void OnComplete(const ExtensionId
& extension_id
);
82 // Callback for error events.
83 virtual void OnError(const ExtensionId
& extension_id
,
84 image_writer_api::Stage stage
,
86 const std::string
& error_message
);
88 // BrowserContextKeyedAPI
89 static BrowserContextKeyedAPIFactory
<OperationManager
>* GetFactoryInstance();
90 static OperationManager
* Get(content::BrowserContext
* context
);
94 static const char* service_name() {
95 return "OperationManager";
98 // NotificationObserver implementation.
99 void Observe(int type
,
100 const content::NotificationSource
& source
,
101 const content::NotificationDetails
& details
) override
;
103 // ExtensionRegistryObserver implementation.
104 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
105 const Extension
* extension
,
106 UnloadedExtensionInfo::Reason reason
) override
;
108 Operation
* GetOperation(const ExtensionId
& extension_id
);
109 void DeleteOperation(const ExtensionId
& extension_id
);
111 friend class BrowserContextKeyedAPIFactory
<OperationManager
>;
112 typedef std::map
<ExtensionId
, scoped_refptr
<Operation
> > OperationMap
;
114 content::BrowserContext
* browser_context_
;
115 OperationMap operations_
;
116 content::NotificationRegistrar registrar_
;
118 // Listen to extension unloaded notification.
119 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
120 extension_registry_observer_
;
122 base::WeakPtrFactory
<OperationManager
> weak_factory_
;
124 DISALLOW_COPY_AND_ASSIGN(OperationManager
);
127 } // namespace image_writer
128 } // namespace extensions
130 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_MANAGER_H_