save_package.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2006-2008 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#ifndef CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_
6#define CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_
7
8#include <queue>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/file_path.h"
14#include "base/hash_tables.h"
15#include "base/ref_counted.h"
16#include "base/task.h"
17#include "chrome/browser/renderer_host/render_view_host_delegate.h"
18#include "chrome/browser/shell_dialogs.h"
19#include "googleurl/src/gurl.h"
20
21class SaveFileManager;
22class SaveItem;
23class SavePackage;
24class DownloadItem;
25class DownloadManager;
26class GURL;
27class MessageLoop;
28class PrefService;
29class Profile;
30class TabContents;
31class URLRequestContextGetter;
32class TabContents;
33
34namespace base {
35class Thread;
36class Time;
37}
38
39struct SaveFileCreateInfo;
40struct SavePackageParam;
41
42// The SavePackage object manages the process of saving a page as only-html or
43// complete-html and providing the information for displaying saving status.
44// Saving page as only-html means means that we save web page to a single HTML
45// file regardless internal sub resources and sub frames.
46// Saving page as complete-html page means we save not only the main html file
47// the user told it to save but also a directory for the auxiliary files such
48// as all sub-frame html files, image files, css files and js files.
49//
50// Each page saving job may include one or multiple files which need to be
51// saved. Each file is represented by a SaveItem, and all SaveItems are owned
52// by the SavePackage. SaveItems are created when a user initiates a page
53// saving job, and exist for the duration of one tab's life time.
54class SavePackage : public base::RefCountedThreadSafe<SavePackage>,
55                    public RenderViewHostDelegate::Save,
56                    public SelectFileDialog::Listener {
57 public:
58  enum SavePackageType {
59    // The value of the save type before its set by the user.
60    SAVE_TYPE_UNKNOWN = -1,
61    // User chose to save only the HTML of the page.
62    SAVE_AS_ONLY_HTML = 0,
63    // User chose to save complete-html page.
64    SAVE_AS_COMPLETE_HTML = 1
65  };
66
67  enum WaitState {
68    // State when created but not initialized.
69    INITIALIZE = 0,
70    // State when after initializing, but not yet saving.
71    START_PROCESS,
72    // Waiting on a list of savable resources from the backend.
73    RESOURCES_LIST,
74    // Waiting for data sent from net IO or from file system.
75    NET_FILES,
76    // Waiting for html DOM data sent from render process.
77    HTML_DATA,
78    // Saving page finished successfully.
79    SUCCESSFUL,
80    // Failed to save page.
81    FAILED
82  };
83
84  // Constructor for user initiated page saving. This constructor results in a
85  // SavePackage that will generate and sanitize a suggested name for the user
86  // in the "Save As" dialog box.
87  explicit SavePackage(TabContents* web_content);
88
89  // This contructor is used only for testing. We can bypass the file and
90  // directory name generation / sanitization by providing well known paths
91  // better suited for tests.
92  SavePackage(TabContents* web_content,
93              SavePackageType save_type,
94              const FilePath& file_full_path,
95              const FilePath& directory_full_path);
96
97  // Initialize the SavePackage. Returns true if it initializes properly.
98  // Need to make sure that this method must be called in the UI thread because
99  // using g_browser_process on a non-UI thread can cause crashes during
100  // shutdown.
101  bool Init();
102
103  void Cancel(bool user_action);
104
105  void Finish();
106
107  // Notifications sent from the file thread to the UI thread.
108  void StartSave(const SaveFileCreateInfo* info);
109  bool UpdateSaveProgress(int32 save_id, int64 size, bool write_success);
110  void SaveFinished(int32 save_id, int64 size, bool is_success);
111  void SaveFailed(const GURL& save_url);
112  void SaveCanceled(SaveItem* save_item);
113
114  // Rough percent complete, -1 means we don't know (since we didn't receive a
115  // total size).
116  int PercentComplete();
117
118  // Show or Open a saved page via the Windows shell.
119  void ShowDownloadInShell();
120
121  bool canceled() const { return user_canceled_ || disk_error_occurred_; }
122  bool finished() const { return finished_; }
123  SavePackageType save_type() const { return save_type_; }
124  int tab_id() const { return tab_id_; }
125  int id() const { return unique_id_; }
126
127  void GetSaveInfo();
128  void ContinueGetSaveInfo(FilePath save_dir);
129  void ContinueSave(SavePackageParam* param,
130                    const FilePath& final_name,
131                    int index);
132
133  // RenderViewHostDelegate::Save ----------------------------------------------
134
135  // Process all of the current page's savable links of subresources, resources
136  // referrers and frames (including the main frame and subframes) from the
137  // render view host.
138  virtual void OnReceivedSavableResourceLinksForCurrentPage(
139      const std::vector<GURL>& resources_list,
140      const std::vector<GURL>& referrers_list,
141      const std::vector<GURL>& frames_list);
142
143  // Process the serialized html content data of a specified web page
144  // gotten from render process.
145  virtual void OnReceivedSerializedHtmlData(const GURL& frame_url,
146                                            const std::string& data,
147                                            int32 status);
148
149  // Statics -------------------------------------------------------------------
150
151  // Used to disable prompting the user for a directory/filename of the saved
152  // web page.  This is available for testing.
153  static void SetShouldPromptUser(bool should_prompt);
154
155  // Check whether we can do the saving page operation for the specified URL.
156  static bool IsSavableURL(const GURL& url);
157
158  // Check whether we can do the saving page operation for the contents which
159  // have the specified MIME type.
160  static bool IsSavableContents(const std::string& contents_mime_type);
161
162  // SelectFileDialog::Listener ------------------------------------------------
163  virtual void FileSelected(const FilePath& path, int index, void* params);
164  virtual void FileSelectionCanceled(void* params);
165
166 private:
167  friend class base::RefCountedThreadSafe<SavePackage>;
168
169  // For testing only.
170  SavePackage(TabContents* tab_contents,
171              const FilePath& file_full_path,
172              const FilePath& directory_full_path);
173
174  ~SavePackage();
175
176  // Notes from Init() above applies here as well.
177  void InternalInit();
178
179  void Stop();
180  void CheckFinish();
181  void SaveNextFile(bool process_all_remainder_items);
182  void DoSavingProcess();
183
184  // Create a file name based on the response from the server.
185  bool GenerateFileName(const std::string& disposition,
186                        const GURL& url,
187                        bool need_html_ext,
188                        FilePath::StringType* generated_name);
189
190  // Get all savable resource links from current web page, include main
191  // frame and sub-frame.
192  void GetAllSavableResourceLinksForCurrentPage();
193  // Get html data by serializing all frames of current page with lists
194  // which contain all resource links that have local copy.
195  void GetSerializedHtmlDataForCurrentPageWithLocalLinks();
196
197  SaveItem* LookupItemInProcessBySaveId(int32 save_id);
198  void PutInProgressItemToSavedMap(SaveItem* save_item);
199
200  // Retrieves the URL to be saved from tab_contents_ variable.
201  GURL GetUrlToBeSaved();
202
203
204  typedef base::hash_map<std::string, SaveItem*> SaveUrlItemMap;
205  // in_progress_items_ is map of all saving job in in-progress state.
206  SaveUrlItemMap in_progress_items_;
207  // saved_failed_items_ is map of all saving job which are failed.
208  SaveUrlItemMap saved_failed_items_;
209
210  // The number of in process SaveItems.
211  int in_process_count() const {
212    return static_cast<int>(in_progress_items_.size());
213  }
214
215  // The number of all SaveItems which have completed, including success items
216  // and failed items.
217  int completed_count() const {
218    return static_cast<int>(saved_success_items_.size() +
219                            saved_failed_items_.size());
220  }
221
222  // Retrieve the preference for the directory to save pages to.
223  static FilePath GetSaveDirPreference(PrefService* prefs);
224
225  // Helper function for preparing suggested name for the SaveAs Dialog. The
226  // suggested name is determined by the web document's title.
227  static FilePath GetSuggestedNameForSaveAs(const FilePath& name,
228      bool can_save_as_complete,
229      const FilePath::StringType& contents_mime_type);
230
231  // Ensures that the file name has a proper extension for HTML by adding ".htm"
232  // if necessary.
233  static FilePath EnsureHtmlExtension(const FilePath& name);
234
235  // Ensures that the file name has a proper extension for supported formats
236  // if necessary.
237  static FilePath EnsureMimeExtension(const FilePath& name,
238      const FilePath::StringType& contents_mime_type);
239
240  // Returns extension for supported MIME types (for example, for "text/plain"
241  // it returns "txt").
242  static const FilePath::CharType* ExtensionForMimeType(
243      const FilePath::StringType& contents_mime_type);
244
245  typedef std::queue<SaveItem*> SaveItemQueue;
246  // A queue for items we are about to start saving.
247  SaveItemQueue waiting_item_queue_;
248
249  typedef base::hash_map<int32, SaveItem*> SavedItemMap;
250  // saved_success_items_ is map of all saving job which are successfully saved.
251  SavedItemMap saved_success_items_;
252
253  // The request context which provides application-specific context for
254  // URLRequest instances.
255  scoped_refptr<URLRequestContextGetter> request_context_getter_;
256
257  // Non-owning pointer for handling file writing on the file thread.
258  SaveFileManager* file_manager_;
259
260  TabContents* tab_contents_;
261
262  // We use a fake DownloadItem here in order to reuse the DownloadItemView.
263  // This class owns the pointer.
264  DownloadItem* download_;
265
266  // The URL of the page the user wants to save.
267  GURL page_url_;
268  FilePath saved_main_file_path_;
269  FilePath saved_main_directory_path_;
270
271  // Indicates whether the actual saving job is finishing or not.
272  bool finished_;
273
274  // Indicates whether user canceled the saving job.
275  bool user_canceled_;
276
277  // Indicates whether user get disk error.
278  bool disk_error_occurred_;
279
280  // Type about saving page as only-html or complete-html.
281  SavePackageType save_type_;
282
283  // Number of all need to be saved resources.
284  size_t all_save_items_count_;
285
286  typedef base::hash_set<FilePath::StringType> FileNameSet;
287  // This set is used to eliminate duplicated file names in saving directory.
288  FileNameSet file_name_set_;
289
290  typedef base::hash_map<FilePath::StringType, uint32> FileNameCountMap;
291  // This map is used to track serial number for specified filename.
292  FileNameCountMap file_name_count_map_;
293
294  // Indicates current waiting state when SavePackage try to get something
295  // from outside.
296  WaitState wait_state_;
297
298  // Since for one tab, it can only have one SavePackage in same time.
299  // Now we actually use render_process_id as tab's unique id.
300  const int tab_id_;
301
302  // Unique ID for this SavePackage.
303  const int unique_id_;
304
305  // For managing select file dialogs.
306  scoped_refptr<SelectFileDialog> select_file_dialog_;
307
308  friend class SavePackageTest;
309
310  ScopedRunnableMethodFactory<SavePackage> method_factory_;
311
312  DISALLOW_COPY_AND_ASSIGN(SavePackage);
313};
314
315#endif  // CHROME_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_
316