OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/precache/content/precache_manager.h" | 5 #include "components/precache/content/precache_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 // static | 49 // static |
50 bool PrecacheManager::IsPrecachingEnabled() { | 50 bool PrecacheManager::IsPrecachingEnabled() { |
51 return base::FieldTrialList::FindFullName(kPrecacheFieldTrialName) == | 51 return base::FieldTrialList::FindFullName(kPrecacheFieldTrialName) == |
52 kPrecacheFieldTrialEnabledGroup || | 52 kPrecacheFieldTrialEnabledGroup || |
53 base::CommandLine::ForCurrentProcess()->HasSwitch( | 53 base::CommandLine::ForCurrentProcess()->HasSwitch( |
54 switches::kEnablePrecache); | 54 switches::kEnablePrecache); |
55 } | 55 } |
56 | 56 |
57 bool PrecacheManager::IsPrecachingAllowed() { | 57 bool PrecacheManager::IsPrecachingAllowed() { |
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
59 return user_prefs::UserPrefs::Get(browser_context_)->GetBoolean( | 59 return user_prefs::UserPrefs::Get(browser_context_)->GetBoolean( |
60 data_reduction_proxy::prefs::kDataReductionProxyEnabled); | 60 data_reduction_proxy::prefs::kDataReductionProxyEnabled); |
61 } | 61 } |
62 | 62 |
63 void PrecacheManager::StartPrecaching( | 63 void PrecacheManager::StartPrecaching( |
64 const PrecacheCompletionCallback& precache_completion_callback, | 64 const PrecacheCompletionCallback& precache_completion_callback, |
65 URLListProvider* url_list_provider) { | 65 URLListProvider* url_list_provider) { |
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 66 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
67 | 67 |
68 if (is_precaching_) { | 68 if (is_precaching_) { |
69 DLOG(WARNING) << "Cannot start precaching because precaching is already " | 69 DLOG(WARNING) << "Cannot start precaching because precaching is already " |
70 "in progress."; | 70 "in progress."; |
71 return; | 71 return; |
72 } | 72 } |
73 is_precaching_ = true; | 73 is_precaching_ = true; |
74 | 74 |
75 BrowserThread::PostTask( | 75 BrowserThread::PostTask( |
76 BrowserThread::DB, FROM_HERE, | 76 BrowserThread::DB, FROM_HERE, |
77 base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory, | 77 base::Bind(&PrecacheDatabase::DeleteExpiredPrecacheHistory, |
78 precache_database_, base::Time::Now())); | 78 precache_database_, base::Time::Now())); |
79 | 79 |
80 precache_completion_callback_ = precache_completion_callback; | 80 precache_completion_callback_ = precache_completion_callback; |
81 | 81 |
82 url_list_provider->GetURLs( | 82 url_list_provider->GetURLs( |
83 base::Bind(&PrecacheManager::OnURLsReceived, AsWeakPtr())); | 83 base::Bind(&PrecacheManager::OnURLsReceived, AsWeakPtr())); |
84 } | 84 } |
85 | 85 |
86 void PrecacheManager::CancelPrecaching() { | 86 void PrecacheManager::CancelPrecaching() { |
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 87 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
88 | 88 |
89 if (!is_precaching_) { | 89 if (!is_precaching_) { |
90 // Do nothing if precaching is not in progress. | 90 // Do nothing if precaching is not in progress. |
91 return; | 91 return; |
92 } | 92 } |
93 is_precaching_ = false; | 93 is_precaching_ = false; |
94 | 94 |
95 // Destroying the |precache_fetcher_| will cancel any fetch in progress. | 95 // Destroying the |precache_fetcher_| will cancel any fetch in progress. |
96 precache_fetcher_.reset(); | 96 precache_fetcher_.reset(); |
97 | 97 |
98 // Uninitialize the callback so that any scoped_refptrs in it are released. | 98 // Uninitialize the callback so that any scoped_refptrs in it are released. |
99 precache_completion_callback_.Reset(); | 99 precache_completion_callback_.Reset(); |
100 } | 100 } |
101 | 101 |
102 bool PrecacheManager::IsPrecaching() const { | 102 bool PrecacheManager::IsPrecaching() const { |
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 103 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
104 return is_precaching_; | 104 return is_precaching_; |
105 } | 105 } |
106 | 106 |
107 void PrecacheManager::RecordStatsForFetch(const GURL& url, | 107 void PrecacheManager::RecordStatsForFetch(const GURL& url, |
108 const base::Time& fetch_time, | 108 const base::Time& fetch_time, |
109 int64 size, | 109 int64 size, |
110 bool was_cached) { | 110 bool was_cached) { |
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 111 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
112 | 112 |
113 if (size == 0 || url.is_empty() || !url.SchemeIsHTTPOrHTTPS()) { | 113 if (size == 0 || url.is_empty() || !url.SchemeIsHTTPOrHTTPS()) { |
114 // Ignore empty responses, empty URLs, or URLs that aren't HTTP or HTTPS. | 114 // Ignore empty responses, empty URLs, or URLs that aren't HTTP or HTTPS. |
115 return; | 115 return; |
116 } | 116 } |
117 | 117 |
118 if (is_precaching_) { | 118 if (is_precaching_) { |
119 // Assume that precache is responsible for all requests made while | 119 // Assume that precache is responsible for all requests made while |
120 // precaching is currently in progress. | 120 // precaching is currently in progress. |
121 // TODO(sclittle): Make PrecacheFetcher explicitly mark precache-motivated | 121 // TODO(sclittle): Make PrecacheFetcher explicitly mark precache-motivated |
(...skipping 13 matching lines...) Expand all Loading... |
135 base::Bind(&PrecacheDatabase::RecordURLFetched, precache_database_, url, | 135 base::Bind(&PrecacheDatabase::RecordURLFetched, precache_database_, url, |
136 fetch_time, size, was_cached, is_connection_cellular)); | 136 fetch_time, size, was_cached, is_connection_cellular)); |
137 } | 137 } |
138 } | 138 } |
139 | 139 |
140 void PrecacheManager::Shutdown() { | 140 void PrecacheManager::Shutdown() { |
141 CancelPrecaching(); | 141 CancelPrecaching(); |
142 } | 142 } |
143 | 143 |
144 void PrecacheManager::OnDone() { | 144 void PrecacheManager::OnDone() { |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 145 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
146 | 146 |
147 // If OnDone has been called, then we should just be finishing precaching. | 147 // If OnDone has been called, then we should just be finishing precaching. |
148 DCHECK(is_precaching_); | 148 DCHECK(is_precaching_); |
149 is_precaching_ = false; | 149 is_precaching_ = false; |
150 | 150 |
151 precache_fetcher_.reset(); | 151 precache_fetcher_.reset(); |
152 | 152 |
153 precache_completion_callback_.Run(); | 153 precache_completion_callback_.Run(); |
154 // Uninitialize the callback so that any scoped_refptrs in it are released. | 154 // Uninitialize the callback so that any scoped_refptrs in it are released. |
155 precache_completion_callback_.Reset(); | 155 precache_completion_callback_.Reset(); |
156 } | 156 } |
157 | 157 |
158 void PrecacheManager::OnURLsReceived(const std::list<GURL>& urls) { | 158 void PrecacheManager::OnURLsReceived(const std::list<GURL>& urls) { |
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 159 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
160 | 160 |
161 if (!is_precaching_) { | 161 if (!is_precaching_) { |
162 // Don't start precaching if it was canceled while waiting for the list of | 162 // Don't start precaching if it was canceled while waiting for the list of |
163 // URLs. | 163 // URLs. |
164 return; | 164 return; |
165 } | 165 } |
166 | 166 |
167 // Start precaching. | 167 // Start precaching. |
168 precache_fetcher_.reset( | 168 precache_fetcher_.reset( |
169 new PrecacheFetcher(urls, browser_context_->GetRequestContext(), this)); | 169 new PrecacheFetcher(urls, browser_context_->GetRequestContext(), this)); |
170 precache_fetcher_->Start(); | 170 precache_fetcher_->Start(); |
171 } | 171 } |
172 | 172 |
173 } // namespace precache | 173 } // namespace precache |
OLD | NEW |