Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: chrome/browser/predictors/resource_prefetcher_manager.cc

Issue 2928033003: predictors: move ResourcePrefetcher handling to LoadingPredictor. (Closed)
Patch Set: Address comment. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 #include "chrome/browser/predictors/resource_prefetcher_manager.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/stl_util.h"
12 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_context_getter.h"
16
17 using content::BrowserThread;
18
19 namespace predictors {
20
21 ResourcePrefetcherManager::ResourcePrefetcherManager(
22 ResourcePrefetchPredictor* predictor,
23 const LoadingPredictorConfig& config,
24 net::URLRequestContextGetter* context_getter)
25 : predictor_(predictor), config_(config), context_getter_(context_getter) {
26 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
27 CHECK(predictor_);
28 CHECK(context_getter_);
29 }
30
31 ResourcePrefetcherManager::~ResourcePrefetcherManager() {
32 DCHECK(prefetcher_map_.empty())
33 << "Did not call ShutdownOnUIThread or ShutdownOnIOThread. "
34 " Will leak Prefetcher pointers.";
35 }
36
37 void ResourcePrefetcherManager::ShutdownOnUIThread() {
38 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
39
40 predictor_ = NULL;
41 BrowserThread::PostTask(
42 BrowserThread::IO, FROM_HERE,
43 base::BindOnce(&ResourcePrefetcherManager::ShutdownOnIOThread, this));
44 }
45
46 void ResourcePrefetcherManager::ShutdownOnIOThread() {
47 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
48 prefetcher_map_.clear();
49 }
50
51 void ResourcePrefetcherManager::MaybeAddPrefetch(
52 const GURL& main_frame_url,
53 const std::vector<GURL>& urls) {
54 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
55
56 // Don't add a duplicate prefetch for the same host.
57 std::string key = main_frame_url.host();
58 if (base::ContainsKey(prefetcher_map_, key))
59 return;
60
61 auto prefetcher = base::MakeUnique<ResourcePrefetcher>(
62 this, config_.max_prefetches_inflight_per_navigation,
63 config_.max_prefetches_inflight_per_host_per_navigation, main_frame_url,
64 urls);
65 prefetcher->Start();
66 prefetcher_map_[key] = std::move(prefetcher);
67 }
68
69 void ResourcePrefetcherManager::MaybeRemovePrefetch(
70 const GURL& main_frame_url) {
71 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
72
73 std::string key = main_frame_url.host();
74 auto it = prefetcher_map_.find(key);
75 if (it != prefetcher_map_.end() &&
76 it->second->main_frame_url() == main_frame_url) {
77 it->second->Stop();
78 }
79 }
80
81 void ResourcePrefetcherManager::ResourcePrefetcherFinished(
82 ResourcePrefetcher* resource_prefetcher,
83 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats) {
84 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
85
86 const GURL& main_frame_url = resource_prefetcher->main_frame_url();
87 BrowserThread::PostTask(
88 BrowserThread::UI, FROM_HERE,
89 base::BindOnce(&ResourcePrefetchPredictor::OnPrefetchingFinished,
90 base::Unretained(predictor_), main_frame_url,
91 base::Passed(std::move(stats))));
92
93 const std::string key = main_frame_url.host();
94 auto it = prefetcher_map_.find(key);
95 DCHECK(it != prefetcher_map_.end());
96 prefetcher_map_.erase(it);
97 }
98
99 net::URLRequestContext* ResourcePrefetcherManager::GetURLRequestContext() {
100 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
101
102 return context_getter_->GetURLRequestContext();
103 }
104
105 } // namespace predictors
OLDNEW
« no previous file with comments | « chrome/browser/predictors/resource_prefetcher_manager.h ('k') | chrome/browser/predictors/resource_prefetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698