[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 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 functionality provided here allows the user to import their bookmarks |
| 6 | // (favorites) from Google Toolbar. |
| 7 | |
| 8 | #ifndef CHROME_BROWSER_IMPORTER_TOOLBAR_IMPORTER_H_ |
| 9 | #define CHROME_BROWSER_IMPORTER_TOOLBAR_IMPORTER_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 10 | #pragma once |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 11 | |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
[email protected] | a4d9b5a | 2011-03-05 01:23:01 | [diff] [blame] | 15 | #include "base/basictypes.h" |
| 16 | #include "base/compiler_specific.h" |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 17 | #include "base/gtest_prod_util.h" |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 18 | #include "base/string16.h" |
[email protected] | 95940f10 | 2010-05-20 19:25:35 | [diff] [blame] | 19 | #include "chrome/browser/importer/importer_data_types.h" |
[email protected] | e46a9e3 | 2011-03-09 15:00:10 | [diff] [blame^] | 20 | #include "chrome/browser/importer/importer_host.h" |
[email protected] | 68d2a05f | 2010-05-07 21:39:55 | [diff] [blame] | 21 | #include "chrome/common/net/url_fetcher.h" |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 22 | |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 23 | class ImporterBridge; |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 24 | class XmlReader; |
| 25 | |
| 26 | // Currently the only configuration information we need is to check whether or |
| 27 | // not the user currently has their GAIA cookie. This is done by the function |
| 28 | // exposed through the ToolbarImportUtils namespace. |
| 29 | namespace toolbar_importer_utils { |
| 30 | bool IsGoogleGAIACookieInstalled(); |
| 31 | } // namespace toolbar_importer_utils |
| 32 | |
| 33 | // Toolbar5Importer is a class which exposes the functionality needed to |
| 34 | // communicate with the Google Toolbar v5 front-end, negotiate the download of |
| 35 | // Toolbar bookmarks, parse them, and install them on the client. |
| 36 | // Toolbar5Importer should not have StartImport called more than once. Futher |
| 37 | // if StartImport is called, then the class must not be destroyed until it |
| 38 | // has either completed or Toolbar5Importer->Cancel() has been called. |
| 39 | class Toolbar5Importer : public URLFetcher::Delegate, public Importer { |
| 40 | public: |
| 41 | Toolbar5Importer(); |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 42 | |
[email protected] | a4d9b5a | 2011-03-05 01:23:01 | [diff] [blame] | 43 | // Importer: |
| 44 | // ImportDialogView calls this method to begin the process. |items| should |
| 45 | // only either be NONE or FAVORITES, since as of right now these are the only |
| 46 | // items this importer supports. |
[email protected] | aad8e8f | 2010-11-09 02:04:17 | [diff] [blame] | 47 | virtual void StartImport(const importer::ProfileInfo& profile_info, |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 48 | uint16 items, |
[email protected] | a4d9b5a | 2011-03-05 01:23:01 | [diff] [blame] | 49 | ImporterBridge* bridge) OVERRIDE; |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 50 | |
| 51 | // Importer view call this method when the user clicks the cancel button |
[email protected] | 2fca3cd | 2011-03-04 01:52:07 | [diff] [blame] | 52 | // in the ImportDialogView UI. We need to post a message to our loop |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 53 | // to cancel network retrieval. |
| 54 | virtual void Cancel(); |
| 55 | |
| 56 | // URLFetcher::Delegate method called back from the URLFetcher object. |
| 57 | virtual void OnURLFetchComplete(const URLFetcher* source, |
[email protected] | 2fca3cd | 2011-03-04 01:52:07 | [diff] [blame] | 58 | const GURL& url, |
| 59 | const net::URLRequestStatus& status, |
| 60 | int response_code, |
| 61 | const ResponseCookies& cookies, |
| 62 | const std::string& data); |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 63 | |
| 64 | private: |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 65 | FRIEND_TEST_ALL_PREFIXES(Toolbar5ImporterTest, BookmarkParse); |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 66 | |
[email protected] | 7991a23 | 2009-11-06 01:55:48 | [diff] [blame] | 67 | virtual ~Toolbar5Importer(); |
| 68 | |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 69 | // Internal states of the toolbar importer. |
| 70 | enum InternalStateEnum { |
| 71 | NOT_USED = -1, |
| 72 | INITIALIZED, |
| 73 | GET_AUTHORIZATION_TOKEN, |
| 74 | GET_BOOKMARKS, |
| 75 | PARSE_BOOKMARKS, |
| 76 | DONE |
| 77 | }; |
| 78 | |
| 79 | typedef std::vector<std::wstring> BookmarkFolderType; |
| 80 | |
| 81 | // URLs for connecting to the toolbar front end are defined below. |
| 82 | static const char kT5AuthorizationTokenUrl[]; |
| 83 | static const char kT5FrontEndUrlTemplate[]; |
| 84 | |
| 85 | // Token replacement tags are defined below. |
| 86 | static const char kRandomNumberToken[]; |
| 87 | static const char kAuthorizationToken[]; |
| 88 | static const char kAuthorizationTokenPrefix[]; |
| 89 | static const char kAuthorizationTokenSuffix[]; |
| 90 | static const char kMaxNumToken[]; |
| 91 | static const char kMaxTimestampToken[]; |
| 92 | |
| 93 | // XML tag names are defined below. |
| 94 | static const char kXmlApiReplyXmlTag[]; |
| 95 | static const char kBookmarksXmlTag[]; |
| 96 | static const char kBookmarkXmlTag[]; |
| 97 | static const char kTitleXmlTag[]; |
| 98 | static const char kUrlXmlTag[]; |
| 99 | static const char kTimestampXmlTag[]; |
| 100 | static const char kLabelsXmlTag[]; |
| 101 | static const char kLabelsXmlCloseTag[]; |
| 102 | static const char kLabelXmlTag[]; |
| 103 | static const char kAttributesXmlTag[]; |
| 104 | |
| 105 | // Flow control for asynchronous import is controlled by the methods below. |
| 106 | // ContinueImport is called back by each import action taken. BeginXXX |
| 107 | // and EndXXX are responsible for updating the state of the asynchronous |
| 108 | // import. EndImport is responsible for state cleanup and notifying the |
| 109 | // caller that import has completed. |
| 110 | void ContinueImport(); |
| 111 | void EndImport(); |
| 112 | void BeginImportBookmarks(); |
| 113 | void EndImportBookmarks(); |
| 114 | |
| 115 | // Network I/O is done by the methods below. These three methods are called |
| 116 | // in the order provided. The last two are called back with the HTML |
| 117 | // response provided by the Toolbar server. |
| 118 | void GetAuthenticationFromServer(); |
| 119 | void GetBookmarkDataFromServer(const std::string& response); |
| 120 | void GetBookmarksFromServerDataResponse(const std::string& response); |
| 121 | |
| 122 | // XML Parsing is implemented with the methods below. |
| 123 | bool ParseAuthenticationTokenResponse(const std::string& response, |
| 124 | std::string* token); |
| 125 | |
| 126 | static bool ParseBookmarksFromReader( |
| 127 | XmlReader* reader, |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 128 | std::vector<ProfileWriter::BookmarkEntry>* bookmarks, |
| 129 | const string16& bookmark_group_string); |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 130 | |
| 131 | static bool LocateNextOpenTag(XmlReader* reader); |
| 132 | static bool LocateNextTagByName(XmlReader* reader, const std::string& tag); |
| 133 | static bool LocateNextTagWithStopByName( |
| 134 | XmlReader* reader, |
| 135 | const std::string& tag, |
| 136 | const std::string& stop); |
| 137 | |
| 138 | static bool ExtractBookmarkInformation( |
| 139 | XmlReader* reader, |
| 140 | ProfileWriter::BookmarkEntry* bookmark_entry, |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 141 | std::vector<BookmarkFolderType>* bookmark_folders, |
| 142 | const string16& bookmark_group_string); |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 143 | static bool ExtractNamedValueFromXmlReader(XmlReader* reader, |
| 144 | const std::string& name, |
| 145 | std::string* buffer); |
| 146 | static bool ExtractTitleFromXmlReader(XmlReader* reader, |
| 147 | ProfileWriter::BookmarkEntry* entry); |
| 148 | static bool ExtractUrlFromXmlReader(XmlReader* reader, |
| 149 | ProfileWriter::BookmarkEntry* entry); |
| 150 | static bool ExtractTimeFromXmlReader(XmlReader* reader, |
| 151 | ProfileWriter::BookmarkEntry* entry); |
| 152 | static bool ExtractFoldersFromXmlReader( |
| 153 | XmlReader* reader, |
[email protected] | 89cfc19d | 2010-06-04 15:36:35 | [diff] [blame] | 154 | std::vector<BookmarkFolderType>* bookmark_folders, |
| 155 | const string16& bookmark_group_string); |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 156 | |
| 157 | // Bookmark creation is done by the method below. |
| 158 | void AddBookmarksToChrome( |
| 159 | const std::vector<ProfileWriter::BookmarkEntry>& bookmarks); |
| 160 | |
[email protected] | 9b7ef69 | 2009-04-16 22:59:02 | [diff] [blame] | 161 | // Internal state is stored in state_. |
| 162 | InternalStateEnum state_; |
| 163 | |
| 164 | // Bitmask of Importer::ImportItem is stored in items_to_import_. |
| 165 | uint16 items_to_import_; |
| 166 | |
| 167 | // The fetchers need to be available to cancel the network call on user cancel |
| 168 | // hence they are stored as member variables. |
| 169 | URLFetcher* token_fetcher_; |
| 170 | URLFetcher* data_fetcher_; |
| 171 | |
| 172 | DISALLOW_COPY_AND_ASSIGN(Toolbar5Importer); |
| 173 | }; |
| 174 | |
| 175 | #endif // CHROME_BROWSER_IMPORTER_TOOLBAR_IMPORTER_H_ |