Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / sync / glue / favicon_cache.h
blobba55695709e81ab42e361d3edbbd114e45ad0425
1 // Copyright (c) 2013 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_SYNC_GLUE_FAVICON_CACHE_H_
6 #define CHROME_BROWSER_SYNC_GLUE_FAVICON_CACHE_H_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/ref_counted_memory.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/scoped_observer.h"
19 #include "base/task/cancelable_task_tracker.h"
20 #include "components/history/core/browser/history_service_observer.h"
21 #include "components/history/core/browser/history_types.h"
22 #include "components/sessions/session_id.h"
23 #include "sync/api/sync_change.h"
24 #include "sync/api/sync_error_factory.h"
25 #include "sync/api/syncable_service.h"
26 #include "url/gurl.h"
28 class Profile;
30 namespace chrome {
31 struct FaviconRawBitmapResult;
34 namespace browser_sync {
36 enum IconSize {
37 SIZE_INVALID,
38 SIZE_16,
39 SIZE_32,
40 SIZE_64,
41 NUM_SIZES
44 struct SyncedFaviconInfo;
46 // Encapsulates the logic for loading and storing synced favicons.
47 // TODO(zea): make this a KeyedService.
48 class FaviconCache : public syncer::SyncableService,
49 public history::HistoryServiceObserver {
50 public:
51 FaviconCache(Profile* profile, int max_sync_favicon_limit);
52 ~FaviconCache() override;
54 // SyncableService implementation.
55 syncer::SyncMergeResult MergeDataAndStartSyncing(
56 syncer::ModelType type,
57 const syncer::SyncDataList& initial_sync_data,
58 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
59 scoped_ptr<syncer::SyncErrorFactory> error_handler) override;
60 void StopSyncing(syncer::ModelType type) override;
61 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
62 syncer::SyncError ProcessSyncChanges(
63 const tracked_objects::Location& from_here,
64 const syncer::SyncChangeList& change_list) override;
66 // If a valid favicon for the icon at |favicon_url| is found, fills
67 // |favicon_png| with the png-encoded image and returns true. Else, returns
68 // false.
69 bool GetSyncedFaviconForFaviconURL(
70 const GURL& favicon_url,
71 scoped_refptr<base::RefCountedMemory>* favicon_png) const;
73 // If a valid favicon for the icon associated with |page_url| is found, fills
74 // |favicon_png| with the png-encoded image and returns true. Else, returns
75 // false.
76 bool GetSyncedFaviconForPageURL(
77 const GURL& page_url,
78 scoped_refptr<base::RefCountedMemory>* favicon_png) const;
80 // Load the favicon for |page_url|. Will create a new sync node or update
81 // an existing one as necessary, and set the last visit time to the current
82 // time. Only those favicon types defined in SupportedFaviconTypes will be
83 // synced.
84 void OnPageFaviconUpdated(const GURL& page_url);
86 // Update the visit count for the favicon associated with |favicon_url|.
87 // If no favicon exists associated with |favicon_url|, triggers a load
88 // for the favicon associated with |page_url|.
89 void OnFaviconVisited(const GURL& page_url, const GURL& favicon_url);
91 // Consume Session sync favicon data. Will not overwrite existing favicons.
92 // If |icon_bytes| is empty, only updates the page->favicon url mapping.
93 // Safe to call within a transaction.
94 void OnReceivedSyncFavicon(const GURL& page_url,
95 const GURL& icon_url,
96 const std::string& icon_bytes,
97 int64 visit_time_ms);
99 private:
100 friend class SyncFaviconCacheTest;
101 FRIEND_TEST_ALL_PREFIXES(SyncFaviconCacheTest, HistoryFullClear);
102 FRIEND_TEST_ALL_PREFIXES(SyncFaviconCacheTest, HistorySubsetClear);
104 // Functor for ordering SyncedFaviconInfo objects by recency;
105 struct FaviconRecencyFunctor {
106 bool operator()(const linked_ptr<SyncedFaviconInfo>& lhs,
107 const linked_ptr<SyncedFaviconInfo>& rhs) const;
111 // Map of favicon url to favicon image.
112 typedef std::map<GURL, linked_ptr<SyncedFaviconInfo> > FaviconMap;
113 typedef std::set<linked_ptr<SyncedFaviconInfo>,
114 FaviconRecencyFunctor> RecencySet;
115 // Map of page url to task id (for favicon loading).
116 typedef std::map<GURL, base::CancelableTaskTracker::TaskId> PageTaskMap;
117 // Map of page url to favicon url.
118 typedef std::map<GURL, GURL> PageFaviconMap;
120 // Helper method to perform OnReceivedSyncFavicon work without worrying about
121 // whether caller holds a sync transaction.
122 void OnReceivedSyncFaviconImpl(const GURL& icon_url,
123 const std::string& icon_bytes,
124 int64 visit_time_ms);
126 // Callback method to store a tab's favicon into its sync node once it becomes
127 // available. Does nothing if no favicon data was available.
128 void OnFaviconDataAvailable(
129 const GURL& page_url,
130 const std::vector<favicon_base::FaviconRawBitmapResult>& bitmap_result);
132 // Helper method to update the sync state of the favicon at |icon_url|. If
133 // either |image_change_type| or |tracking_change_type| is ACTION_INVALID,
134 // the corresponding datatype won't be updated.
135 // Note: should only be called after both FAVICON_IMAGES and FAVICON_TRACKING
136 // have been successfully set up.
137 void UpdateSyncState(const GURL& icon_url,
138 syncer::SyncChange::SyncChangeType image_change_type,
139 syncer::SyncChange::SyncChangeType tracking_change_type);
141 // Helper method to get favicon info from |synced_favicons_|. If no info
142 // exists for |icon_url|, creates a new SyncedFaviconInfo in both
143 // |synced_favicons_| and |recent_favicons_| and returns it.
144 SyncedFaviconInfo* GetFaviconInfo(const GURL& icon_url);
146 // Updates the last visit time for the favicon at |icon_url| to |time| (and
147 // correspondly updates position in |recent_favicons_|.
148 void UpdateFaviconVisitTime(const GURL& icon_url, base::Time time);
150 // Expiration method. Looks through |recent_favicons_| to find any favicons
151 // that should be expired in order to maintain the sync favicon limit,
152 // appending deletions to |image_changes| and |tracking_changes| as necessary.
153 void ExpireFaviconsIfNecessary(syncer::SyncChangeList* image_changes,
154 syncer::SyncChangeList* tracking_changes);
156 // Returns the local favicon url associated with |sync_favicon| if one exists
157 // in |synced_favicons_|, else returns an invalid GURL.
158 GURL GetLocalFaviconFromSyncedData(
159 const syncer::SyncData& sync_favicon) const;
161 // Merges |sync_favicon| into |synced_favicons_|, updating |local_changes|
162 // with any changes that should be pushed to the sync processor.
163 void MergeSyncFavicon(const syncer::SyncData& sync_favicon,
164 syncer::SyncChangeList* sync_changes);
166 // Updates |synced_favicons_| with the favicon data from |sync_favicon|.
167 void AddLocalFaviconFromSyncedData(const syncer::SyncData& sync_favicon);
169 // Creates a SyncData object from the |type| data of |favicon_url|
170 // from within |synced_favicons_|.
171 syncer::SyncData CreateSyncDataFromLocalFavicon(
172 syncer::ModelType type,
173 const GURL& favicon_url) const;
175 // Deletes all synced favicons corresponding with |favicon_urls| and pushes
176 // the deletions to sync.
177 void DeleteSyncedFavicons(const std::set<GURL>& favicon_urls);
179 // Deletes the favicon pointed to by |favicon_iter| and appends the necessary
180 // sync deletions to |image_changes| and |tracking_changes|.
181 void DeleteSyncedFavicon(FaviconMap::iterator favicon_iter,
182 syncer::SyncChangeList* image_changes,
183 syncer::SyncChangeList* tracking_changes);
185 // Locally drops the favicon pointed to by |favicon_iter|.
186 void DropSyncedFavicon(FaviconMap::iterator favicon_iter);
188 // Only drops the data associated with |type| of |favicon_iter|.
189 void DropPartialFavicon(FaviconMap::iterator favicon_iter,
190 syncer::ModelType type);
192 // For testing only.
193 size_t NumFaviconsForTest() const;
194 size_t NumTasksForTest() const;
196 // history::HistoryServiceObserver:
197 void OnURLsDeleted(history::HistoryService* history_service,
198 bool all_history,
199 bool expired,
200 const history::URLRows& deleted_rows,
201 const std::set<GURL>& favicon_urls) override;
203 // Trask tracker for loading favicons.
204 base::CancelableTaskTracker cancelable_task_tracker_;
206 // Our actual cached favicon data.
207 FaviconMap synced_favicons_;
209 // An LRU ordering of the favicons comprising |synced_favicons_| (oldest to
210 // newest).
211 RecencySet recent_favicons_;
213 // Our set of pending favicon loads, indexed by page url.
214 PageTaskMap page_task_map_;
216 // Map of page and associated favicon urls.
217 PageFaviconMap page_favicon_map_;
219 Profile* profile_;
221 // TODO(zea): consider creating a favicon handler here for fetching unsynced
222 // favicons from the web.
224 scoped_ptr<syncer::SyncChangeProcessor> favicon_images_sync_processor_;
225 scoped_ptr<syncer::SyncChangeProcessor> favicon_tracking_sync_processor_;
227 // Maximum number of favicons to sync. 0 means no limit.
228 const size_t max_sync_favicon_limit_;
230 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
231 history_service_observer_;
233 // Weak pointer factory for favicon loads.
234 base::WeakPtrFactory<FaviconCache> weak_ptr_factory_;
236 DISALLOW_COPY_AND_ASSIGN(FaviconCache);
239 } // namespace browser_sync
241 #endif // CHROME_BROWSER_SYNC_GLUE_FAVICON_CACHE_H_