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 <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/metrics/field_trial.h" | 15 #include "base/metrics/field_trial.h" |
16 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_ names.h" | |
19 #include "components/history/core/browser/history_service.h" | 18 #include "components/history/core/browser/history_service.h" |
20 #include "components/precache/core/precache_database.h" | 19 #include "components/precache/core/precache_database.h" |
21 #include "components/precache/core/precache_switches.h" | 20 #include "components/precache/core/precache_switches.h" |
21 #include "components/sync_driver/sync_service.h" | |
22 #include "components/user_prefs/user_prefs.h" | 22 #include "components/user_prefs/user_prefs.h" |
23 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
25 #include "net/base/network_change_notifier.h" | 25 #include "net/base/network_change_notifier.h" |
26 | 26 |
27 using content::BrowserThread; | 27 using content::BrowserThread; |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 const char kPrecacheFieldTrialName[] = "Precache"; | 31 const char kPrecacheFieldTrialName[] = "Precache"; |
32 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled"; | 32 const char kPrecacheFieldTrialEnabledGroup[] = "Enabled"; |
33 const int kNumTopHosts = 100; | 33 const int kNumTopHosts = 100; |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 namespace precache { | 37 namespace precache { |
38 | 38 |
39 int NumTopHosts() { | 39 int NumTopHosts() { |
40 return kNumTopHosts; | 40 return kNumTopHosts; |
41 } | 41 } |
42 | 42 |
43 PrecacheManager::PrecacheManager(content::BrowserContext* browser_context) | 43 PrecacheManager::PrecacheManager(content::BrowserContext* browser_context, |
44 sync_driver::SyncService* sync_service) | |
44 : browser_context_(browser_context), | 45 : browser_context_(browser_context), |
46 sync_service_(sync_service), | |
45 precache_database_(new PrecacheDatabase()), | 47 precache_database_(new PrecacheDatabase()), |
46 is_precaching_(false) { | 48 is_precaching_(false) { |
47 base::FilePath db_path(browser_context_->GetPath().Append( | 49 base::FilePath db_path(browser_context_->GetPath().Append( |
48 base::FilePath(FILE_PATH_LITERAL("PrecacheDatabase")))); | 50 base::FilePath(FILE_PATH_LITERAL("PrecacheDatabase")))); |
49 | 51 |
50 BrowserThread::PostTask( | 52 BrowserThread::PostTask( |
51 BrowserThread::DB, FROM_HERE, | 53 BrowserThread::DB, FROM_HERE, |
52 base::Bind(base::IgnoreResult(&PrecacheDatabase::Init), | 54 base::Bind(base::IgnoreResult(&PrecacheDatabase::Init), |
53 precache_database_, db_path)); | 55 precache_database_, db_path)); |
54 } | 56 } |
55 | 57 |
56 PrecacheManager::~PrecacheManager() {} | 58 PrecacheManager::~PrecacheManager() {} |
57 | 59 |
58 // static | 60 // static |
59 bool PrecacheManager::IsPrecachingEnabled() { | 61 bool PrecacheManager::IsPrecachingEnabled() { |
60 return base::FieldTrialList::FindFullName(kPrecacheFieldTrialName) == | 62 return base::FieldTrialList::FindFullName(kPrecacheFieldTrialName) == |
61 kPrecacheFieldTrialEnabledGroup || | 63 kPrecacheFieldTrialEnabledGroup || |
62 base::CommandLine::ForCurrentProcess()->HasSwitch( | 64 base::CommandLine::ForCurrentProcess()->HasSwitch( |
63 switches::kEnablePrecache); | 65 switches::kEnablePrecache); |
64 } | 66 } |
65 | 67 |
66 bool PrecacheManager::IsPrecachingAllowed() { | 68 bool PrecacheManager::IsPrecachingAllowed() { |
67 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 69 return sync_service_ && sync_service_->IsSyncEnabledAndLoggedIn() && |
maxbogue
2015/06/16 19:51:27
This logic lgtm. FYI, http://crrev.com/1175243009
twifkak
2015/06/16 20:56:42
Done; thanks for the heads up.
| |
68 return user_prefs::UserPrefs::Get(browser_context_)->GetBoolean( | 70 sync_service_->HasSyncSetupCompleted() && |
69 data_reduction_proxy::prefs::kDataReductionProxyEnabled); | 71 sync_service_->GetPreferredDataTypes().Has(syncer::SESSIONS) && |
72 !sync_service_->GetEncryptedDataTypes().Has(syncer::SESSIONS); | |
70 } | 73 } |
71 | 74 |
72 void PrecacheManager::StartPrecaching( | 75 void PrecacheManager::StartPrecaching( |
73 const PrecacheCompletionCallback& precache_completion_callback, | 76 const PrecacheCompletionCallback& precache_completion_callback, |
74 const history::HistoryService& history_service) { | 77 const history::HistoryService& history_service) { |
75 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 78 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
76 | 79 |
77 if (is_precaching_) { | 80 if (is_precaching_) { |
78 DLOG(WARNING) << "Cannot start precaching because precaching is already " | 81 DLOG(WARNING) << "Cannot start precaching because precaching is already " |
79 "in progress."; | 82 "in progress."; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 for (const auto& host_count : host_counts) | 185 for (const auto& host_count : host_counts) |
183 hosts.push_back(host_count.first); | 186 hosts.push_back(host_count.first); |
184 | 187 |
185 // Start precaching. | 188 // Start precaching. |
186 precache_fetcher_.reset( | 189 precache_fetcher_.reset( |
187 new PrecacheFetcher(hosts, browser_context_->GetRequestContext(), this)); | 190 new PrecacheFetcher(hosts, browser_context_->GetRequestContext(), this)); |
188 precache_fetcher_->Start(); | 191 precache_fetcher_->Start(); |
189 } | 192 } |
190 | 193 |
191 } // namespace precache | 194 } // namespace precache |
OLD | NEW |