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