blob: 608fc12d0027b99a795244ad0e8843557da1dc28 [file] [log] [blame]
[email protected]27a112c2011-01-06 04:19:301// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]9b7ef692009-04-16 22:59:022// 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]32b76ef2010-07-26 23:08:2410#pragma once
[email protected]9b7ef692009-04-16 22:59:0211
12#include <string>
13#include <vector>
14
[email protected]a4d9b5a2011-03-05 01:23:0115#include "base/basictypes.h"
16#include "base/compiler_specific.h"
[email protected]a918f872010-06-01 14:30:5117#include "base/gtest_prod_util.h"
[email protected]89cfc19d2010-06-04 15:36:3518#include "base/string16.h"
[email protected]95940f102010-05-20 19:25:3519#include "chrome/browser/importer/importer_data_types.h"
[email protected]e46a9e32011-03-09 15:00:1020#include "chrome/browser/importer/importer_host.h"
[email protected]68d2a05f2010-05-07 21:39:5521#include "chrome/common/net/url_fetcher.h"
[email protected]9b7ef692009-04-16 22:59:0222
[email protected]f6061aed2009-10-06 16:28:5723class ImporterBridge;
[email protected]9b7ef692009-04-16 22:59:0224class 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.
29namespace toolbar_importer_utils {
30bool 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.
39class Toolbar5Importer : public URLFetcher::Delegate, public Importer {
40 public:
41 Toolbar5Importer();
[email protected]9b7ef692009-04-16 22:59:0242
[email protected]a4d9b5a2011-03-05 01:23:0143 // 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]aad8e8f2010-11-09 02:04:1747 virtual void StartImport(const importer::ProfileInfo& profile_info,
[email protected]9b7ef692009-04-16 22:59:0248 uint16 items,
[email protected]a4d9b5a2011-03-05 01:23:0149 ImporterBridge* bridge) OVERRIDE;
[email protected]9b7ef692009-04-16 22:59:0250
51 // Importer view call this method when the user clicks the cancel button
[email protected]2fca3cd2011-03-04 01:52:0752 // in the ImportDialogView UI. We need to post a message to our loop
[email protected]9b7ef692009-04-16 22:59:0253 // 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]2fca3cd2011-03-04 01:52:0758 const GURL& url,
59 const net::URLRequestStatus& status,
60 int response_code,
61 const ResponseCookies& cookies,
62 const std::string& data);
[email protected]9b7ef692009-04-16 22:59:0263
64 private:
[email protected]a918f872010-06-01 14:30:5165 FRIEND_TEST_ALL_PREFIXES(Toolbar5ImporterTest, BookmarkParse);
[email protected]9b7ef692009-04-16 22:59:0266
[email protected]7991a232009-11-06 01:55:4867 virtual ~Toolbar5Importer();
68
[email protected]9b7ef692009-04-16 22:59:0269 // 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]89cfc19d2010-06-04 15:36:35128 std::vector<ProfileWriter::BookmarkEntry>* bookmarks,
129 const string16& bookmark_group_string);
[email protected]9b7ef692009-04-16 22:59:02130
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]89cfc19d2010-06-04 15:36:35141 std::vector<BookmarkFolderType>* bookmark_folders,
142 const string16& bookmark_group_string);
[email protected]9b7ef692009-04-16 22:59:02143 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]89cfc19d2010-06-04 15:36:35154 std::vector<BookmarkFolderType>* bookmark_folders,
155 const string16& bookmark_group_string);
[email protected]9b7ef692009-04-16 22:59:02156
157 // Bookmark creation is done by the method below.
158 void AddBookmarksToChrome(
159 const std::vector<ProfileWriter::BookmarkEntry>& bookmarks);
160
[email protected]9b7ef692009-04-16 22:59:02161 // 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_