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

Side by Side Diff: chrome/browser/save_package.h

Issue 3040: Move the Save Page code. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 12 years, 3 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
« no previous file with comments | « chrome/browser/save_item.cc ('k') | chrome/browser/save_package.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 // The SavePackage object manages the process of saving a page as only-html or
6 // complete-html and providing the information for displaying saving status.
7 // Saving page as only-html means means that we save web page to a single HTML
8 // file regardless internal sub resources and sub frames.
9 // Saving page as complete-html page means we save not only the main html file
10 // the user told it to save but also a directory for the auxiliary files such
11 // as all sub-frame html files, image files, css files and js files.
12 //
13 // Each page saving job may include one or multiple files which need to be
14 // saved. Each file is represented by a SaveItem, and all SaveItems are owned
15 // by the SavePackage. SaveItems are created when a user initiates a page
16 // saving job, and exist for the duration of one tab's life time.
17
18 #ifndef CHROME_BROWSER_SAVE_PACKAGE_H__
19 #define CHROME_BROWSER_SAVE_PACKAGE_H__
20
21 #include <string>
22 #include <vector>
23 #include <queue>
24 #include <utility>
25
26 #include "base/basictypes.h"
27 #include "base/hash_tables.h"
28 #include "base/ref_counted.h"
29 #include "base/time.h"
30 #include "chrome/common/pref_member.h"
31 #include "chrome/browser/save_item.h"
32 #include "chrome/browser/save_types.h"
33
34 class SaveFileManager;
35 class SavePackage;
36 class DownloadItem;
37 class GURL;
38 class MessageLoop;
39 class PrefService;
40 class Profile;
41 class WebContents;
42 class URLRequestContext;
43 class WebContents;
44 class Time;
45
46 namespace base {
47 class Thread;
48 }
49
50 // save package: manages all save item.
51 class SavePackage : public base::RefCountedThreadSafe<SavePackage> {
52 public:
53 enum SavePackageType {
54 // User chose to save only the HTML of the page.
55 SAVE_AS_ONLY_HTML = 0,
56 // User chose to save complete-html page.
57 SAVE_AS_COMPLETE_HTML = 1
58 };
59
60 enum WaitState {
61 // State when created but not initialized.
62 INITIALIZE = 0,
63 // State when after initializing, but not yet saving.
64 START_PROCESS,
65 // Waiting on a list of savable resources from the backend.
66 RESOURCES_LIST,
67 // Waiting for data sent from net IO or from file system.
68 NET_FILES,
69 // Waiting for html DOM data sent from render process.
70 HTML_DATA,
71 // Saving page finished successfully.
72 SUCCESSFUL,
73 // Failed to save page.
74 FAILED
75 };
76
77 SavePackage(WebContents* web_content,
78 SavePackageType save_type,
79 const std::wstring& file_full_path,
80 const std::wstring& directory_full_path);
81
82 ~SavePackage();
83
84 // Initialize the SavePackage. Returns true if it initializes properly.
85 // Need to make sure that this method must be called in the UI thread because
86 // using g_browser_process on a non-UI thread can cause crashes during
87 // shutdown.
88 bool Init();
89
90 void Cancel(bool user_action);
91
92 void Finish();
93
94 // Notifications sent from the file thread to the UI thread.
95 void StartSave(const SaveFileCreateInfo* info);
96 bool UpdateSaveProgress(int32 save_id, int64 size, bool write_success);
97 void SaveFinished(int32 save_id, int64 size, bool is_success);
98 void SaveFailed(const std::wstring& save_url);
99 void SaveCanceled(SaveItem* save_item);
100
101 // Process current page's all savable links of sub resources, resources'
102 // referrer and frames(include main frame and sub frames) gotten from
103 // render process.
104 void ProcessCurrentPageAllSavableResourceLinks(
105 const std::vector<GURL>& resources_list,
106 const std::vector<GURL>& referrers_list,
107 const std::vector<GURL>& frames_list);
108
109 // Process the serialized html content data of a specified web page
110 // gotten from render process.
111 void ProcessSerializedHtmlData(const GURL& frame_url,
112 const std::string& data,
113 int32 status);
114
115 // Rough percent complete, -1 means we don't know (since we didn't receive a
116 // total size).
117 int PercentComplete();
118
119 // Show or Open a saved page via the Windows shell.
120 void ShowDownloadInShell();
121
122 bool canceled() { return user_canceled_ || disk_error_occurred_; }
123
124 // Accessor
125 bool finished() { return finished_; }
126 SavePackageType save_type() { return save_type_; }
127
128 // Since for one tab, it can only have one SavePackage in same time.
129 // Now we actually use render_process_id as tab's unique id.
130 int GetTabId();
131
132 // Helper function for preparing suggested name for the SaveAs Dialog. The
133 // suggested name is composed of the default save path and the web document's
134 // title.
135 static std::wstring GetSuggestNameForSaveAs(PrefService* prefs,
136 const std::wstring& name);
137
138 // This structure is for storing parameters which we will use to create
139 // a SavePackage object later.
140 struct SavePackageParam {
141 // MIME type of current tab contents.
142 const std::string& current_tab_mime_type;
143 // Pointer to preference service.
144 PrefService* prefs;
145 // Type about saving page as only-html or complete-html.
146 SavePackageType save_type;
147 // File path for main html file.
148 std::wstring saved_main_file_path;
149 // Directory path for saving sub resources and sub html frames.
150 std::wstring dir;
151
152 SavePackageParam(const std::string& mime_type)
153 : current_tab_mime_type(mime_type) { }
154 };
155 static bool GetSaveInfo(const std::wstring& suggest_name,
156 HWND container_hwnd,
157 SavePackageParam* param);
158
159 // File name is consist of pure file name, dot and file extension name. File
160 // name might has no dot and file extension, or has multiple dot inside file
161 // name. The dot, which separates the pure file name and file extension name,
162 // is last dot in the file name. If the file name matches following patterns:
163 // base_file_name(ordinal_number) or base_file_name(ordinal_number).extension,
164 // this function will return true and get the base file name part and
165 // ordinal_number part via output parameters. The |file_ordinal_number| could
166 // be empty if there is no content in ordinal_number part. If the file name
167 // does not match the pattern or the ordinal_number part has non-digit
168 // content, just return false.
169 static bool GetBaseFileNameAndFileOrdinalNumber(
170 const std::wstring& file_name,
171 std::wstring* base_file_name,
172 std::wstring* file_ordinal_number);
173
174 // Check whether we can do the saving page operation for the specified URL.
175 static bool IsSavableURL(const GURL& url);
176
177 // Check whether we can do the saving page operation for the contents which
178 // have the specified MIME type.
179 static bool IsSavableContents(const std::string& contents_mime_type);
180
181 // Check whether we can save page as complete-HTML for the contents which
182 // have specified a MIME type. Now only contents which have the MIME type
183 // "text/html" can be saved as complete-HTML.
184 static bool CanSaveAsComplete(const std::string& contents_mime_type);
185
186 // File name is considered being consist of pure file name, dot and file
187 // extension name. File name might has no dot and file extension, or has
188 // multiple dot inside file name. The dot, which separates the pure file
189 // name and file extension name, is last dot in the whole file name.
190 // This function is for making sure the length of specified file path is not
191 // great than the specified maximum length of file path and getting safe pure
192 // file name part if the input pure file name is too long.
193 // The parameter |dir_path| specifies directory part of the specified
194 // file path. The parameter |file_name_ext| specifies file extension
195 // name part of the specified file path (including start dot). The parameter
196 // |max_file_path_len| specifies maximum length of the specified file path.
197 // The parameter |pure_file_name| input pure file name part of the specified
198 // file path. If the length of specified file path is great than
199 // |max_file_path_len|, the |pure_file_name| will output new pure file name
200 // part for making sure the length of specified file path is less than
201 // specified maximum length of file path. Return false if the function can
202 // not get a safe pure file name, otherwise it returns true.
203 static bool GetSafePureFileName(const std::wstring& dir_path,
204 const std::wstring& file_name_ext,
205 uint32 max_file_path_len,
206 std::wstring* pure_file_name);
207
208 private:
209 // For testing.
210 friend class SavePackageTest;
211 SavePackage(const wchar_t* file_full_path,
212 const wchar_t* directory_full_path);
213
214 void Stop();
215 void CheckFinish();
216 void SaveNextFile(bool process_all_remainder_items);
217 void DoSavingProcess();
218
219 // Create a file name based on the response from the server.
220 bool GenerateFilename(const std::string& disposition,
221 const std::wstring& url,
222 bool need_html_ext,
223 std::wstring* generated_name);
224
225 // Get all savable resource links from current web page, include main
226 // frame and sub-frame.
227 void GetAllSavableResourceLinksForCurrentPage();
228 // Get html data by serializing all frames of current page with lists
229 // which contain all resource links that have local copy.
230 void GetSerializedHtmlDataForCurrentPageWithLocalLinks();
231
232 SaveItem* LookupItemInProcessBySaveId(int32 save_id);
233 void PutInProgressItemToSavedMap(SaveItem* save_item);
234
235 typedef base::hash_map<std::wstring, SaveItem*> SaveUrlItemMap;
236 // in_progress_items_ is map of all saving job in in-progress state.
237 SaveUrlItemMap in_progress_items_;
238 // saved_failed_items_ is map of all saving job which are failed.
239 SaveUrlItemMap saved_failed_items_;
240
241 // The number of in process SaveItems.
242 int in_process_count() const {
243 return static_cast<int>(in_progress_items_.size());
244 }
245
246 // The number of all SaveItems which have completed, including success items
247 // and failed items.
248 int completed_count() const {
249 return static_cast<int>(saved_success_items_.size() +
250 saved_failed_items_.size());
251 }
252
253 typedef std::queue<SaveItem*> SaveItemQueue;
254 // A queue for items we are about to start saving.
255 SaveItemQueue waiting_item_queue_;
256
257 typedef base::hash_map<int32, SaveItem*> SavedItemMap;
258 // saved_success_items_ is map of all saving job which are successfully saved.
259 SavedItemMap saved_success_items_;
260
261 // The request context which provides application-specific context for
262 // URLRequest instances.
263 scoped_refptr<URLRequestContext> request_context_;
264
265 // Non-owning pointer for handling file writing on the file thread.
266 SaveFileManager* file_manager_;
267
268 WebContents* web_contents_;
269
270 // We use a fake DownloadItem here in order to reuse the DownloadItemView.
271 // This class owns the pointer.
272 DownloadItem* download_;
273
274 // The URL of the page the user wants to save.
275 std::wstring page_url_;
276 std::wstring saved_main_file_path_;
277 std::wstring saved_main_directory_path_;
278
279 // Indicates whether the actual saving job is finishing or not.
280 bool finished_;
281
282 // Indicates whether user canceled the saving job.
283 bool user_canceled_;
284
285 // Indicates whether user get disk error.
286 bool disk_error_occurred_;
287
288 // Type about saving page as only-html or complete-html.
289 SavePackageType save_type_;
290
291 // Number of all need to be saved resources.
292 int all_save_items_count_;
293
294 typedef base::hash_set<std::wstring> FileNameSet;
295 // This set is used to eliminate duplicated file names in saving directory.
296 FileNameSet file_name_set_;
297
298 typedef base::hash_map<std::wstring, uint32> FileNameCountMap;
299 // This map is used to track serial number for specified filename.
300 FileNameCountMap file_name_count_map_;
301
302 // Indicates current waiting state when SavePackage try to get something
303 // from outside.
304 WaitState wait_state_;
305
306 DISALLOW_EVIL_CONSTRUCTORS(SavePackage);
307 };
308
309 #endif // CHROME_BROWSER_SAVE_PACKAGE_H__
OLDNEW
« no previous file with comments | « chrome/browser/save_item.cc ('k') | chrome/browser/save_package.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698