Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: content/browser/download/save_package.h

Issue 10069014: Save Page As MHTML (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: avoid re-entering Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ 5 #ifndef CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_
6 #define CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ 6 #define CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_
7 #pragma once 7 #pragma once
8 8
9 #include <queue> 9 #include <queue>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/basictypes.h" 13 #include "base/basictypes.h"
14 #include "base/file_path.h" 14 #include "base/file_path.h"
15 #include "base/gtest_prod_util.h" 15 #include "base/gtest_prod_util.h"
16 #include "base/hash_tables.h" 16 #include "base/hash_tables.h"
17 #include "base/memory/ref_counted.h" 17 #include "base/memory/ref_counted.h"
18 #include "base/memory/weak_ptr.h" 18 #include "base/memory/weak_ptr.h"
19 #include "base/time.h" 19 #include "base/time.h"
20 #include "content/common/content_export.h" 20 #include "content/common/content_export.h"
21 #include "content/public/browser/download_item.h" 21 #include "content/public/browser/download_item.h"
22 #include "content/public/browser/download_manager_delegate.h"
22 #include "content/public/browser/save_page_type.h" 23 #include "content/public/browser/save_page_type.h"
23 #include "content/public/browser/web_contents_observer.h" 24 #include "content/public/browser/web_contents_observer.h"
24 #include "googleurl/src/gurl.h" 25 #include "googleurl/src/gurl.h"
25 26
26 class GURL; 27 class GURL;
27 class SaveFileManager; 28 class SaveFileManager;
28 class SaveItem; 29 class SaveItem;
29 class SavePackage; 30 class SavePackage;
30 struct SaveFileCreateInfo; 31 struct SaveFileCreateInfo;
31 32
32 namespace content { 33 namespace content {
33 class DownloadManager; 34 class DownloadManager;
34 class WebContents; 35 class WebContents;
35 } 36 }
36 37
37 // The SavePackage object manages the process of saving a page as only-html or 38 // The SavePackage object manages the process of saving a page as only-html or
38 // complete-html and providing the information for displaying saving status. 39 // complete-html or MHTML and providing the information for displaying saving
39 // Saving page as only-html means means that we save web page to a single HTML 40 // status. Saving page as only-html means means that we save web page to a
40 // file regardless internal sub resources and sub frames. 41 // single HTML file regardless internal sub resources and sub frames. Saving
41 // Saving page as complete-html page means we save not only the main html file 42 // page as complete-html page means we save not only the main html file the user
42 // the user told it to save but also a directory for the auxiliary files such 43 // told it to save but also a directory for the auxiliary files such as all
43 // as all sub-frame html files, image files, css files and js files. 44 // sub-frame html files, image files, css files and js files. Saving page as
45 // MHTML means the same thing as complete-html, but it uses the MHTML format to
46 // contain the html and all auxiliary files in a single text file.
44 // 47 //
45 // Each page saving job may include one or multiple files which need to be 48 // Each page saving job may include one or multiple files which need to be
46 // saved. Each file is represented by a SaveItem, and all SaveItems are owned 49 // saved. Each file is represented by a SaveItem, and all SaveItems are owned
47 // by the SavePackage. SaveItems are created when a user initiates a page 50 // by the SavePackage. SaveItems are created when a user initiates a page
48 // saving job, and exist for the duration of one contents's life time. 51 // saving job, and exist for the duration of one contents's life time.
49 class CONTENT_EXPORT SavePackage 52 class CONTENT_EXPORT SavePackage
50 : public base::RefCountedThreadSafe<SavePackage>, 53 : public base::RefCountedThreadSafe<SavePackage>,
51 public content::WebContentsObserver, 54 public content::WebContentsObserver,
52 public content::DownloadItem::Observer, 55 public content::DownloadItem::Observer,
53 public base::SupportsWeakPtr<SavePackage> { 56 public base::SupportsWeakPtr<SavePackage> {
(...skipping 27 matching lines...) Expand all
81 // better suited for tests. 84 // better suited for tests.
82 SavePackage(content::WebContents* web_contents, 85 SavePackage(content::WebContents* web_contents,
83 content::SavePageType save_type, 86 content::SavePageType save_type,
84 const FilePath& file_full_path, 87 const FilePath& file_full_path,
85 const FilePath& directory_full_path); 88 const FilePath& directory_full_path);
86 89
87 // Initialize the SavePackage. Returns true if it initializes properly. 90 // Initialize the SavePackage. Returns true if it initializes properly.
88 // Need to make sure that this method must be called in the UI thread because 91 // Need to make sure that this method must be called in the UI thread because
89 // using g_browser_process on a non-UI thread can cause crashes during 92 // using g_browser_process on a non-UI thread can cause crashes during
90 // shutdown. 93 // shutdown.
91 bool Init(); 94 // |download_created_callback| will be called when the DownloadItem is
95 // created, before data is written to disk.
96 bool Init(const content::SaveFileDownloadCreatedCallback& cb);
92 97
93 // Cancel all in progress request, might be called by user or internal error. 98 // Cancel all in progress request, might be called by user or internal error.
94 void Cancel(bool user_action); 99 void Cancel(bool user_action);
95 100
96 void Finish(); 101 void Finish();
97 102
98 // Notifications sent from the file thread to the UI thread. 103 // Notifications sent from the file thread to the UI thread.
99 void StartSave(const SaveFileCreateInfo* info); 104 void StartSave(const SaveFileCreateInfo* info);
100 bool UpdateSaveProgress(int32 save_id, int64 size, bool write_success); 105 bool UpdateSaveProgress(int32 save_id, int64 size, bool write_success);
101 void SaveFinished(int32 save_id, int64 size, bool is_success); 106 void SaveFinished(int32 save_id, int64 size, bool is_success);
102 void SaveFailed(const GURL& save_url); 107 void SaveFailed(const GURL& save_url);
103 void SaveCanceled(SaveItem* save_item); 108 void SaveCanceled(SaveItem* save_item);
104 109
105 // Rough percent complete, -1 means we don't know (since we didn't receive a 110 // Rough percent complete, -1 means we don't know (since we didn't receive a
106 // total size). 111 // total size).
107 int PercentComplete(); 112 int PercentComplete();
108 113
109 bool canceled() const { return user_canceled_ || disk_error_occurred_; } 114 bool canceled() const { return user_canceled_ || disk_error_occurred_; }
110 bool finished() const { return finished_; } 115 bool finished() const { return finished_; }
111 content::SavePageType save_type() const { return save_type_; } 116 content::SavePageType save_type() const { return save_type_; }
112 int contents_id() const { return contents_id_; } 117 int contents_id() const { return contents_id_; }
113 int id() const { return unique_id_; } 118 int id() const { return unique_id_; }
114 content::WebContents* web_contents() const; 119 content::WebContents* web_contents() const;
115 120
116 void GetSaveInfo(); 121 void GetSaveInfo();
117 122
118 private: 123 private:
119 friend class base::RefCountedThreadSafe<SavePackage>; 124 friend class base::RefCountedThreadSafe<SavePackage>;
120 125
126 // Callback for content::WebContents::GenerateMHTML().
127 void OnMHTMLGenerated(const FilePath& path, int64 size);
128
121 // For testing only. 129 // For testing only.
122 SavePackage(content::WebContents* web_contents, 130 SavePackage(content::WebContents* web_contents,
123 const FilePath& file_full_path, 131 const FilePath& file_full_path,
124 const FilePath& directory_full_path); 132 const FilePath& directory_full_path);
125 133
126 virtual ~SavePackage(); 134 virtual ~SavePackage();
127 135
128 // Notes from Init() above applies here as well. 136 // Notes from Init() above applies here as well.
129 void InternalInit(); 137 void InternalInit();
130 138
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 186
179 // Retrieves the URL to be saved from the WebContents. 187 // Retrieves the URL to be saved from the WebContents.
180 GURL GetUrlToBeSaved(); 188 GURL GetUrlToBeSaved();
181 189
182 void CreateDirectoryOnFileThread(const FilePath& website_save_dir, 190 void CreateDirectoryOnFileThread(const FilePath& website_save_dir,
183 const FilePath& download_save_dir, 191 const FilePath& download_save_dir,
184 const std::string& mime_type, 192 const std::string& mime_type,
185 const std::string& accept_langs); 193 const std::string& accept_langs);
186 void ContinueGetSaveInfo(const FilePath& suggested_path, 194 void ContinueGetSaveInfo(const FilePath& suggested_path,
187 bool can_save_as_complete); 195 bool can_save_as_complete);
188 void OnPathPicked(const FilePath& final_name, content::SavePageType type); 196 void OnPathPicked(
197 const FilePath& final_name,
198 content::SavePageType type,
199 const content::SaveFileDownloadCreatedCallback& cb);
189 void OnReceivedSavableResourceLinksForCurrentPage( 200 void OnReceivedSavableResourceLinksForCurrentPage(
190 const std::vector<GURL>& resources_list, 201 const std::vector<GURL>& resources_list,
191 const std::vector<GURL>& referrers_list, 202 const std::vector<GURL>& referrers_list,
192 const std::vector<GURL>& frames_list); 203 const std::vector<GURL>& frames_list);
193 204
194 void OnReceivedSerializedHtmlData(const GURL& frame_url, 205 void OnReceivedSerializedHtmlData(const GURL& frame_url,
195 const std::string& data, 206 const std::string& data,
196 int32 status); 207 int32 status);
197 208
198 typedef base::hash_map<std::string, SaveItem*> SaveUrlItemMap; 209 typedef base::hash_map<std::string, SaveItem*> SaveUrlItemMap;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 274
264 // The title of the page the user wants to save. 275 // The title of the page the user wants to save.
265 string16 title_; 276 string16 title_;
266 277
267 // Used to calculate package download speed (in files per second). 278 // Used to calculate package download speed (in files per second).
268 base::TimeTicks start_tick_; 279 base::TimeTicks start_tick_;
269 280
270 // Indicates whether the actual saving job is finishing or not. 281 // Indicates whether the actual saving job is finishing or not.
271 bool finished_; 282 bool finished_;
272 283
284 // Indicates whether a call to Finish() has been scheduled.
285 bool mhtml_finishing_;
286
273 // Indicates whether user canceled the saving job. 287 // Indicates whether user canceled the saving job.
274 bool user_canceled_; 288 bool user_canceled_;
275 289
276 // Indicates whether user get disk error. 290 // Indicates whether user get disk error.
277 bool disk_error_occurred_; 291 bool disk_error_occurred_;
278 292
279 // Type about saving page as only-html or complete-html. 293 // Type about saving page as only-html or complete-html.
280 content::SavePageType save_type_; 294 content::SavePageType save_type_;
281 295
282 // Number of all need to be saved resources. 296 // Number of all need to be saved resources.
(...skipping 24 matching lines...) Expand all
307 bool wrote_to_failed_file_; 321 bool wrote_to_failed_file_;
308 322
309 friend class SavePackageTest; 323 friend class SavePackageTest;
310 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames); 324 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestSuggestedSaveNames);
311 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename); 325 FRIEND_TEST_ALL_PREFIXES(SavePackageTest, TestLongSafePureFilename);
312 326
313 DISALLOW_COPY_AND_ASSIGN(SavePackage); 327 DISALLOW_COPY_AND_ASSIGN(SavePackage);
314 }; 328 };
315 329
316 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_ 330 #endif // CONTENT_BROWSER_DOWNLOAD_SAVE_PACKAGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698