OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_IMPORTER_IMPORTER_H_ | 5 #ifndef CHROME_BROWSER_IMPORTER_IMPORTER_H_ |
6 #define CHROME_BROWSER_IMPORTER_IMPORTER_H_ | 6 #define CHROME_BROWSER_IMPORTER_IMPORTER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
14 #include "chrome/browser/bookmarks/bookmark_model.h" | 14 #include "chrome/browser/bookmarks/bookmark_model.h" |
15 #include "chrome/browser/history/history_types.h" | 15 #include "chrome/browser/history/history_types.h" |
16 #include "chrome/browser/ie7_password.h" | 16 #include "chrome/browser/ie7_password.h" |
17 #include "chrome/browser/profile.h" | 17 #include "chrome/browser/profile.h" |
18 #include "chrome/browser/template_url.h" | 18 #include "chrome/browser/template_url.h" |
19 #include "chrome/browser/views/importer_lock_view.h" | 19 #include "chrome/browser/views/importer_lock_view.h" |
20 #include "chrome/common/notification_service.h" | 20 #include "chrome/common/notification_service.h" |
21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
22 #include "webkit/glue/password_form.h" | 22 #include "webkit/glue/password_form.h" |
23 | 23 |
24 // An enumeration of the type of browsers that we support to import | 24 // An enumeration of the type of browsers that we support to import |
25 // settings and data from them. | 25 // settings and data from them. |
26 enum ProfileType { | 26 enum ProfileType { |
27 MS_IE = 0, | 27 MS_IE = 0, |
28 FIREFOX2, | 28 FIREFOX2, |
29 FIREFOX3, | 29 FIREFOX3, |
30 GOOGLE_TOOLBAR5 | 30 GOOGLE_TOOLBAR5, |
| 31 // Identifies a 'bookmarks.html' file. |
| 32 BOOKMARKS_HTML |
31 }; | 33 }; |
32 | 34 |
33 // An enumeration of the type of data we want to import. | 35 // An enumeration of the type of data we want to import. |
34 enum ImportItem { | 36 enum ImportItem { |
35 NONE = 0x0000, | 37 NONE = 0x0000, |
36 HISTORY = 0x0001, | 38 HISTORY = 0x0001, |
37 FAVORITES = 0x0002, | 39 FAVORITES = 0x0002, |
38 COOKIES = 0x0004, // not supported yet. | 40 COOKIES = 0x0004, // not supported yet. |
39 PASSWORDS = 0x0008, | 41 PASSWORDS = 0x0008, |
40 SEARCH_ENGINES = 0x0010, | 42 SEARCH_ENGINES = 0x0010, |
41 HOME_PAGE = 0x0020, | 43 HOME_PAGE = 0x0020, |
42 ALL = 0x003f | 44 ALL = 0x003f |
43 }; | 45 }; |
44 | 46 |
45 typedef struct { | 47 typedef struct { |
46 std::wstring description; | 48 std::wstring description; |
47 ProfileType browser_type; | 49 ProfileType browser_type; |
48 std::wstring source_path; | 50 std::wstring source_path; |
49 std::wstring app_path; | 51 std::wstring app_path; |
50 uint16 services_supported; // bitmap of ImportItem | 52 uint16 services_supported; // bitmap of ImportItem |
51 } ProfileInfo; | 53 } ProfileInfo; |
52 | 54 |
53 class FirefoxProfileLock; | 55 class FirefoxProfileLock; |
54 class Importer; | 56 class Importer; |
55 | 57 |
56 // ProfileWriter encapsulates profile for writing entries into it. | 58 // ProfileWriter encapsulates profile for writing entries into it. |
57 // This object must be invoked on UI thread. | 59 // This object must be invoked on UI thread. |
58 class ProfileWriter : public base::RefCounted<ProfileWriter> { | 60 class ProfileWriter : public base::RefCounted<ProfileWriter> { |
59 public: | 61 public: |
| 62 // Used to identify how the bookmarks are added. |
| 63 enum BookmarkOptions { |
| 64 // Indicates the bookmark should only be added if unique. Uniqueness |
| 65 // is done by title, url and path. That is, if this is passed to |
| 66 // AddBookmarkEntry the bookmark is added only if there is no other |
| 67 // URL with the same url, path and title. |
| 68 ADD_IF_UNIQUE = 1 << 0, |
| 69 |
| 70 // Indicates the bookmarks are being added during first run. |
| 71 FIRST_RUN = 1 << 1 |
| 72 }; |
| 73 |
60 explicit ProfileWriter(Profile* profile) : profile_(profile) { } | 74 explicit ProfileWriter(Profile* profile) : profile_(profile) { } |
61 virtual ~ProfileWriter() { } | 75 virtual ~ProfileWriter() { } |
62 | 76 |
63 // Methods for monitoring BookmarkModel status. | 77 // Methods for monitoring BookmarkModel status. |
64 virtual bool BookmarkModelIsLoaded() const; | 78 virtual bool BookmarkModelIsLoaded() const; |
65 virtual void AddBookmarkModelObserver( | 79 virtual void AddBookmarkModelObserver( |
66 BookmarkModelObserver* observer); | 80 BookmarkModelObserver* observer); |
67 | 81 |
68 // Methods for monitoring TemplateURLModel status. | 82 // Methods for monitoring TemplateURLModel status. |
69 virtual bool TemplateURLModelIsLoaded() const; | 83 virtual bool TemplateURLModelIsLoaded() const; |
70 virtual void AddTemplateURLModelObserver( | 84 virtual void AddTemplateURLModelObserver( |
71 NotificationObserver* observer); | 85 NotificationObserver* observer); |
72 | 86 |
73 // A bookmark entry. | 87 // A bookmark entry. |
74 struct BookmarkEntry { | 88 struct BookmarkEntry { |
75 bool in_toolbar; | 89 bool in_toolbar; |
76 GURL url; | 90 GURL url; |
77 std::vector<std::wstring> path; | 91 std::vector<std::wstring> path; |
78 std::wstring title; | 92 std::wstring title; |
79 base::Time creation_time; | 93 base::Time creation_time; |
80 | 94 |
81 BookmarkEntry() : in_toolbar(false) {} | 95 BookmarkEntry() : in_toolbar(false) {} |
82 }; | 96 }; |
83 | 97 |
84 // Helper methods for adding data to local stores. | 98 // Helper methods for adding data to local stores. |
85 virtual void AddPasswordForm(const PasswordForm& form); | 99 virtual void AddPasswordForm(const PasswordForm& form); |
86 virtual void AddIE7PasswordInfo(const IE7PasswordInfo& info); | 100 virtual void AddIE7PasswordInfo(const IE7PasswordInfo& info); |
87 virtual void AddHistoryPage(const std::vector<history::URLRow>& page); | 101 virtual void AddHistoryPage(const std::vector<history::URLRow>& page); |
88 virtual void AddHomepage(const GURL& homepage); | 102 virtual void AddHomepage(const GURL& homepage); |
89 virtual void AddBookmarkEntry( | 103 // Adds the bookmarks to the BookmarkModel. |
90 const std::vector<BookmarkEntry>& bookmark, | 104 // |options| is a bitmask of BookmarkOptions and dictates how and |
91 bool check_uniqueness); | 105 // which bookmarks are added. If the bitmask contains FIRST_RUN, |
| 106 // then any entries with a value of true for in_toolbar are added to |
| 107 // the bookmark bar. If the bitmask does not contain FIRST_RUN then |
| 108 // the folder name the bookmarks are added to is uniqued based on |
| 109 // |first_folder_name|. For example, if |first_folder_name| is 'foo' |
| 110 // and a folder with the name 'foo' already exists in the other |
| 111 // bookmarks folder, then the folder name 'foo 2' is used. |
| 112 // If |options| contains ADD_IF_UNIQUE, then the bookmark is added only |
| 113 // if another bookmarks does not exist with the same title, path and |
| 114 // url. |
| 115 virtual void AddBookmarkEntry(const std::vector<BookmarkEntry>& bookmark, |
| 116 const std::wstring& first_folder_name, |
| 117 int options); |
92 virtual void AddFavicons( | 118 virtual void AddFavicons( |
93 const std::vector<history::ImportedFavIconUsage>& favicons); | 119 const std::vector<history::ImportedFavIconUsage>& favicons); |
94 // Add the TemplateURLs in |template_urls| to the local store and make the | 120 // Add the TemplateURLs in |template_urls| to the local store and make the |
95 // TemplateURL at |default_keyword_index| the default keyword (does not set | 121 // TemplateURL at |default_keyword_index| the default keyword (does not set |
96 // a default keyword if it is -1). The local store becomes the owner of the | 122 // a default keyword if it is -1). The local store becomes the owner of the |
97 // TemplateURLs. Some TemplateURLs in |template_urls| may conflict (same | 123 // TemplateURLs. Some TemplateURLs in |template_urls| may conflict (same |
98 // keyword or same host name in the URL) with existing TemplateURLs in the | 124 // keyword or same host name in the URL) with existing TemplateURLs in the |
99 // local store, in which case the existing ones takes precedence and the | 125 // local store, in which case the existing ones takes precedence and the |
100 // duplicate in |template_urls| are deleted. | 126 // duplicate in |template_urls| are deleted. |
101 // If unique_on_host_and_path a TemplateURL is only added if there is not an | 127 // If unique_on_host_and_path a TemplateURL is only added if there is not an |
102 // existing TemplateURL that has a replaceable search url with the same | 128 // existing TemplateURL that has a replaceable search url with the same |
103 // host+path combination. | 129 // host+path combination. |
104 virtual void AddKeywords(const std::vector<TemplateURL*>& template_urls, | 130 virtual void AddKeywords(const std::vector<TemplateURL*>& template_urls, |
105 int default_keyword_index, | 131 int default_keyword_index, |
106 bool unique_on_host_and_path); | 132 bool unique_on_host_and_path); |
107 | 133 |
108 // Shows the bookmarks toolbar. | 134 // Shows the bookmarks toolbar. |
109 void ShowBookmarkBar(); | 135 void ShowBookmarkBar(); |
110 | 136 |
111 Profile* GetProfile() const { return profile_; } | 137 Profile* GetProfile() const { return profile_; } |
112 | 138 |
113 private: | 139 private: |
| 140 // Generates a unique folder name. If folder_name is not unique, then this |
| 141 // repeatedly tests for '|folder_name| + (i)' until a unique name is found. |
| 142 std::wstring GenerateUniqueFolderName(BookmarkModel* model, |
| 143 const std::wstring& folder_name); |
| 144 |
| 145 // Returns true if a bookmark exists with the same url, title and path |
| 146 // as |entry|. |first_folder_name| is the name to use for the first |
| 147 // path entry if |first_run| is true. |
| 148 bool DoesBookmarkExist(BookmarkModel* model, |
| 149 const BookmarkEntry& entry, |
| 150 const std::wstring& first_folder_name, |
| 151 bool first_run); |
| 152 |
114 Profile* profile_; | 153 Profile* profile_; |
115 | 154 |
116 DISALLOW_EVIL_CONSTRUCTORS(ProfileWriter); | 155 DISALLOW_EVIL_CONSTRUCTORS(ProfileWriter); |
117 }; | 156 }; |
118 | 157 |
119 // This class hosts the importers. It enumerates profiles from other | 158 // This class hosts the importers. It enumerates profiles from other |
120 // browsers dynamically, and controls the process of importing. When | 159 // browsers dynamically, and controls the process of importing. When |
121 // the import process is done, ImporterHost deletes itself. | 160 // the import process is done, ImporterHost deletes itself. |
122 class ImporterHost : public base::RefCounted<ImporterHost>, | 161 class ImporterHost : public base::RefCounted<ImporterHost>, |
123 public BookmarkModelObserver, | 162 public BookmarkModelObserver, |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 uint16 items, | 322 uint16 items, |
284 ProfileWriter* writer, | 323 ProfileWriter* writer, |
285 MessageLoop* delegate_loop, | 324 MessageLoop* delegate_loop, |
286 ImporterHost* host) = 0; | 325 ImporterHost* host) = 0; |
287 | 326 |
288 // Cancels the import process. | 327 // Cancels the import process. |
289 virtual void Cancel() { cancelled_ = true; } | 328 virtual void Cancel() { cancelled_ = true; } |
290 | 329 |
291 void set_first_run(bool first_run) { first_run_ = first_run; } | 330 void set_first_run(bool first_run) { first_run_ = first_run; } |
292 | 331 |
| 332 bool cancelled() const { return cancelled_; } |
| 333 |
293 protected: | 334 protected: |
294 Importer() | 335 Importer() |
295 : main_loop_(MessageLoop::current()), | 336 : main_loop_(MessageLoop::current()), |
296 delagate_loop_(NULL), | 337 delagate_loop_(NULL), |
297 importer_host_(NULL), | 338 importer_host_(NULL), |
298 cancelled_(false) {} | 339 cancelled_(false), |
| 340 first_run_(false) {} |
299 | 341 |
300 // Notifies the coordinator that the collection of data for the specified | 342 // Notifies the coordinator that the collection of data for the specified |
301 // item has begun. | 343 // item has begun. |
302 void NotifyItemStarted(ImportItem item) { | 344 void NotifyItemStarted(ImportItem item) { |
303 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(importer_host_, | 345 main_loop_->PostTask(FROM_HERE, NewRunnableMethod(importer_host_, |
304 &ImporterHost::ImportItemStarted, item)); | 346 &ImporterHost::ImportItemStarted, item)); |
305 } | 347 } |
306 | 348 |
307 // Notifies the coordinator that the collection of data for the specified | 349 // Notifies the coordinator that the collection of data for the specified |
308 // item has completed. | 350 // item has completed. |
(...skipping 13 matching lines...) Expand all Loading... |
322 main_loop_->PostTask(FROM_HERE, | 364 main_loop_->PostTask(FROM_HERE, |
323 NewRunnableMethod(importer_host_, &ImporterHost::ImportEnded)); | 365 NewRunnableMethod(importer_host_, &ImporterHost::ImportEnded)); |
324 } | 366 } |
325 | 367 |
326 // Given raw image data, decodes the icon, re-sampling to the correct size as | 368 // Given raw image data, decodes the icon, re-sampling to the correct size as |
327 // necessary, and re-encodes as PNG data in the given output vector. Returns | 369 // necessary, and re-encodes as PNG data in the given output vector. Returns |
328 // true on success. | 370 // true on success. |
329 static bool ReencodeFavicon(const unsigned char* src_data, size_t src_len, | 371 static bool ReencodeFavicon(const unsigned char* src_data, size_t src_len, |
330 std::vector<unsigned char>* png_data); | 372 std::vector<unsigned char>* png_data); |
331 | 373 |
332 bool cancelled() const { return cancelled_; } | |
333 | |
334 bool first_run() const { return first_run_; } | 374 bool first_run() const { return first_run_; } |
335 | 375 |
336 // The importer should know the main thread so that ProfileWriter | 376 // The importer should know the main thread so that ProfileWriter |
337 // will be invoked in thread instead. | 377 // will be invoked in thread instead. |
338 MessageLoop* main_loop_; | 378 MessageLoop* main_loop_; |
339 | 379 |
340 // The message loop in which the importer operates. | 380 // The message loop in which the importer operates. |
341 MessageLoop* delagate_loop_; | 381 MessageLoop* delagate_loop_; |
342 | 382 |
343 // The coordinator host for this importer. | 383 // The coordinator host for this importer. |
344 ImporterHost* importer_host_; | 384 ImporterHost* importer_host_; |
345 | 385 |
346 private: | 386 private: |
347 // True if the caller cancels the import process. | 387 // True if the caller cancels the import process. |
348 bool cancelled_; | 388 bool cancelled_; |
349 | 389 |
350 // True if the importer is created in the first run UI. | 390 // True if the importer is created in the first run UI. |
351 bool first_run_; | 391 bool first_run_; |
352 | 392 |
353 DISALLOW_EVIL_CONSTRUCTORS(Importer); | 393 DISALLOW_EVIL_CONSTRUCTORS(Importer); |
354 }; | 394 }; |
355 | 395 |
356 // An interface an object that calls StartImportingWithUI can call to be | 396 // An interface an object that calls StartImportingWithUI can call to be |
357 // notified about the state of the import operation. | 397 // notified about the state of the import operation. |
358 class ImportObserver { | 398 class ImportObserver { |
359 public: | 399 public: |
360 virtual ~ImportObserver() {} | 400 virtual ~ImportObserver() {} |
361 // The import operation was canceled by the user. | 401 // The import operation was canceled by the user. |
| 402 // TODO (4164): this is never invoked, either rip it out or invoke it. |
362 virtual void ImportCanceled() = 0; | 403 virtual void ImportCanceled() = 0; |
363 | 404 |
364 // The import operation was completed successfully. | 405 // The import operation was completed successfully. |
365 virtual void ImportComplete() = 0; | 406 virtual void ImportComplete() = 0; |
366 }; | 407 }; |
367 | 408 |
368 | 409 |
369 // Shows a UI for importing and begins importing the specified items from | 410 // Shows a UI for importing and begins importing the specified items from |
370 // source_profile to target_profile. observer is notified when the process is | 411 // source_profile to target_profile. observer is notified when the process is |
371 // complete, can be NULL. parent is the window to parent the UI to, can be NULL | 412 // complete, can be NULL. parent is the window to parent the UI to, can be NULL |
372 // if there's nothing to parent to. first_run is true if it's invoked in the | 413 // if there's nothing to parent to. first_run is true if it's invoked in the |
373 // first run UI. | 414 // first run UI. |
374 void StartImportingWithUI(HWND parent_window, | 415 void StartImportingWithUI(HWND parent_window, |
375 int16 items, | 416 int16 items, |
376 ImporterHost* coordinator, | 417 ImporterHost* coordinator, |
377 const ProfileInfo& source_profile, | 418 const ProfileInfo& source_profile, |
378 Profile* target_profile, | 419 Profile* target_profile, |
379 ImportObserver* observer, | 420 ImportObserver* observer, |
380 bool first_run); | 421 bool first_run); |
381 | 422 |
382 #endif // CHROME_BROWSER_IMPORTER_IMPORTER_H_ | 423 #endif // CHROME_BROWSER_IMPORTER_IMPORTER_H_ |
383 | |
OLD | NEW |