OLD | NEW |
| (Empty) |
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 | |
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_ | |
10 | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/basictypes.h" | |
15 #include "base/compiler_specific.h" | |
16 #include "base/gtest_prod_util.h" | |
17 #include "base/memory/ref_counted.h" | |
18 #include "base/strings/string16.h" | |
19 #include "chrome/browser/importer/importer.h" | |
20 #include "net/url_request/url_fetcher_delegate.h" | |
21 #include "net/url_request/url_request_context_getter.h" | |
22 | |
23 struct ImportedBookmarkEntry; | |
24 class ImporterBridge; | |
25 class XmlReader; | |
26 | |
27 namespace net { | |
28 class URLFetcher; | |
29 } // namespace net | |
30 | |
31 // Toolbar5Importer is a class which exposes the functionality needed to | |
32 // communicate with the Google Toolbar v5 front-end, negotiate the download of | |
33 // Toolbar bookmarks, parse them, and install them on the client. | |
34 // Toolbar5Importer should not have StartImport called more than once. Futher | |
35 // if StartImport is called, then the class must not be destroyed until it has | |
36 // either completed or Toolbar5Importer->Cancel() has been called. | |
37 class Toolbar5Importer : public net::URLFetcherDelegate, public Importer { | |
38 public: | |
39 Toolbar5Importer(); | |
40 | |
41 // Importer: | |
42 // The importer view calls this method to begin the process. |items| should | |
43 // only either be NONE or FAVORITES, since as of right now these are the only | |
44 // items this importer supports. | |
45 virtual void StartImport(const importer::SourceProfile& source_profile, | |
46 uint16 items, | |
47 ImporterBridge* bridge) OVERRIDE; | |
48 | |
49 // Importer view call this method when the user clicks the cancel button | |
50 // in the tabbed options UI. We need to post a message to our loop | |
51 // to cancel network retrieval. | |
52 virtual void Cancel() OVERRIDE; | |
53 | |
54 // net::URLFetcherDelegate method called back from the URLFetcher object. | |
55 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
56 | |
57 private: | |
58 FRIEND_TEST_ALL_PREFIXES(Toolbar5ImporterTest, BookmarkParse); | |
59 | |
60 virtual ~Toolbar5Importer(); | |
61 | |
62 // Internal states of the toolbar importer. | |
63 enum InternalStateEnum { | |
64 NOT_USED = -1, | |
65 INITIALIZED, | |
66 GET_AUTHORIZATION_TOKEN, | |
67 GET_BOOKMARKS, | |
68 PARSE_BOOKMARKS, | |
69 DONE | |
70 }; | |
71 | |
72 typedef std::vector<string16> BookmarkFolderType; | |
73 | |
74 // URLs for connecting to the toolbar front end are defined below. | |
75 static const char kT5AuthorizationTokenUrl[]; | |
76 static const char kT5FrontEndUrlTemplate[]; | |
77 | |
78 // Token replacement tags are defined below. | |
79 static const char kRandomNumberToken[]; | |
80 static const char kAuthorizationToken[]; | |
81 static const char kAuthorizationTokenPrefix[]; | |
82 static const char kAuthorizationTokenSuffix[]; | |
83 static const char kMaxNumToken[]; | |
84 static const char kMaxTimestampToken[]; | |
85 | |
86 // XML tag names are defined below. | |
87 static const char kXmlApiReplyXmlTag[]; | |
88 static const char kBookmarksXmlTag[]; | |
89 static const char kBookmarkXmlTag[]; | |
90 static const char kTitleXmlTag[]; | |
91 static const char kUrlXmlTag[]; | |
92 static const char kTimestampXmlTag[]; | |
93 static const char kLabelsXmlTag[]; | |
94 static const char kLabelsXmlCloseTag[]; | |
95 static const char kLabelXmlTag[]; | |
96 static const char kAttributesXmlTag[]; | |
97 | |
98 // Flow control for asynchronous import is controlled by the methods below. | |
99 // ContinueImport is called back by each import action taken. BeginXXX | |
100 // and EndXXX are responsible for updating the state of the asynchronous | |
101 // import. EndImport is responsible for state cleanup and notifying the | |
102 // caller that import has completed. | |
103 void ContinueImport(); | |
104 void EndImport(); | |
105 void BeginImportBookmarks(); | |
106 void EndImportBookmarks(); | |
107 | |
108 // Network I/O is done by the methods below. These three methods are called | |
109 // in the order provided. The last two are called back with the HTML | |
110 // response provided by the Toolbar server. | |
111 void GetAuthenticationFromServer(); | |
112 void GetBookmarkDataFromServer(const std::string& response); | |
113 void GetBookmarksFromServerDataResponse(const std::string& response); | |
114 | |
115 // XML Parsing is implemented with the methods below. | |
116 bool ParseAuthenticationTokenResponse(const std::string& response, | |
117 std::string* token); | |
118 | |
119 static bool ParseBookmarksFromReader( | |
120 XmlReader* reader, | |
121 std::vector<ImportedBookmarkEntry>* bookmarks, | |
122 const string16& bookmark_group_string); | |
123 | |
124 static bool LocateNextOpenTag(XmlReader* reader); | |
125 static bool LocateNextTagByName(XmlReader* reader, const std::string& tag); | |
126 static bool LocateNextTagWithStopByName( | |
127 XmlReader* reader, | |
128 const std::string& tag, | |
129 const std::string& stop); | |
130 | |
131 static bool ExtractBookmarkInformation( | |
132 XmlReader* reader, | |
133 ImportedBookmarkEntry* bookmark_entry, | |
134 std::vector<BookmarkFolderType>* bookmark_folders, | |
135 const string16& bookmark_group_string); | |
136 static bool ExtractNamedValueFromXmlReader(XmlReader* reader, | |
137 const std::string& name, | |
138 std::string* buffer); | |
139 static bool ExtractTitleFromXmlReader(XmlReader* reader, | |
140 ImportedBookmarkEntry* entry); | |
141 static bool ExtractUrlFromXmlReader(XmlReader* reader, | |
142 ImportedBookmarkEntry* entry); | |
143 static bool ExtractTimeFromXmlReader(XmlReader* reader, | |
144 ImportedBookmarkEntry* entry); | |
145 static bool ExtractFoldersFromXmlReader( | |
146 XmlReader* reader, | |
147 std::vector<BookmarkFolderType>* bookmark_folders, | |
148 const string16& bookmark_group_string); | |
149 | |
150 // Bookmark creation is done by the method below. | |
151 void AddBookmarksToChrome( | |
152 const std::vector<ImportedBookmarkEntry>& bookmarks); | |
153 | |
154 InternalStateEnum state_; | |
155 | |
156 // Bitmask of Importer::ImportItem. | |
157 uint16 items_to_import_; | |
158 | |
159 // The fetchers need to be available to cancel the network call on user cancel | |
160 // hence they are stored as member variables. | |
161 net::URLFetcher* token_fetcher_; | |
162 net::URLFetcher* data_fetcher_; | |
163 | |
164 // Used to get correct login data for the toolbar server. | |
165 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(Toolbar5Importer); | |
168 }; | |
169 | |
170 #endif // CHROME_BROWSER_IMPORTER_TOOLBAR_IMPORTER_H_ | |
OLD | NEW |