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