bookmark_html_writer.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_HTML_WRITER_H_
6#define CHROME_BROWSER_BOOKMARKS_BOOKMARK_HTML_WRITER_H_
7
8#include <list>
9#include <map>
10#include <string>
11
12#include "base/files/file_path.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/ref_counted_memory.h"
15#include "base/task/cancelable_task_tracker.h"
16#include "components/favicon_base/favicon_types.h"
17#include "content/public/browser/notification_observer.h"
18#include "content/public/browser/notification_registrar.h"
19
20class BookmarkNode;
21class Profile;
22
23namespace chrome {
24struct FaviconBitmapResult;
25}
26
27// Observer for bookmark html output. Used only in tests.
28class BookmarksExportObserver {
29 public:
30  // Is invoked on the IO thread.
31  virtual void OnExportFinished() = 0;
32
33 protected:
34  virtual ~BookmarksExportObserver() {}
35};
36
37// Class that fetches favicons for list of bookmarks and
38// then starts Writer which outputs bookmarks and favicons to html file.
39// Should be used only by WriteBookmarks function.
40class BookmarkFaviconFetcher: public content::NotificationObserver {
41 public:
42  // Map of URL and corresponding favicons.
43  typedef std::map<std::string, scoped_refptr<base::RefCountedMemory> >
44      URLFaviconMap;
45
46  BookmarkFaviconFetcher(Profile* profile,
47                         const base::FilePath& path,
48                         BookmarksExportObserver* observer);
49  virtual ~BookmarkFaviconFetcher();
50
51  // Executes bookmark export process.
52  void ExportBookmarks();
53
54  // content::NotificationObserver implementation.
55  virtual void Observe(int type,
56                       const content::NotificationSource& source,
57                       const content::NotificationDetails& details) OVERRIDE;
58
59 private:
60  // Recursively extracts URLs from bookmarks.
61  void ExtractUrls(const BookmarkNode* node);
62
63  // Executes Writer task that writes bookmarks data to html file.
64  void ExecuteWriter();
65
66  // Starts async fetch for the next bookmark favicon.
67  // Takes single url from bookmark_urls_ and removes it from the list.
68  // Returns true if there are more favicons to extract.
69  bool FetchNextFavicon();
70
71  // Favicon fetch callback. After all favicons are fetched executes
72  // html output on the file thread.
73  void OnFaviconDataAvailable(
74      const favicon_base::FaviconBitmapResult& bitmap_result);
75
76  // The Profile object used for accessing FaviconService, bookmarks model.
77  Profile* profile_;
78
79  // All URLs that are extracted from bookmarks. Used to fetch favicons
80  // for each of them. After favicon is fetched top url is removed from list.
81  std::list<std::string> bookmark_urls_;
82
83  // Tracks favicon tasks.
84  base::CancelableTaskTracker cancelable_task_tracker_;
85
86  // Map that stores favicon per URL.
87  scoped_ptr<URLFaviconMap> favicons_map_;
88
89  // Path where html output is stored.
90  base::FilePath path_;
91
92  BookmarksExportObserver* observer_;
93
94  content::NotificationRegistrar registrar_;
95
96  DISALLOW_COPY_AND_ASSIGN(BookmarkFaviconFetcher);
97};
98
99namespace bookmark_html_writer {
100
101// Writes the bookmarks out in the 'bookmarks.html' format understood by
102// Firefox and IE. The results are written to the file at |path|.  The file
103// thread is used.
104// Before writing to the file favicons are fetched on the main thread.
105// TODO(sky): need a callback on failure.
106void WriteBookmarks(Profile* profile,
107                    const base::FilePath& path,
108                    BookmarksExportObserver* observer);
109
110}  // namespace bookmark_html_writer
111
112#endif  // CHROME_BROWSER_BOOKMARKS_BOOKMARK_HTML_WRITER_H_
113