• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/basictypes.h"
16 #include "base/compiler_specific.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/string16.h"
19 #include "chrome/browser/importer/importer.h"
20 #include "chrome/browser/importer/profile_writer.h"
21 #include "chrome/common/net/url_fetcher.h"
22 
23 class ImporterBridge;
24 class XmlReader;
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 has
31 // either completed or Toolbar5Importer->Cancel() has been called.
32 class Toolbar5Importer : public URLFetcher::Delegate, public Importer {
33  public:
34   Toolbar5Importer();
35 
36   // Begin Importer implementation:
37 
38   // This method is called to begin the import process. |items| should only
39   // either be NONE or FAVORITES, since as of right now these are the only
40   // items this importer supports.
41   virtual void StartImport(const importer::SourceProfile& source_profile,
42                            uint16 items,
43                            ImporterBridge* bridge) OVERRIDE;
44 
45   // This method is called when the user clicks the cancel button on the UI
46   // dialog. We need to post a message to our loop to cancel network retrieval.
47   virtual void Cancel() OVERRIDE;
48 
49   // End Importer implementation.
50 
51   // URLFetcher::Delegate method called back from the URLFetcher object.
52   virtual void OnURLFetchComplete(const URLFetcher* source,
53                                   const GURL& url,
54                                   const net::URLRequestStatus& status,
55                                   int response_code,
56                                   const ResponseCookies& cookies,
57                                   const std::string& data);
58 
59  private:
60   FRIEND_TEST_ALL_PREFIXES(Toolbar5ImporterTest, BookmarkParse);
61 
62   virtual ~Toolbar5Importer();
63 
64   // Internal states of the toolbar importer.
65   enum InternalStateEnum {
66     NOT_USED = -1,
67     INITIALIZED,
68     GET_AUTHORIZATION_TOKEN,
69     GET_BOOKMARKS,
70     PARSE_BOOKMARKS,
71     DONE
72   };
73 
74   typedef std::vector<string16> BookmarkFolderType;
75 
76   // URLs for connecting to the toolbar front end are defined below.
77   static const char kT5AuthorizationTokenUrl[];
78   static const char kT5FrontEndUrlTemplate[];
79 
80   // Token replacement tags are defined below.
81   static const char kRandomNumberToken[];
82   static const char kAuthorizationToken[];
83   static const char kAuthorizationTokenPrefix[];
84   static const char kAuthorizationTokenSuffix[];
85   static const char kMaxNumToken[];
86   static const char kMaxTimestampToken[];
87 
88   // XML tag names are defined below.
89   static const char kXmlApiReplyXmlTag[];
90   static const char kBookmarksXmlTag[];
91   static const char kBookmarkXmlTag[];
92   static const char kTitleXmlTag[];
93   static const char kUrlXmlTag[];
94   static const char kTimestampXmlTag[];
95   static const char kLabelsXmlTag[];
96   static const char kLabelsXmlCloseTag[];
97   static const char kLabelXmlTag[];
98   static const char kAttributesXmlTag[];
99 
100   // Flow control for asynchronous import is controlled by the methods below.
101   // ContinueImport is called back by each import action taken.  BeginXXX
102   // and EndXXX are responsible for updating the state of the asynchronous
103   // import.  EndImport is responsible for state cleanup and notifying the
104   // caller that import has completed.
105   void ContinueImport();
106   void EndImport();
107   void BeginImportBookmarks();
108   void EndImportBookmarks();
109 
110   // Network I/O is done by the methods below.  These three methods are called
111   // in the order provided.  The last two are called back with the HTML
112   // response provided by the Toolbar server.
113   void GetAuthenticationFromServer();
114   void GetBookmarkDataFromServer(const std::string& response);
115   void GetBookmarksFromServerDataResponse(const std::string& response);
116 
117   // XML Parsing is implemented with the methods below.
118   bool ParseAuthenticationTokenResponse(const std::string& response,
119                                         std::string* token);
120 
121   static bool ParseBookmarksFromReader(
122       XmlReader* reader,
123       std::vector<ProfileWriter::BookmarkEntry>* bookmarks,
124       const string16& bookmark_group_string);
125 
126   static bool LocateNextOpenTag(XmlReader* reader);
127   static bool LocateNextTagByName(XmlReader* reader, const std::string& tag);
128   static bool LocateNextTagWithStopByName(
129       XmlReader* reader,
130       const std::string& tag,
131       const std::string& stop);
132 
133   static bool ExtractBookmarkInformation(
134       XmlReader* reader,
135       ProfileWriter::BookmarkEntry* bookmark_entry,
136       std::vector<BookmarkFolderType>* bookmark_folders,
137       const string16& bookmark_group_string);
138   static bool ExtractNamedValueFromXmlReader(XmlReader* reader,
139                                              const std::string& name,
140                                              std::string* buffer);
141   static bool ExtractTitleFromXmlReader(XmlReader* reader,
142                                         ProfileWriter::BookmarkEntry* entry);
143   static bool ExtractUrlFromXmlReader(XmlReader* reader,
144                                       ProfileWriter::BookmarkEntry* entry);
145   static bool ExtractTimeFromXmlReader(XmlReader* reader,
146                                        ProfileWriter::BookmarkEntry* entry);
147   static bool ExtractFoldersFromXmlReader(
148       XmlReader* reader,
149       std::vector<BookmarkFolderType>* bookmark_folders,
150       const string16& bookmark_group_string);
151 
152   // Bookmark creation is done by the method below.
153   void AddBookmarksToChrome(
154       const std::vector<ProfileWriter::BookmarkEntry>& bookmarks);
155 
156   InternalStateEnum state_;
157 
158   // Bitmask of Importer::ImportItem.
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_
170