summaryrefslogtreecommitdiffstats
path: root/chrome/browser/bookmarks/bookmark_html_writer.h
blob: 58da351273813e885374b76949e3fcf8c82c3105 (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
// Copyright (c) 2010 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_BOOKMARKS_BOOKMARK_HTML_WRITER_H_
#define CHROME_BROWSER_BOOKMARKS_BOOKMARK_HTML_WRITER_H_

#include <list>
#include <map>
#include <string>

#include "base/ref_counted.h"
#include "base/values.h"
#include "chrome/browser/history/history.h"
#include "chrome/common/notification_registrar.h"
#include "net/base/file_stream.h"

class BookmarkModel;
class BookmarkNode;
class DictionaryValue;
class FilePath;
class GURL;
class Profile;

// Observer for bookmark html output. Used only in tests.
class BookmarksExportObserver {
 public:
  // Is invoked on the IO thread.
  virtual void OnExportFinished() = 0;

 protected:
  virtual ~BookmarksExportObserver() {}
};

// Class that fetches favicons for list of bookmarks and
// then starts Writer which outputs bookmarks and favicons to html file.
// Should be used only by WriteBookmarks function.
class BookmarkFaviconFetcher: public NotificationObserver {
 public:
  // Map of URL and corresponding favicons.
  typedef std::map<std::string, scoped_refptr<RefCountedMemory> > URLFaviconMap;

  BookmarkFaviconFetcher(Profile* profile,
                         const FilePath& path,
                         BookmarksExportObserver* observer);
  ~BookmarkFaviconFetcher();

  // Executes bookmark export process.
  void ExportBookmarks();

  // NotificationObserver implementation.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

 private:
  // Recursively extracts URLs from bookmarks.
  void ExtractUrls(const BookmarkNode* node);

  // Executes Writer task that writes bookmarks data to html file.
  void ExecuteWriter();

  // Starts async fetch for the next bookmark favicon.
  // Takes single url from bookmark_urls_ and removes it from the list.
  // Returns true if there are more favicons to extract.
  bool FetchNextFavicon();

  // Favicon fetch callback. After all favicons are fetched executes
  // html output on the file thread.
  void OnFavIconDataAvailable(FaviconService::Handle handle,
                              bool know_favicon,
                              scoped_refptr<RefCountedMemory> data,
                              bool expired,
                              GURL icon_url);

  // The Profile object used for accessing FaviconService, bookmarks model.
  Profile* profile_;

  // All URLs that are extracted from bookmarks. Used to fetch favicons
  // for each of them. After favicon is fetched top url is removed from list.
  std::list<std::string> bookmark_urls_;

  // Consumer for requesting favicons.
  CancelableRequestConsumer fav_icon_consumer_;

  // Map that stores favicon per URL.
  scoped_ptr<URLFaviconMap> favicons_map_;

  // Path where html output is stored.
  FilePath path_;

  BookmarksExportObserver* observer_;

  NotificationRegistrar registrar_;

  DISALLOW_COPY_AND_ASSIGN(BookmarkFaviconFetcher);
};

namespace bookmark_html_writer {

// Writes the bookmarks out in the 'bookmarks.html' format understood by
// Firefox and IE. The results are written to the file at |path|.  The file
// thread is used.
// Before writing to the file favicons are fetched on the main thread.
// TODO(sky): need a callback on failure.
void WriteBookmarks(Profile* profile,
                    const FilePath& path,
                    BookmarksExportObserver* observer);

}  // namespace bookmark_html_writer

#endif  // CHROME_BROWSER_BOOKMARKS_BOOKMARK_HTML_WRITER_H_