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