1// Copyright (c) 2011 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.
4
5
6#ifndef CHROME_BROWSER_UI_WEBUI_CHROMEOS_IMAGEBURNER_UI_H_
7#define CHROME_BROWSER_UI_WEBUI_CHROMEOS_IMAGEBURNER_UI_H_
8
9#include <map>
10#include <string>
11#include <vector>
12
13#include "base/file_path.h"
14#include "base/file_util.h"
15#include "base/memory/scoped_ptr.h"
16#include "base/string16.h"
17#include "base/values.h"
18#include "chrome/browser/chromeos/cros/burn_library.h"
19#include "chrome/browser/chromeos/cros/cros_library.h"
20#include "chrome/browser/chromeos/cros/mount_library.h"
21#include "chrome/browser/download/download_item.h"
22#include "chrome/browser/download/download_manager.h"
23#include "chrome/browser/download/download_util.h"
24#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
25#include "content/browser/webui/web_ui.h"
26#include "googleurl/src/gurl.h"
27#include "net/base/file_stream.h"
28#include "ui/base/dragdrop/download_file_interface.h"
29
30template <typename T> struct DefaultSingletonTraits;
31
32class TabContents;
33class ImageBurnTaskProxy;
34class ImageBurnDownloaderTaskProxy;
35
36class ImageBurnDownloader {
37 public:
38
39  class Listener {
40   public:
41    // After download starts download status updates can be followed through
42    // DownloadItem::Observer interface.
43    virtual void OnDownloadStarted(bool success) = 0;
44  };
45
46 private:
47  typedef std::multimap<GURL, Listener*> ListenerMap;
48
49 public:
50  // Returns the singleton instance.
51  static ImageBurnDownloader* GetInstance();
52
53  // Downloads a file from the Internet.
54  // Should be called from UI thread.
55  void DownloadFile(const GURL& url, const FilePath& target_file,
56      TabContents* tab_contents);
57
58  // Creates file stream for a download.
59  // Has to be called from FILE thread.
60  void CreateFileStreamOnFileThread(const GURL& url, const FilePath& file_path,
61      TabContents* tab_contents, ImageBurnDownloaderTaskProxy* task);
62
63  // Gets called after file stream is created and starts download.
64  void OnFileStreamCreatedOnUIThread(const GURL& url,
65      const FilePath& file_path, TabContents* tab_contents,
66      net::FileStream* created_file_stream);
67
68  // Adds an item to list of listeners that wait for confirmation that download
69  // has started.
70  void AddListener(Listener* listener, const GURL& url);
71
72 private:
73  ImageBurnDownloader() {}
74  ~ImageBurnDownloader() {}
75
76  // Let listeners know if download started successfully.
77  void DownloadStarted(bool success, const GURL& url);
78
79  friend struct DefaultSingletonTraits<ImageBurnDownloader>;
80  std::multimap<GURL, Listener*> listeners_;
81
82  DISALLOW_COPY_AND_ASSIGN(ImageBurnDownloader);
83};
84
85class ImageBurnResourceManager
86    : public DownloadManager::Observer,
87      public DownloadItem::Observer,
88      public ImageBurnDownloader::Listener {
89 public:
90
91  class Delegate {
92   public:
93    virtual void OnImageDirCreated(bool success, ImageBurnTaskProxy* task) = 0;
94    virtual void OnImageUrlCreated(GURL* image_url, bool success) = 0;
95  };
96
97  // Returns the singleton instance.
98  static ImageBurnResourceManager* GetInstance();
99
100  // DownloadItem::Observer interface
101  virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE;
102  virtual void OnDownloadOpened(DownloadItem* download) OVERRIDE {}
103
104  // DownloadManager::Observer interface
105  virtual void ModelChanged() OVERRIDE;
106
107  // ImageBurnDownloader::Listener interface.
108  virtual void OnDownloadStarted(bool success) OVERRIDE;
109
110  // Creates URL image should be fetched from.
111  // Must be called from UI thread.
112  void CreateImageUrl(TabContents* tab_content,
113                      Delegate* delegate);
114
115  // Creates directory image will be downloaded to.
116  // Must be called from FILE thread.
117  void CreateImageDir(Delegate* delegate,
118                      ImageBurnTaskProxy* task);
119
120  // Returns directory image should be dopwnloaded to.
121  // The directory has to be previously created.
122  const FilePath& GetImageDir();
123
124  void ConfigFileFetched(bool fetched);
125
126  bool image_download_requested() const { return image_download_requested_; }
127  void set_image_download_requested(bool r) { image_download_requested_ = r; }
128
129  bool download_started() const { return download_started_; }
130  void set_download_started(bool s) { download_started_ = s; }
131
132  bool download_finished() const { return download_finished_; }
133  void SetDownloadFinished(bool finished);
134
135  bool burn_in_progress() const { return burn_in_progress_; }
136  void set_burn_in_progress(bool b) { burn_in_progress_ = b; }
137
138 private:
139  friend struct DefaultSingletonTraits<ImageBurnResourceManager>;
140
141  ImageBurnResourceManager();
142  ~ImageBurnResourceManager();
143
144  FilePath image_dir_;
145  FilePath config_file_path_;
146
147  bool image_download_requested_;
148  bool download_started_;
149  bool download_finished_;
150  bool burn_in_progress_;
151
152  DownloadManager* download_manager_;
153  bool download_item_observer_added_;
154  DownloadItem*  active_download_item_;
155
156  scoped_ptr<GURL> image_url_;
157  GURL config_file_url_;
158  bool config_file_requested_;
159  bool config_file_fetched_;
160  std::vector<Delegate*> downloaders_;
161
162  DISALLOW_COPY_AND_ASSIGN(ImageBurnResourceManager);
163};
164
165class ImageBurnHandler : public WebUIMessageHandler,
166                         public chromeos::MountLibrary::Observer,
167                         public chromeos::BurnLibrary::Observer,
168                         public DownloadManager::Observer,
169                         public DownloadItem::Observer,
170                         public ImageBurnResourceManager::Delegate,
171                         public ImageBurnDownloader::Listener,
172                         public base::SupportsWeakPtr<ImageBurnHandler> {
173 public:
174  explicit ImageBurnHandler(TabContents* contents);
175  virtual ~ImageBurnHandler();
176
177  // WebUIMessageHandler implementation.
178  virtual WebUIMessageHandler* Attach(WebUI* web_ui) OVERRIDE;
179  virtual void RegisterMessages() OVERRIDE;
180
181  // chromeos::MountLibrary::Observer interface.
182  virtual void DiskChanged(chromeos::MountLibraryEventType event,
183                           const chromeos::MountLibrary::Disk* disk) OVERRIDE;
184  virtual void DeviceChanged(chromeos::MountLibraryEventType event,
185                             const std::string& device_path) OVERRIDE;
186
187  // chromeos::BurnLibrary::Observer interface.
188  virtual void ProgressUpdated(chromeos::BurnLibrary* object,
189                               chromeos::BurnEventType evt,
190                               const ImageBurnStatus& status) OVERRIDE;
191
192  // DownloadItem::Observer interface.
193  virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE;
194  virtual void OnDownloadOpened(DownloadItem* download) OVERRIDE;
195
196  // DownloadManager::Observer interface.
197  virtual void ModelChanged() OVERRIDE;
198
199  // ImageBurnResourceManager::Delegate interface.
200  virtual void OnImageDirCreated(bool success, ImageBurnTaskProxy* task)
201      OVERRIDE;
202  virtual void OnImageUrlCreated(GURL* image_url, bool success) OVERRIDE;
203
204  // ImageBurnDownloader::Listener interface.
205  virtual void OnDownloadStarted(bool success) OVERRIDE;
206
207  // Called by ImageBurnTaskProxy.
208  void CreateImageDirOnFileThread(ImageBurnTaskProxy* task);
209  void OnImageDirCreatedOnUIThread(bool success);
210  void BurnImageOnFileThread();
211  void UnzipImageOnFileThread(ImageBurnTaskProxy* task);
212  void UnzipComplete(bool success);
213
214 private:
215  // Callback for the "getRoots" message.
216  void HandleGetRoots(const ListValue* args);
217
218  // Callback for the "downloadImage" message.
219  void HandleDownloadImage(const ListValue* args);
220
221  // Callback for the "burnImage" message.
222  void HandleBurnImage(const ListValue* args);
223
224  // Callback for the "cancelBurnImage" message.
225  void HandleCancelBurnImage(const ListValue* args);
226
227  void DownloadCompleted(bool success);
228
229  void FinalizeBurn(bool successful);
230
231  void UpdateBurnProgress(int64 total_burnt, int64 image_size,
232                          const std::string& path, chromeos::BurnEventType evt);
233  string16 GetBurnProgressText(int64 total_burnt, int64 image_size);
234
235  void UnzipImage();
236  bool UnzipImageImpl();
237
238  // helper functions
239  void ExtractTargetedDeviceSystemPath(const ListValue* list_value);
240
241 private:
242  FilePath zip_image_file_path_;
243  FilePath image_file_path_;
244  FilePath image_target_;
245  GURL* image_download_url_;
246  TabContents* tab_contents_;
247  DownloadManager* download_manager_;
248  bool download_item_observer_added_;
249  DownloadItem*  active_download_item_;
250  ImageBurnResourceManager* resource_manager_;
251
252  DISALLOW_COPY_AND_ASSIGN(ImageBurnHandler);
253};
254
255class ImageBurnUI : public WebUI {
256 public:
257  explicit ImageBurnUI(TabContents* contents);
258
259 private:
260  DISALLOW_COPY_AND_ASSIGN(ImageBurnUI);
261};
262
263#endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_IMAGEBURNER_UI_H_
264