Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / bookmarks / bookmark_html_writer.h
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 "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18
19 class BookmarkNode;
20 class Profile;
21
22 namespace chrome {
23 struct FaviconBitmapResult;
24 }
25
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;
31
32  protected:
33   virtual ~BookmarksExportObserver() {}
34 };
35
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;
44
45   BookmarkFaviconFetcher(Profile* profile,
46                          const base::FilePath& path,
47                          BookmarksExportObserver* observer);
48   virtual ~BookmarkFaviconFetcher();
49
50   // Executes bookmark export process.
51   void ExportBookmarks();
52
53   // content::NotificationObserver implementation.
54   virtual void Observe(int type,
55                        const content::NotificationSource& source,
56                        const content::NotificationDetails& details) OVERRIDE;
57
58  private:
59   // Recursively extracts URLs from bookmarks.
60   void ExtractUrls(const BookmarkNode* node);
61
62   // Executes Writer task that writes bookmarks data to html file.
63   void ExecuteWriter();
64
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();
69
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);
73
74   // The Profile object used for accessing FaviconService, bookmarks model.
75   Profile* profile_;
76
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_;
80
81   // Tracks favicon tasks.
82   base::CancelableTaskTracker cancelable_task_tracker_;
83
84   // Map that stores favicon per URL.
85   scoped_ptr<URLFaviconMap> favicons_map_;
86
87   // Path where html output is stored.
88   base::FilePath path_;
89
90   BookmarksExportObserver* observer_;
91
92   content::NotificationRegistrar registrar_;
93
94   DISALLOW_COPY_AND_ASSIGN(BookmarkFaviconFetcher);
95 };
96
97 namespace bookmark_html_writer {
98
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);
107
108 }  // namespace bookmark_html_writer
109
110 #endif  // CHROME_BROWSER_BOOKMARKS_BOOKMARK_HTML_WRITER_H_