| Index: chrome/browser/predictors/loading_predictor.h
|
| diff --git a/chrome/browser/predictors/loading_predictor.h b/chrome/browser/predictors/loading_predictor.h
|
| index 89f48bb407b630e6186b440795610738deab34e1..2aa3f6768548f182c5919c95852bbc393f9b49ac 100644
|
| --- a/chrome/browser/predictors/loading_predictor.h
|
| +++ b/chrome/browser/predictors/loading_predictor.h
|
| @@ -7,6 +7,7 @@
|
|
|
| #include <map>
|
| #include <memory>
|
| +#include <string>
|
| #include <utility>
|
|
|
| #include "base/gtest_prod_util.h"
|
| @@ -15,6 +16,8 @@
|
| #include "chrome/browser/predictors/loading_data_collector.h"
|
| #include "chrome/browser/predictors/resource_prefetch_common.h"
|
| #include "chrome/browser/predictors/resource_prefetch_predictor.h"
|
| +#include "chrome/browser/predictors/resource_prefetcher.h"
|
| +#include "chrome/browser/profiles/profile.h"
|
| #include "components/keyed_service/core/keyed_service.h"
|
| #include "url/gurl.h"
|
|
|
| @@ -24,6 +27,7 @@ namespace predictors {
|
|
|
| class ResourcePrefetchPredictor;
|
| class LoadingStatsCollector;
|
| +class TestLoadingObserver;
|
|
|
| // Entry point for the Loading predictor.
|
| // From a high-level request (GURL and motivation) and a database of historical
|
| @@ -36,7 +40,8 @@ class LoadingStatsCollector;
|
| // predictor.
|
| //
|
| // All methods must be called from the UI thread.
|
| -class LoadingPredictor : public KeyedService {
|
| +class LoadingPredictor : public KeyedService,
|
| + public ResourcePrefetcher::Delegate {
|
| public:
|
| LoadingPredictor(const LoadingPredictorConfig& config, Profile* profile);
|
| ~LoadingPredictor() override;
|
| @@ -69,6 +74,16 @@ class LoadingPredictor : public KeyedService {
|
| return weak_factory_.GetWeakPtr();
|
| }
|
|
|
| + // ResourcePrefetcher::Delegate:
|
| + void ResourcePrefetcherFinished(
|
| + ResourcePrefetcher* prefetcher,
|
| + std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats) override;
|
| +
|
| + // Sets the |observer| to be notified when prefetches start and
|
| + // finish. A previously registered observer will be discarded. Call this with
|
| + // a nullptr parameter to de-register the observer.
|
| + void SetObserverForTesting(TestLoadingObserver* observer);
|
| +
|
| private:
|
| // Cancels an active hint, from its iterator inside |active_hints_|. If the
|
| // iterator is .end(), does nothing. Returns the iterator after deletion of
|
| @@ -77,6 +92,16 @@ class LoadingPredictor : public KeyedService {
|
| std::map<GURL, base::TimeTicks>::iterator hint_it);
|
| void CleanupAbandonedHintsAndNavigations(const NavigationID& navigation_id);
|
|
|
| + // May start a prefetch for |url| with the data from |prediction|, and a
|
| + // given hint |origin|. A new prefetch may not start if there is already
|
| + // one in flight, for instance.
|
| + void MaybeAddPrefetch(const GURL& url,
|
| + const ResourcePrefetchPredictor::Prediction& prediction,
|
| + HintOrigin origin);
|
| +
|
| + // If a prefetch exists for |url|, stop it.
|
| + void MaybeRemovePrefetch(const GURL& url);
|
| +
|
| // For testing.
|
| void set_mock_resource_prefetch_predictor(
|
| std::unique_ptr<ResourcePrefetchPredictor> predictor) {
|
| @@ -91,6 +116,10 @@ class LoadingPredictor : public KeyedService {
|
| std::map<GURL, base::TimeTicks> active_hints_;
|
| // Initial URL.
|
| std::map<NavigationID, GURL> active_navigations_;
|
| + // The value is {prefetcher, already deleted}.
|
| + std::map<std::string, std::pair<std::unique_ptr<ResourcePrefetcher>, bool>>
|
| + prefetches_;
|
| + TestLoadingObserver* observer_;
|
|
|
| friend class LoadingPredictorTest;
|
| FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest,
|
| @@ -109,6 +138,30 @@ class LoadingPredictor : public KeyedService {
|
| DISALLOW_COPY_AND_ASSIGN(LoadingPredictor);
|
| };
|
|
|
| +// An interface used to notify that data in the LoadingPredictor has
|
| +// changed. All methods are called on the UI thread.
|
| +class TestLoadingObserver {
|
| + public:
|
| + // De-registers itself from |predictor_| on destruction.
|
| + virtual ~TestLoadingObserver();
|
| +
|
| + virtual void OnPrefetchingStarted(const GURL& main_frame_url) {}
|
| +
|
| + virtual void OnPrefetchingStopped(const GURL& main_frame_url) {}
|
| +
|
| + virtual void OnPrefetchingFinished(const GURL& main_frame_url) {}
|
| +
|
| + protected:
|
| + // |predictor| must be non-NULL and has to outlive the LoadingTestObserver.
|
| + // Also the predictor must not have a LoadingTestObserver set.
|
| + explicit TestLoadingObserver(LoadingPredictor* predictor);
|
| +
|
| + private:
|
| + LoadingPredictor* predictor_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(TestLoadingObserver);
|
| +};
|
| +
|
| } // namespace predictors
|
|
|
| #endif // CHROME_BROWSER_PREDICTORS_LOADING_PREDICTOR_H_
|
|
|