summaryrefslogtreecommitdiffstats
path: root/chrome/browser/importer/toolbar_importer.h
blob: 729e73ead0493e195f7186990887c706d3b03e66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_IMPORTER_TOOLBAR_IMPROTER_H__
#define CHROME_BROWSER_IMPORTER_TOOLBAR_IMPROTER_H__

#include <string>
#include <vector>

#include "chrome/browser/importer/importer.h"
#include "chrome/browser/url_fetcher.h"

class XmlReader;

enum TOOLBAR_VERSION {
  NO_VERSION = -1,
  DEPRECATED,
  VERSION_4,
  VERSION_5
};

class ToolbarImporterUtils {
 public:

  static bool IsToolbarInstalled();
  static bool IsGoogleGAIACookieInstalled();
  static TOOLBAR_VERSION GetToolbarVersion();

 private:
  static const HKEY kToolbarInstallRegistryRoots[2];
  static const TCHAR* kToolbarRootRegistryFolder;
  static const TCHAR* kToolbarVersionRegistryFolder;
  static const TCHAR* kToolbarVersionRegistryKey;

  ToolbarImporterUtils() {}
  ~ToolbarImporterUtils() {}

  DISALLOW_COPY_AND_ASSIGN(ToolbarImporterUtils);
};

class Toolbar5Importer : public URLFetcher::Delegate,
                         public Importer {
 public:
  Toolbar5Importer();
  virtual ~Toolbar5Importer();

  // Importer view calls this method to being the process.
  virtual void StartImport(ProfileInfo profile_info,
                           uint16 items,
                           ProfileWriter* writer,
                           ImporterHost* host);

  // URLFetcher::Delegate method
  void OnURLFetchComplete(const URLFetcher* source,
                          const GURL& url,
                          const URLRequestStatus& status,
                          int response_code,
                          const ResponseCookies& cookies,
                          const std::string& data);

 private:
  // Internal state
  enum INTERNAL_STATE {
    NOT_USED = -1,
    INITIALIZED,
    GET_AUTHORIZATION_TOKEN,
    GET_BOOKMARKS,
    DONE
  };

  // URLs for connecting to the toolbar front end
  static const std::string kT5AuthorizationTokenUrl;
  static const std::string kT5FrontEndUrlTemplate;
  static const std::string kT4FrontEndUrlTemplate;

  // Token replacement tags
  static const std::string kRandomNumberToken;
  static const std::string kAuthorizationToken;
  static const std::string kAuthorizationTokenPrefix;
  static const std::string kAuthorizationTokenSuffix;
  static const std::string kMaxNumToken;
  static const std::string kMaxTimestampToken;

  // XML tag names
  static const std::string kXmlApiReplyXmlTag;
  static const std::string kBookmarksXmlTag;
  static const std::string kBookmarkXmlTag;
  static const std::string kTitleXmlTag;
  static const std::string kUrlXmlTag;
  static const std::string kTimestampXmlTag;
  static const std::string kLabelsXmlTag;
  static const std::string kLabelXmlTag;
  static const std::string kAttributesXmlTag;
  static const std::string kAttributeXmlTag;
  static const std::string kNameXmlTag;
  static const std::string kValueXmlTag;
  static const std::string kFaviconAttributeXmlName;

  // Flow control
  void ContinueImport();
  void EndImport();
  void BeginImportBookmarks();
  void EndImportBookmarks(bool success);

  // Network I/O
  void GetAuthenticationFromServer();
  void GetBookmarkDataFromServer(const std::string& response);
  void GetBookmarsFromServerDataResponse(const std::string& response);

  // XML Parsing
  bool ParseAuthenticationTokenResponse(const std::string& response,
                                        std::string* token);
  void ConstructFEConnectionString(const std::string& token,
                                   std::string* conn_string);

  bool ParseBookmarksFromReader(
      XmlReader* reader,
      std::vector< ProfileWriter::BookmarkEntry >* bookmarks,
      std::vector< history::ImportedFavIconUsage >* favicons);

  bool LocateNextTagByName(XmlReader* reader, const std::string& tag);

  bool ExtractBookmarkInformation(XmlReader* reader,
                                  ProfileWriter::BookmarkEntry* bookmark_entry,
                                  history::ImportedFavIconUsage* favicon_entry);
  bool ExtractNamedValueFromXmlReader(XmlReader* reader,
                                      const std::string& name,
                                      std::string* buffer);
  bool ExtractTitleFromXmlReader(XmlReader* reader,
                                 ProfileWriter::BookmarkEntry* entry);
  bool ExtractUrlFromXmlReader(XmlReader* reader,
                               ProfileWriter::BookmarkEntry* entry);
  bool ExtractTimeFromXmlReader(XmlReader* reader,
                                ProfileWriter::BookmarkEntry* entry);
  bool ExtractFolderFromXmlReader(XmlReader* reader,
                                  ProfileWriter::BookmarkEntry* entry);
  bool ExtractFaviconFromXmlReader(
      XmlReader* reader,
      ProfileWriter::BookmarkEntry* bookmark_entry,
      history::ImportedFavIconUsage* favicon_entry);

  // Bookmark creation
  void AddBookMarksToChrome(
    const std::vector< ProfileWriter::BookmarkEntry >& bookmarks,
    const std::vector< history::ImportedFavIconUsage >& favicons);

  // Hosts the writer used in this importer.
  ProfileWriter* writer_;

  // Internal state
  INTERNAL_STATE state_;

  // Bitmask of Importer::ImportItem
  uint16  items_to_import_;

  // The fetchers need to be available to cancel the network call on user cancel
  URLFetcher * token_fetcher_;
  URLFetcher * data_fetcher_;

  DISALLOW_COPY_AND_ASSIGN(Toolbar5Importer);
};

#endif  // CHROME_BROWSER_IMPORTER_TOOLBAR_IMPROTER_H__