Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / bookmarks / bookmark_html_writer.h
blob36330bf6acd77d502e0dd0fe5a788210fcac992b
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 "components/favicon_base/favicon_types.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
20 class Profile;
22 namespace bookmarks {
23 class BookmarkNode;
26 namespace chrome {
27 struct FaviconRawBitmapResult;
30 // Observer for bookmark html output. Used only in tests.
31 class BookmarksExportObserver {
32 public:
33 // Is invoked on the IO thread.
34 virtual void OnExportFinished() = 0;
36 protected:
37 virtual ~BookmarksExportObserver() {}
40 // Class that fetches favicons for list of bookmarks and
41 // then starts Writer which outputs bookmarks and favicons to html file.
42 // Should be used only by WriteBookmarks function.
43 class BookmarkFaviconFetcher: public content::NotificationObserver {
44 public:
45 // Map of URL and corresponding favicons.
46 typedef std::map<std::string, scoped_refptr<base::RefCountedMemory> >
47 URLFaviconMap;
49 BookmarkFaviconFetcher(Profile* profile,
50 const base::FilePath& path,
51 BookmarksExportObserver* observer);
52 ~BookmarkFaviconFetcher() override;
54 // Executes bookmark export process.
55 void ExportBookmarks();
57 // content::NotificationObserver implementation.
58 void Observe(int type,
59 const content::NotificationSource& source,
60 const content::NotificationDetails& details) override;
62 private:
63 // Recursively extracts URLs from bookmarks.
64 void ExtractUrls(const bookmarks::BookmarkNode* node);
66 // Executes Writer task that writes bookmarks data to html file.
67 void ExecuteWriter();
69 // Starts async fetch for the next bookmark favicon.
70 // Takes single url from bookmark_urls_ and removes it from the list.
71 // Returns true if there are more favicons to extract.
72 bool FetchNextFavicon();
74 // Favicon fetch callback. After all favicons are fetched executes
75 // html output on the file thread.
76 void OnFaviconDataAvailable(
77 const favicon_base::FaviconRawBitmapResult& bitmap_result);
79 // The Profile object used for accessing FaviconService, bookmarks model.
80 Profile* profile_;
82 // All URLs that are extracted from bookmarks. Used to fetch favicons
83 // for each of them. After favicon is fetched top url is removed from list.
84 std::list<std::string> bookmark_urls_;
86 // Tracks favicon tasks.
87 base::CancelableTaskTracker cancelable_task_tracker_;
89 // Map that stores favicon per URL.
90 scoped_ptr<URLFaviconMap> favicons_map_;
92 // Path where html output is stored.
93 base::FilePath path_;
95 BookmarksExportObserver* observer_;
97 content::NotificationRegistrar registrar_;
99 DISALLOW_COPY_AND_ASSIGN(BookmarkFaviconFetcher);
102 namespace bookmark_html_writer {
104 // Writes the bookmarks out in the 'bookmarks.html' format understood by
105 // Firefox and IE. The results are written to the file at |path|. The file
106 // thread is used.
107 // Before writing to the file favicons are fetched on the main thread.
108 // TODO(sky): need a callback on failure.
109 void WriteBookmarks(Profile* profile,
110 const base::FilePath& path,
111 BookmarksExportObserver* observer);
113 } // namespace bookmark_html_writer
115 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_HTML_WRITER_H_