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