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

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

Issue 10817004: Adds speculative prefetching of resources. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Adding Unittest. Created 8 years, 3 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 (c) 2012 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 "base/bind.h"
8 #include "base/stl_util.h"
9 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "net/url_request/url_request.h"
12 #include "net/url_request/url_request_context_getter.h"
13
14 using content::BrowserThread;
15
16 namespace predictors {
17
18 ResourcePrefetcherManager::ResourcePrefetcherManager(
19 ResourcePrefetchPredictor* predictor,
20 const ResourcePrefetchPredictorConfig& config,
21 net::URLRequestContextGetter* context_getter)
22 : predictor_(predictor),
23 config_(config),
24 context_getter_(context_getter),
25 prefetcher_map_deleter_(&prefetcher_map_) {
26 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
27 CHECK(predictor_);
28 CHECK(context_getter_);
29 }
30
31 ResourcePrefetcherManager::~ResourcePrefetcherManager() {
32 }
33
34 void ResourcePrefetcherManager::ShutdownOnUIThread() {
35 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
36
37 predictor_ = NULL;
38 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
39 base::Bind(&ResourcePrefetcherManager::ShutdownOnIOThread,
40 this));
41 }
42
43 void ResourcePrefetcherManager::ShutdownOnIOThread() {
44 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
dominich 2012/09/11 17:40:11 is this necessary? It looks like you're posting a
Shishir 2012/09/11 18:18:08 This is not right. I think I overlooked this case
45 }
46
47 void ResourcePrefetcherManager::MaybeAddPrefetch(
48 const NavigationID& navigation_id,
49 scoped_ptr<ResourcePrefetcher::RequestVector> requests) {
50 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
51
52 const GURL& main_frame_url = navigation_id.main_frame_url;
53 PrefetcherMap::iterator prefetcher_it = prefetcher_map_.find(main_frame_url);
54 if (prefetcher_it != prefetcher_map_.end())
55 return;
56
57 ResourcePrefetcher* prefetcher = new ResourcePrefetcher(
58 this, config_, navigation_id, requests.Pass());
59 prefetcher_map_.insert(std::make_pair(main_frame_url, prefetcher));
60 prefetcher->Start();
61 }
62
63 void ResourcePrefetcherManager::MaybeRemovePrefetch(
64 const NavigationID& navigation_id) {
65 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
66
67 PrefetcherMap::iterator it = prefetcher_map_.find(
68 navigation_id.main_frame_url);
69 if (it != prefetcher_map_.end() &&
70 it->second->navigation_id() == navigation_id) {
71 it->second->Stop();
72 }
73 }
74
75 void ResourcePrefetcherManager::ResourcePrefetcherFinished(
76 ResourcePrefetcher* resource_prefetcher,
77 ResourcePrefetcher::RequestVector* requests) {
78 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
79
80 // |predictor_| can only be accessed from the UI thread.
81 scoped_ptr<ResourcePrefetcher::RequestVector> requests_ptr(requests);
82 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
83 base::Bind(&ResourcePrefetcherManager::ResourcePrefetcherFinishedOnUI,
84 this,
85 resource_prefetcher->navigation_id(),
86 base::Passed(&requests_ptr)));
87
88 const GURL& main_frame_url =
89 resource_prefetcher->navigation_id().main_frame_url;
90 PrefetcherMap::iterator it = prefetcher_map_.find(main_frame_url);
91 DCHECK(it != prefetcher_map_.end());
92 delete it->second;
93 prefetcher_map_.erase(main_frame_url);
94 }
95
96 void ResourcePrefetcherManager::ResourcePrefetcherFinishedOnUI(
97 const NavigationID& navigation_id,
98 scoped_ptr<ResourcePrefetcher::RequestVector> requests) {
99 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
100
101 // |predictor_| may have been set to NULL if the predictor is shutting down.
102 if (predictor_)
103 predictor_->FinishedPrefetchForNavigation(navigation_id,
104 requests.Pass());
105 }
106
107 net::URLRequestContext* ResourcePrefetcherManager::GetURLRequestContext() {
108 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
109
110 return context_getter_->GetURLRequestContext();
111 }
112
113 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698