blob: 37a97fe8f8a887b161c0ce714c0bffe99fac6538 [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
[email protected]f6061aed2009-10-06 16:28:5717class ImporterBridge;
[email protected]9b7ef692009-04-16 22:59:0218class XmlReader;
19
20// Currently the only configuration information we need is to check whether or
21// not the user currently has their GAIA cookie. This is done by the function
22// exposed through the ToolbarImportUtils namespace.
23namespace toolbar_importer_utils {
24bool IsGoogleGAIACookieInstalled();
25} // namespace toolbar_importer_utils
26
27// Toolbar5Importer is a class which exposes the functionality needed to
28// communicate with the Google Toolbar v5 front-end, negotiate the download of
29// Toolbar bookmarks, parse them, and install them on the client.
30// Toolbar5Importer should not have StartImport called more than once. Futher
31// if StartImport is called, then the class must not be destroyed until it
32// has either completed or Toolbar5Importer->Cancel() has been called.
33class Toolbar5Importer : public URLFetcher::Delegate, public Importer {
34 public:
35 Toolbar5Importer();
[email protected]9b7ef692009-04-16 22:59:0236
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,
[email protected]f6061aed2009-10-06 16:28:5743 ImporterBridge* bridge);
[email protected]9b7ef692009-04-16 22:59:0244
45 // Importer view call this method when the user clicks the cancel button
46 // in the ImporterView UI. We need to post a message to our loop
47 // to cancel network retrieval.
48 virtual void Cancel();
49
50 // URLFetcher::Delegate method called back from the URLFetcher object.
51 virtual void OnURLFetchComplete(const URLFetcher* source,
52 const GURL& url,
53 const URLRequestStatus& status,
54 int response_code,
55 const ResponseCookies& cookies,
56 const std::string& data);
57
58 private:
[email protected]e2b56432009-05-12 19:34:5359 FRIEND_TEST(Toolbar5ImporterTest, BookmarkParse);
[email protected]9b7ef692009-04-16 22:59:0260
[email protected]7991a232009-11-06 01:55:4861 virtual ~Toolbar5Importer();
62
[email protected]9b7ef692009-04-16 22:59:0263 // 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
[email protected]9b7ef692009-04-16 22:59:02152 // Internal state is stored in state_.
153 InternalStateEnum state_;
154
155 // Bitmask of Importer::ImportItem is stored in items_to_import_.
156 uint16 items_to_import_;
157
158 // The fetchers need to be available to cancel the network call on user cancel
159 // hence they are stored as member variables.
160 URLFetcher* token_fetcher_;
161 URLFetcher* data_fetcher_;
162
163 DISALLOW_COPY_AND_ASSIGN(Toolbar5Importer);
164};
165
166#endif // CHROME_BROWSER_IMPORTER_TOOLBAR_IMPORTER_H_