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
|
// Copyright (c) 2011 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.
#ifndef CHROME_BROWSER_UI_WEBUI_CHROMEOS_IMAGEBURNER_UI_H_
#define CHROME_BROWSER_UI_WEBUI_CHROMEOS_IMAGEBURNER_UI_H_
#include <map>
#include <string>
#include <vector>
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
#include "base/values.h"
#include "chrome/browser/chromeos/cros/burn_library.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/cros/mount_library.h"
#include "chrome/browser/download/download_item.h"
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
#include "content/browser/webui/web_ui.h"
#include "googleurl/src/gurl.h"
#include "net/base/file_stream.h"
#include "ui/base/dragdrop/download_file_interface.h"
template <typename T> struct DefaultSingletonTraits;
class TabContents;
class ImageBurnTaskProxy;
class ImageBurnDownloaderTaskProxy;
class ImageBurnDownloader {
public:
class Listener {
public:
// After download starts download status updates can be followed through
// DownloadItem::Observer interface.
virtual void OnDownloadStarted(bool success) = 0;
};
private:
typedef std::multimap<GURL, Listener*> ListenerMap;
public:
// Returns the singleton instance.
static ImageBurnDownloader* GetInstance();
// Downloads a file from the Internet.
// Should be called from UI thread.
void DownloadFile(const GURL& url, const FilePath& target_file,
TabContents* tab_contents);
// Creates file stream for a download.
// Has to be called from FILE thread.
void CreateFileStreamOnFileThread(const GURL& url, const FilePath& file_path,
TabContents* tab_contents, ImageBurnDownloaderTaskProxy* task);
// Gets called after file stream is created and starts download.
void OnFileStreamCreatedOnUIThread(const GURL& url,
const FilePath& file_path, TabContents* tab_contents,
net::FileStream* created_file_stream);
// Adds an item to list of listeners that wait for confirmation that download
// has started.
void AddListener(Listener* listener, const GURL& url);
private:
ImageBurnDownloader() {}
~ImageBurnDownloader() {}
// Let listeners know if download started successfully.
void DownloadStarted(bool success, const GURL& url);
friend struct DefaultSingletonTraits<ImageBurnDownloader>;
std::multimap<GURL, Listener*> listeners_;
DISALLOW_COPY_AND_ASSIGN(ImageBurnDownloader);
};
class ImageBurnResourceManager
: public DownloadManager::Observer,
public DownloadItem::Observer,
public ImageBurnDownloader::Listener {
public:
class Delegate {
public:
virtual void OnImageDirCreated(bool success, ImageBurnTaskProxy* task) = 0;
virtual void OnImageUrlCreated(GURL* image_url, bool success) = 0;
};
// Returns the singleton instance.
static ImageBurnResourceManager* GetInstance();
// DownloadItem::Observer interface
virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE;
virtual void OnDownloadOpened(DownloadItem* download) OVERRIDE {}
// DownloadManager::Observer interface
virtual void ModelChanged() OVERRIDE;
// ImageBurnDownloader::Listener interface.
virtual void OnDownloadStarted(bool success) OVERRIDE;
// Creates URL image should be fetched from.
// Must be called from UI thread.
void CreateImageUrl(TabContents* tab_content,
Delegate* delegate);
// Creates directory image will be downloaded to.
// Must be called from FILE thread.
void CreateImageDir(Delegate* delegate,
ImageBurnTaskProxy* task);
// Returns directory image should be dopwnloaded to.
// The directory has to be previously created.
const FilePath& GetImageDir();
void ConfigFileFetched(bool fetched);
bool image_download_requested() const { return image_download_requested_; }
void set_image_download_requested(bool r) { image_download_requested_ = r; }
bool download_started() const { return download_started_; }
void set_download_started(bool s) { download_started_ = s; }
bool download_finished() const { return download_finished_; }
void SetDownloadFinished(bool finished);
bool burn_in_progress() const { return burn_in_progress_; }
void set_burn_in_progress(bool b) { burn_in_progress_ = b; }
private:
friend struct DefaultSingletonTraits<ImageBurnResourceManager>;
ImageBurnResourceManager();
~ImageBurnResourceManager();
FilePath image_dir_;
FilePath config_file_path_;
bool image_download_requested_;
bool download_started_;
bool download_finished_;
bool burn_in_progress_;
DownloadManager* download_manager_;
bool download_item_observer_added_;
DownloadItem* active_download_item_;
scoped_ptr<GURL> image_url_;
GURL config_file_url_;
bool config_file_requested_;
bool config_file_fetched_;
std::vector<Delegate*> downloaders_;
DISALLOW_COPY_AND_ASSIGN(ImageBurnResourceManager);
};
class ImageBurnHandler : public WebUIMessageHandler,
public chromeos::MountLibrary::Observer,
public chromeos::BurnLibrary::Observer,
public DownloadManager::Observer,
public DownloadItem::Observer,
public ImageBurnResourceManager::Delegate,
public ImageBurnDownloader::Listener,
public base::SupportsWeakPtr<ImageBurnHandler> {
public:
explicit ImageBurnHandler(TabContents* contents);
virtual ~ImageBurnHandler();
// WebUIMessageHandler implementation.
virtual WebUIMessageHandler* Attach(WebUI* web_ui) OVERRIDE;
virtual void RegisterMessages() OVERRIDE;
// chromeos::MountLibrary::Observer interface.
virtual void DiskChanged(chromeos::MountLibraryEventType event,
const chromeos::MountLibrary::Disk* disk) OVERRIDE;
virtual void DeviceChanged(chromeos::MountLibraryEventType event,
const std::string& device_path) OVERRIDE;
// chromeos::BurnLibrary::Observer interface.
virtual void ProgressUpdated(chromeos::BurnLibrary* object,
chromeos::BurnEventType evt,
const ImageBurnStatus& status) OVERRIDE;
// DownloadItem::Observer interface.
virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE;
virtual void OnDownloadOpened(DownloadItem* download) OVERRIDE;
// DownloadManager::Observer interface.
virtual void ModelChanged() OVERRIDE;
// ImageBurnResourceManager::Delegate interface.
virtual void OnImageDirCreated(bool success, ImageBurnTaskProxy* task)
OVERRIDE;
virtual void OnImageUrlCreated(GURL* image_url, bool success) OVERRIDE;
// ImageBurnDownloader::Listener interface.
virtual void OnDownloadStarted(bool success) OVERRIDE;
// Called by ImageBurnTaskProxy.
void CreateImageDirOnFileThread(ImageBurnTaskProxy* task);
void OnImageDirCreatedOnUIThread(bool success);
void BurnImageOnFileThread();
void UnzipImageOnFileThread(ImageBurnTaskProxy* task);
void UnzipComplete(bool success);
private:
// Callback for the "getRoots" message.
void HandleGetRoots(const ListValue* args);
// Callback for the "downloadImage" message.
void HandleDownloadImage(const ListValue* args);
// Callback for the "burnImage" message.
void HandleBurnImage(const ListValue* args);
// Callback for the "cancelBurnImage" message.
void HandleCancelBurnImage(const ListValue* args);
void DownloadCompleted(bool success);
void FinalizeBurn(bool successful);
void UpdateBurnProgress(int64 total_burnt, int64 image_size,
const std::string& path, chromeos::BurnEventType evt);
string16 GetBurnProgressText(int64 total_burnt, int64 image_size);
void UnzipImage();
bool UnzipImageImpl();
// helper functions
void ExtractTargetedDeviceSystemPath(const ListValue* list_value);
private:
FilePath zip_image_file_path_;
FilePath image_file_path_;
FilePath image_target_;
GURL* image_download_url_;
TabContents* tab_contents_;
DownloadManager* download_manager_;
bool download_item_observer_added_;
DownloadItem* active_download_item_;
ImageBurnResourceManager* resource_manager_;
DISALLOW_COPY_AND_ASSIGN(ImageBurnHandler);
};
class ImageBurnUI : public WebUI {
public:
explicit ImageBurnUI(TabContents* contents);
private:
DISALLOW_COPY_AND_ASSIGN(ImageBurnUI);
};
#endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_IMAGEBURNER_UI_H_
|