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
|
// Copyright 2013 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 COMPONENTS_NTP_TILES_MOST_VISITED_SITES_H_
#define COMPONENTS_NTP_TILES_MOST_VISITED_SITES_H_
#include <stddef.h>
#include <memory>
#include <vector>
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
#include "base/strings/string16.h"
#include "components/history/core/browser/history_types.h"
#include "components/history/core/browser/top_sites_observer.h"
#include "components/ntp_tiles/ntp_tile.h"
#include "components/ntp_tiles/popular_sites.h"
#include "components/suggestions/proto/suggestions.pb.h"
#include "components/suggestions/suggestions_service.h"
#include "url/gurl.h"
namespace history {
class TopSites;
}
namespace user_prefs {
class PrefRegistrySyncable;
}
class PrefService;
namespace ntp_tiles {
class IconCacher;
// Shim interface for SupervisedUserService.
class MostVisitedSitesSupervisor {
public:
struct Whitelist {
base::string16 title;
GURL entry_point;
base::FilePath large_icon_path;
};
class Observer {
public:
virtual void OnBlockedSitesChanged() = 0;
protected:
~Observer() {}
};
virtual ~MostVisitedSitesSupervisor() {}
// Pass non-null to set observer, or null to remove observer.
// If setting observer, there must not yet be an observer set.
// If removing observer, there must already be one to remove.
// Does not take ownership. Observer must outlive this object.
virtual void SetObserver(Observer* new_observer) = 0;
// If true, |url| should not be shown on the NTP.
virtual bool IsBlocked(const GURL& url) = 0;
// Explicitly-specified sites to show on NTP.
virtual std::vector<Whitelist> whitelists() = 0;
// If true, be conservative about suggesting sites from outside sources.
virtual bool IsChildProfile() = 0;
};
// Tracks the list of most visited sites and their thumbnails.
class MostVisitedSites : public history::TopSitesObserver,
public MostVisitedSitesSupervisor::Observer {
public:
// The observer to be notified when the list of most visited sites changes.
class Observer {
public:
virtual void OnMostVisitedURLsAvailable(const NTPTilesVector& tiles) = 0;
virtual void OnIconMadeAvailable(const GURL& site_url) = 0;
protected:
virtual ~Observer() {}
};
// Construct a MostVisitedSites instance.
//
// |prefs| and |suggestions| are required and may not be null. |top_sites|,
// |popular_sites|, and |supervisor| are optional and if null the associated
// features will be disabled.
MostVisitedSites(PrefService* prefs,
scoped_refptr<history::TopSites> top_sites,
suggestions::SuggestionsService* suggestions,
std::unique_ptr<PopularSites> popular_sites,
std::unique_ptr<IconCacher> icon_cacher,
std::unique_ptr<MostVisitedSitesSupervisor> supervisor);
~MostVisitedSites() override;
// Sets the observer, and immediately fetches the current suggestions.
// Does not take ownership of |observer|, which must outlive this object and
// must not be null.
void SetMostVisitedURLsObserver(Observer* observer, int num_sites);
// Requests an asynchronous refresh of the suggestions. Notifies the observer
// once the request completes.
void Refresh();
void AddOrRemoveBlacklistedUrl(const GURL& url, bool add_url);
void ClearBlacklistedUrls();
// MostVisitedSitesSupervisor::Observer implementation.
void OnBlockedSitesChanged() override;
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
// Workhorse for SaveNewTiles. Implemented as a separate static and public
// method for ease of testing.
static NTPTilesVector MergeTiles(NTPTilesVector personal_tiles,
NTPTilesVector whitelist_tiles,
NTPTilesVector popular_tiles);
private:
void BuildCurrentTiles();
// Initialize the query to Top Sites. Called if the SuggestionsService
// returned no data.
void InitiateTopSitesQuery();
// If there's a whitelist entry point for the URL, return the large icon path.
base::FilePath GetWhitelistLargeIconPath(const GURL& url);
// Callback for when data is available from TopSites.
void OnMostVisitedURLsAvailable(
const history::MostVisitedURLList& visited_list);
// Callback for when data is available from the SuggestionsService.
void OnSuggestionsProfileAvailable(
const suggestions::SuggestionsProfile& suggestions_profile);
// Takes the personal suggestions and creates whitelist entry point
// suggestions if necessary.
NTPTilesVector CreateWhitelistEntryPointTiles(
const NTPTilesVector& personal_tiles);
// Takes the personal and whitelist tiles and creates popular tiles if
// necessary.
NTPTilesVector CreatePopularSitesTiles(const NTPTilesVector& personal_tiles,
const NTPTilesVector& whitelist_tiles);
// Takes the personal tiles, creates and merges in whitelist and popular tiles
// if appropriate, and saves the new tiles.
void SaveNewTiles(NTPTilesVector personal_tiles);
// Notifies the observer about the availability of tiles.
// Also records impressions UMA if not done already.
void NotifyMostVisitedURLsObserver();
void OnPopularSitesDownloaded(bool success);
void OnIconMadeAvailable(const GURL& site_url, bool newly_available);
// history::TopSitesObserver implementation.
void TopSitesLoaded(history::TopSites* top_sites) override;
void TopSitesChanged(history::TopSites* top_sites,
ChangeReason change_reason) override;
PrefService* prefs_;
scoped_refptr<history::TopSites> top_sites_;
suggestions::SuggestionsService* suggestions_service_;
std::unique_ptr<PopularSites> const popular_sites_;
std::unique_ptr<IconCacher> const icon_cacher_;
std::unique_ptr<MostVisitedSitesSupervisor> supervisor_;
Observer* observer_;
// The maximum number of most visited sites to return.
int num_sites_;
std::unique_ptr<
suggestions::SuggestionsService::ResponseCallbackList::Subscription>
suggestions_subscription_;
ScopedObserver<history::TopSites, history::TopSitesObserver>
top_sites_observer_;
// The main source of personal tiles - either TOP_SITES or SUGGESTIONS_SEVICE.
NTPTileSource mv_source_;
NTPTilesVector current_tiles_;
// For callbacks may be run after destruction, used exclusively for TopSites
// (since it's used to detect whether there's a query in flight).
base::WeakPtrFactory<MostVisitedSites> top_sites_weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(MostVisitedSites);
};
} // namespace ntp_tiles
#endif // COMPONENTS_NTP_TILES_MOST_VISITED_SITES_H_
|