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_
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"
24 struct FaviconBitmapResult
;
27 // Observer for bookmark html output. Used only in tests.
28 class BookmarksExportObserver
{
30 // Is invoked on the IO thread.
31 virtual void OnExportFinished() = 0;
34 virtual ~BookmarksExportObserver() {}
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.
40 class BookmarkFaviconFetcher
: public content::NotificationObserver
{
42 // Map of URL and corresponding favicons.
43 typedef std::map
<std::string
, scoped_refptr
<base::RefCountedMemory
> >
46 BookmarkFaviconFetcher(Profile
* profile
,
47 const base::FilePath
& path
,
48 BookmarksExportObserver
* observer
);
49 virtual ~BookmarkFaviconFetcher();
51 // Executes bookmark export process.
52 void ExportBookmarks();
54 // content::NotificationObserver implementation.
55 virtual void Observe(int type
,
56 const content::NotificationSource
& source
,
57 const content::NotificationDetails
& details
) OVERRIDE
;
60 // Recursively extracts URLs from bookmarks.
61 void ExtractUrls(const BookmarkNode
* node
);
63 // Executes Writer task that writes bookmarks data to html file.
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();
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
);
76 // The Profile object used for accessing FaviconService, bookmarks model.
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_
;
83 // Tracks favicon tasks.
84 base::CancelableTaskTracker cancelable_task_tracker_
;
86 // Map that stores favicon per URL.
87 scoped_ptr
<URLFaviconMap
> favicons_map_
;
89 // Path where html output is stored.
92 BookmarksExportObserver
* observer_
;
94 content::NotificationRegistrar registrar_
;
96 DISALLOW_COPY_AND_ASSIGN(BookmarkFaviconFetcher
);
99 namespace bookmark_html_writer
{
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
104 // Before writing to the file favicons are fetched on the main thread.
105 // TODO(sky): need a callback on failure.
106 void WriteBookmarks(Profile
* profile
,
107 const base::FilePath
& path
,
108 BookmarksExportObserver
* observer
);
110 } // namespace bookmark_html_writer
112 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_HTML_WRITER_H_