summaryrefslogtreecommitdiffstats
path: root/chrome/browser/history/top_sites.h
blob: 99d20ce98b51a89761ebd06ec25dc06a12638598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_HISTORY_TOP_SITES_H_
#define CHROME_BROWSER_HISTORY_TOP_SITES_H_
#pragma once

#include <map>
#include <set>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "base/lock.h"
#include "base/timer.h"
#include "base/ref_counted.h"
#include "base/ref_counted_memory.h"
#include "chrome/browser/cancelable_request.h"
#include "chrome/browser/history/history_types.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/history/page_usage_data.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/thumbnail_score.h"
#include "googleurl/src/gurl.h"

class DictionaryValue;
class SkBitmap;
class Profile;

namespace history {

class TopSitesBackend;
class TopSitesDatabase;
class TopSitesTest;

typedef std::vector<MostVisitedURL> MostVisitedURLList;

// Stores the data for the top "most visited" sites. This includes a cache of
// the most visited data from history, as well as the corresponding thumbnails
// of those sites.
//
// This class IS threadsafe. It is designed to be used from the UI thread of
// the browser (where history requests must be kicked off and received from)
// and from the I/O thread (where new tab page requests come in). Handling the
// new tab page requests on the I/O thread without proxying to the UI thread is
// a nontrivial performance win, especially when the browser is starting and
// the UI thread is busy.
class TopSites : public NotificationObserver,
                 public base::RefCountedThreadSafe<TopSites>,
                 public CancelableRequestProvider {
 public:
  explicit TopSites(Profile* profile);

  class MockHistoryService {
    // A mockup of a HistoryService used for testing TopSites.
   public:
    virtual HistoryService::Handle QueryMostVisitedURLs(
        int result_count, int days_back,
        CancelableRequestConsumerBase* consumer,
        HistoryService::QueryMostVisitedURLsCallback* callback) = 0;
    virtual ~MockHistoryService() {}
    virtual void GetPageThumbnail(
        const GURL& page_url,
        CancelableRequestConsumerTSimple<size_t>* consumer,
        HistoryService::ThumbnailDataCallback* callback,
        size_t index) = 0;
  };

  struct Images {
    scoped_refptr<RefCountedBytes> thumbnail;
    ThumbnailScore thumbnail_score;

    // TODO(brettw): this will eventually store the favicon.
    // scoped_refptr<RefCountedBytes> favicon;
  };

  // Initializes TopSites.
  void Init(const FilePath& db_name);

  // Sets the given thumbnail for the given URL. Returns true if the thumbnail
  // was updated. False means either the URL wasn't known to us, or we felt
  // that our current thumbnail was superior to the given one.
  bool SetPageThumbnail(const GURL& url,
                        const SkBitmap& thumbnail,
                        const ThumbnailScore& score);

  // Callback for GetMostVisitedURLs.
  typedef Callback1<const MostVisitedURLList&>::Type GetTopSitesCallback;

  // Returns a list of most visited URLs via a callback.
  // NOTE: the callback may be called immediately if we have the data cached.
  void GetMostVisitedURLs(CancelableRequestConsumer* consumer,
                          GetTopSitesCallback* callback);

  // Get a thumbnail for a given page. Returns true iff we have the thumbnail.
  bool GetPageThumbnail(const GURL& url, RefCountedBytes** data) const;

  // For testing with a HistoryService mock.
  void SetMockHistoryService(MockHistoryService* mhs);

  // Start reading thumbnails from the ThumbnailDatabase.
  void StartMigration();

  // Blacklisted URLs

  // Add a  URL to the blacklist.
  void AddBlacklistedURL(const GURL& url);

  // Returns true if the URL is blacklisted.
  bool IsBlacklisted(const GURL& url);

  // Removes a URL from the blacklist.
  void RemoveBlacklistedURL(const GURL& url);

  // Clear the blacklist.
  void ClearBlacklistedURLs();

  // Pinned URLs

  // Pin a URL at |index|.
  void AddPinnedURL(const GURL& url, size_t index);

  // Get the index of a URL. Returns true if |url| is pinned.
  bool GetIndexOfPinnedURL(const GURL& url, size_t* index);

  // Returns true if a URL is pinned.
  bool IsURLPinned(const GURL& url);

  // Unpin a URL.
  void RemovePinnedURL(const GURL& url);

  // Return a URL pinned at |index| via |out|. Returns true if there
  // is a URL pinned at |index|.
  bool GetPinnedURLAtIndex(size_t index, GURL* out);

 private:
  friend class base::RefCountedThreadSafe<TopSites>;
  friend class TopSitesTest;
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, GetMostVisited);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, RealDatabase);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, MockDatabase);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, DeleteNotifications);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, GetUpdateDelay);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, Migration);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, QueueingRequestsForTopSites);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, CancelingRequestsForTopSites);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, AddTemporaryThumbnail);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, Blacklisting);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, PinnedURLs);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, BlacklistingAndPinnedURLs);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, AddPrepopulatedPages);
  FRIEND_TEST_ALL_PREFIXES(TopSitesTest, GetIndexForChromeStore);

  ~TopSites();

  // Sets the thumbnail without writing to the database. Useful when
  // reading last known top sites from the DB.
  // Returns true if the thumbnail was set, false if the existing one is better.
  bool SetPageThumbnailNoDB(const GURL& url,
                            const RefCountedBytes* thumbnail_data,
                            const ThumbnailScore& score);

  // A version of SetPageThumbnail that takes RefCountedBytes as
  // returned by HistoryService.
  bool SetPageThumbnail(const GURL& url,
                        const RefCountedBytes* thumbnail,
                        const ThumbnailScore& score);

  // Query history service for the list of available thumbnails.
  void StartQueryForMostVisited();

  // Query history service for the thumbnail for a given url. |index|
  // is the index into top_sites_.
  void StartQueryForThumbnail(size_t index);

  // Called when history service returns a list of top URLs.
  void OnTopSitesAvailable(CancelableRequestProvider::Handle handle,
                           MostVisitedURLList data);

  // Called when history service returns a thumbnail.
  void OnThumbnailAvailable(CancelableRequestProvider::Handle handle,
                            scoped_refptr<RefCountedBytes> thumbnail);

  // Saves the set of the top URLs visited by this user. The 0th item is the
  // most popular.
  // DANGER! This will clear all data from the input argument.
  void StoreMostVisited(MostVisitedURLList* most_visited);

  // Saves the given set of redirects. The redirects are in order of the
  // given vector, so [0] -> [1] -> [2].
  void StoreRedirectChain(const RedirectList& redirects,
                          size_t destination);

  // Each item in the most visited view can redirect elsewhere. This returns
  // the canonical URL one identifying the site if the given URL does appear
  // in the "top sites" list.
  //
  // If the given URL is not in the top sites, this will return an empty GURL.
  GURL GetCanonicalURL(const GURL& url) const;

  // Finds the given URL in the redirect chain for the given TopSite, and
  // returns the distance from the destination in hops that the given URL is.
  // The URL is assumed to be in the list. The destination is 0.
  static int GetRedirectDistanceForURL(const MostVisitedURL& most_visited,
                                       const GURL& url);

  // Generates the diff of things that happened between "old" and "new."
  //
  // The URLs that are in "new" but not "old" will be have their index into
  // "new" put in |added_urls|. The URLs that are in "old" but not "new" will
  // have their index into "old" put into |deleted_urls|.
  //
  // URLs appearing in both old and new lists but having different indices will
  // have their index into "new" be put into |moved_urls|.
  static void DiffMostVisited(const MostVisitedURLList& old_list,
                              const MostVisitedURLList& new_list,
                              std::vector<size_t>* added_urls,
                              std::vector<size_t>* deleted_urls,
                              std::vector<size_t>* moved_urls);

  // Implementation of NotificationObserver.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

  // Returns the delay until the next update of history is needed.
  // Uses num_urls_changed
  base::TimeDelta GetUpdateDelay();

  // The following methods must be run on the DB thread since they
  // access the database.

  // Reads the database from disk. Called on startup to get the last
  // known top sites.
  void ReadDatabase();

  // Write a thumbnail to database.
  void WriteThumbnailToDB(const MostVisitedURL& url,
                          int url_rank,
                          const TopSites::Images& thumbnail);

  // Updates the top sites list and writes the difference to disk.
  void UpdateMostVisited(MostVisitedURLList most_visited);

  // Deletes the database file, then reinitializes the database.
  void ResetDatabase();

  // Called after TopSites completes migration.
  void OnMigrationDone();

  // Add a thumbnail for an unknown url. See temp_thumbnails_map_.
  void AddTemporaryThumbnail(const GURL& url,
                             const RefCountedBytes* thumbnail,
                             const ThumbnailScore& score);

  // Returns an index of a thumbnail that should be replaced by the
  // Chrome App Store. Returns -1 App Store should not be added.
  int GetIndexForChromeStore(const MostVisitedURLList& urls);

  // Adds Chrome App Store thumbnail to a list of URLs, if possible.
  // Returns true if it was added.
  bool AddChromeStore(MostVisitedURLList* urls);

  // Add prepopulated pages: 'welcome to Chrome' and themes gallery.
  // Returns true if any pages were added.
  bool AddPrepopulatedPages(MostVisitedURLList* urls);

  // Convert pinned_urls_ dictionary to the new format. Use URLs as
  // dictionary keys.
  void MigratePinnedURLs();

  // Takes |urls|, produces it's copy in |out| after removing
  // blacklisted URLs and reordering pinned URLs.
  void ApplyBlacklistAndPinnedURLs(const MostVisitedURLList& urls,
                                   MostVisitedURLList* out);

  // Converts a url into a canonical string representation.
  std::wstring GetURLString(const GURL& url);

  // Returns an MD5 hash of the URL. Hashing is required for blacklisted URLs.
  std::wstring GetURLHash(const GURL& url);

  Profile* profile_;
  // A mockup to use for testing. If NULL, use the real HistoryService
  // from the profile_. See SetMockHistoryService.
  MockHistoryService* mock_history_service_;
  CancelableRequestConsumerTSimple<size_t> cancelable_consumer_;
  mutable Lock lock_;

  // The cached version of the top sites. The 0th item in this vector is the
  // #1 site.
  MostVisitedURLList top_sites_;

  // The images corresponding to the top_sites. This is indexed by the URL of
  // the top site, so this doesn't have to be shuffled around when the ordering
  // changes of the top sites. Some top_sites_ entries may not have images.
  std::map<GURL, Images> top_images_;

  // Generated from the redirects to and from the most visited pages, this
  // maps the redirects to the index into top_sites_ that contains it.
  std::map<GURL, size_t> canonical_urls_;

  // Timer for updating TopSites data.
  base::OneShotTimer<TopSites> timer_;

  scoped_ptr<TopSitesDatabase> db_;
  FilePath db_path_;

  NotificationRegistrar registrar_;

  // The number of URLs changed on the last update.
  size_t last_num_urls_changed_;

  // Are we in the middle of migration from ThumbnailsDatabase to
  // TopSites?
  bool migration_in_progress_;

  // URLs for which we are expecting thumbnails.
  std::set<GURL> migration_pending_urls_;

  // The map of requests for the top sites list. Can only be
  // non-empty at startup. After we read the top sites from the DB, we'll
  // always have a cached list.
  typedef std::set<scoped_refptr<CancelableRequest<GetTopSitesCallback> > >
      PendingCallbackSet;
  PendingCallbackSet pending_callbacks_;

  // Are we waiting for the top sites from HistoryService?
  bool waiting_for_results_;

  // Stores thumbnails for unknown pages. When SetPageThumbnail is
  // called, if we don't know about that URL yet and we don't have
  // enough Top Sites (new profile), we store it until the next
  // UpdateMostVisitedURLs call.
  std::map<GURL, Images> temp_thumbnails_map_;

  // Blacklisted and pinned URLs are stored in Preferences.

  // Blacklisted URLs. They are filtered out from the list of Top
  // Sites when GetMostVisitedURLs is called. Note that we are still
  // storing all URLs, but filtering on access. It is a dictionary,
  // key is the URL, value is a dummy value. This is owned by the
  // PrefService.
  DictionaryValue* blacklist_;

  // This is a dictionary for the pinned URLs for the the most visited
  // part of the new tab page. Key is the URL, value is
  // index where it is pinned at (may be the same as key). This is
  // owned by the PrefService.
  DictionaryValue* pinned_urls_;

  DISALLOW_COPY_AND_ASSIGN(TopSites);
};

}  // namespace history

#endif  // CHROME_BROWSER_HISTORY_TOP_SITES_H_