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

Side by Side Diff: chrome/browser/privacy_blacklist/blacklist_manager.cc

Issue 501082: Implement delaying resource requests until privacy blacklists are ready. (Closed)
Patch Set: don't get stuck on errors Created 10 years, 11 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
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 "chrome/browser/privacy_blacklist/blacklist_manager.h" 5 #include "chrome/browser/privacy_blacklist/blacklist_manager.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/task.h" 9 #include "base/task.h"
10 #include "base/thread.h" 10 #include "base/thread.h"
11 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/privacy_blacklist/blacklist.h" 12 #include "chrome/browser/privacy_blacklist/blacklist.h"
12 #include "chrome/browser/privacy_blacklist/blacklist_io.h" 13 #include "chrome/browser/privacy_blacklist/blacklist_io.h"
13 #include "chrome/browser/profile.h" 14 #include "chrome/browser/profile.h"
14 #include "chrome/common/chrome_constants.h" 15 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/extensions/extension.h" 16 #include "chrome/common/extensions/extension.h"
16 #include "chrome/common/notification_service.h" 17 #include "chrome/common/notification_service.h"
17 #include "chrome/common/notification_source.h" 18 #include "chrome/common/notification_source.h"
18 #include "chrome/common/notification_type.h" 19 #include "chrome/common/notification_type.h"
19 20
20 BlacklistPathProvider::~BlacklistPathProvider() { 21 BlacklistPathProvider::~BlacklistPathProvider() {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 } 83 }
83 } 84 }
84 85
85 BlacklistManager::~BlacklistManager() { 86 BlacklistManager::~BlacklistManager() {
86 } 87 }
87 88
88 void BlacklistManager::CompileBlacklist() { 89 void BlacklistManager::CompileBlacklist() {
89 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 90 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
90 DCHECK(path_provider_->AreBlacklistPathsReady()); 91 DCHECK(path_provider_->AreBlacklistPathsReady());
91 92
93 // The compiled blacklist is going to change. Make sure our clients don't use
94 // the old one and wait for the update instead by indicating that the compiled
95 // blacklist is not ready.
96 ChromeThread::PostTask(ChromeThread::IO, FROM_HERE,
97 NewRunnableMethod(this,
98 &BlacklistManager::ResetPublishedCompiledBlacklist));
99
100 // Schedule actual compilation on the background thread.
92 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, 101 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
93 NewRunnableMethod(this, &BlacklistManager::DoCompileBlacklist, 102 NewRunnableMethod(this, &BlacklistManager::DoCompileBlacklist,
94 path_provider_->GetPersistentBlacklistPaths())); 103 path_provider_->GetPersistentBlacklistPaths()));
95 } 104 }
96 105
97 void BlacklistManager::DoCompileBlacklist( 106 void BlacklistManager::DoCompileBlacklist(
98 const std::vector<FilePath>& source_blacklists) { 107 const std::vector<FilePath>& source_blacklists) {
99 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); 108 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
100 109
101 bool success = true;
102 Blacklist blacklist; 110 Blacklist blacklist;
103 std::string error_string; 111 std::string error_string;
112 std::vector<string16> errors;
104 for (std::vector<FilePath>::const_iterator i = source_blacklists.begin(); 113 for (std::vector<FilePath>::const_iterator i = source_blacklists.begin();
105 i != source_blacklists.end(); ++i) { 114 i != source_blacklists.end(); ++i) {
115 std::string error_string;
106 if (!BlacklistIO::ReadText(&blacklist, *i, &error_string)) { 116 if (!BlacklistIO::ReadText(&blacklist, *i, &error_string)) {
107 success = false; 117 string16 path = WideToUTF16(i->ToWStringHack());
108 break; 118 errors.push_back(path + ASCIIToUTF16(": " + error_string));
109 } 119 }
110 } 120 }
111 121
112 // Only overwrite the current compiled blacklist if we read all source 122 if (!errors.empty()) {
113 // files successfully. 123 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE,
114 if (success) 124 NewRunnableMethod(this,
115 success = BlacklistIO::WriteBinary(&blacklist, compiled_blacklist_path_); 125 &BlacklistManager::OnBlacklistCompilationReadErrors,
126 errors));
127 }
116 128
129 // Write the new compiled blacklist based on the data we could read
130 // successfully.
131 bool success = BlacklistIO::WriteBinary(&blacklist, compiled_blacklist_path_);
117 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, 132 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE,
118 NewRunnableMethod(this, &BlacklistManager::OnBlacklistCompilationFinished, 133 NewRunnableMethod(this, &BlacklistManager::OnBlacklistCompilationFinished,
119 success)); 134 success));
120 } 135 }
121 136
122 void BlacklistManager::OnBlacklistCompilationFinished(bool success) { 137 void BlacklistManager::OnBlacklistCompilationFinished(bool success) {
123 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 138 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
124 139
125 if (success) { 140 if (success) {
126 ReadBlacklist(); 141 ReadBlacklist();
127 } else { 142 } else {
128 string16 error_message(ASCIIToUTF16("Blacklist compilation failed.")); 143 string16 error_message(ASCIIToUTF16("Blacklist compilation failed."));
129 NotificationService::current()->Notify( 144 NotificationService::current()->Notify(
130 NotificationType::BLACKLIST_MANAGER_ERROR, 145 NotificationType::BLACKLIST_MANAGER_ERROR,
131 Source<Profile>(profile_), 146 Source<Profile>(profile_),
132 Details<string16>(&error_message)); 147 Details<string16>(&error_message));
133 } 148 }
134 } 149 }
135 150
151 void BlacklistManager::OnBlacklistCompilationReadErrors(
152 const std::vector<string16>& errors) {
153 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
154
155 string16 error_message(ASCIIToUTF16("Blacklist compilation failed.\n"));
156 for (std::vector<string16>::const_iterator i = errors.begin();
157 i != errors.end(); ++i) {
158 error_message += *i + ASCIIToUTF16("\n");
159 }
160 NotificationService::current()->Notify(
161 NotificationType::BLACKLIST_MANAGER_ERROR,
162 Source<Profile>(profile_),
163 Details<string16>(&error_message));
164 }
165
136 void BlacklistManager::ReadBlacklist() { 166 void BlacklistManager::ReadBlacklist() {
137 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 167 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
138 DCHECK(path_provider_->AreBlacklistPathsReady()); 168 DCHECK(path_provider_->AreBlacklistPathsReady());
139 169
140 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, 170 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
141 NewRunnableMethod(this, &BlacklistManager::DoReadBlacklist, 171 NewRunnableMethod(this, &BlacklistManager::DoReadBlacklist,
142 path_provider_->GetTransientBlacklistPaths())); 172 path_provider_->GetTransientBlacklistPaths()));
143 } 173 }
144 174
145 void BlacklistManager::DoReadBlacklist( 175 void BlacklistManager::DoReadBlacklist(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 Source<Profile>(profile_), 220 Source<Profile>(profile_),
191 Details<string16>(&error_message)); 221 Details<string16>(&error_message));
192 if (!first_read_finished_) { 222 if (!first_read_finished_) {
193 // If we're loading for the first time, the compiled blacklist could 223 // If we're loading for the first time, the compiled blacklist could
194 // just not exist. Try compiling it once. 224 // just not exist. Try compiling it once.
195 CompileBlacklist(); 225 CompileBlacklist();
196 } 226 }
197 } 227 }
198 first_read_finished_ = true; 228 first_read_finished_ = true;
199 } 229 }
230
231 void BlacklistManager::ResetPublishedCompiledBlacklist() {
232 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
233 compiled_blacklist_.reset();
234 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698