| OLD | NEW |
| 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" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 DCHECK(original_loop_); | 28 DCHECK(original_loop_); |
| 29 manager->AddRef(); | 29 manager->AddRef(); |
| 30 } | 30 } |
| 31 | 31 |
| 32 ~BlacklistManagerTask() { | 32 ~BlacklistManagerTask() { |
| 33 original_loop_->ReleaseSoon(FROM_HERE, manager_); | 33 original_loop_->ReleaseSoon(FROM_HERE, manager_); |
| 34 } | 34 } |
| 35 | 35 |
| 36 protected: | 36 protected: |
| 37 BlacklistManager* blacklist_manager() const { return manager_; } | 37 BlacklistManager* blacklist_manager() const { return manager_; } |
| 38 | 38 |
| 39 MessageLoop* original_loop_; | 39 MessageLoop* original_loop_; |
| 40 | 40 |
| 41 private: | 41 private: |
| 42 BlacklistManager* manager_; | 42 BlacklistManager* manager_; |
| 43 | 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(BlacklistManagerTask); | 44 DISALLOW_COPY_AND_ASSIGN(BlacklistManagerTask); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 BlacklistPathProvider::~BlacklistPathProvider() { | 47 BlacklistPathProvider::~BlacklistPathProvider() { |
| 48 } | 48 } |
| 49 | 49 |
| 50 class BlacklistManager::CompileBlacklistTask : public BlacklistManagerTask { | 50 class BlacklistManager::CompileBlacklistTask : public BlacklistManagerTask { |
| 51 public: | 51 public: |
| 52 CompileBlacklistTask(BlacklistManager* manager, | 52 CompileBlacklistTask(BlacklistManager* manager, |
| 53 const FilePath& destination_blacklist, | 53 const FilePath& destination_blacklist, |
| 54 const std::vector<FilePath>& source_blacklists) | 54 const std::vector<FilePath>& source_blacklists) |
| 55 : BlacklistManagerTask(manager), | 55 : BlacklistManagerTask(manager), |
| 56 destination_blacklist_(destination_blacklist), | 56 destination_blacklist_(destination_blacklist), |
| 57 source_blacklists_(source_blacklists) { | 57 source_blacklists_(source_blacklists) { |
| 58 } | 58 } |
| 59 | 59 |
| 60 virtual void Run() { | 60 virtual void Run() { |
| 61 bool success = true; | 61 bool success = true; |
| 62 | 62 |
| 63 Blacklist blacklist; | 63 Blacklist blacklist; |
| 64 std::string error_string; | 64 std::string error_string; |
| 65 | 65 |
| 66 for (std::vector<FilePath>::const_iterator i = source_blacklists_.begin(); | 66 for (std::vector<FilePath>::const_iterator i = source_blacklists_.begin(); |
| 67 i != source_blacklists_.end(); ++i) { | 67 i != source_blacklists_.end(); ++i) { |
| 68 if (!BlacklistIO::ReadText(&blacklist, *i, &error_string)) { | 68 if (!BlacklistIO::ReadText(&blacklist, *i, &error_string)) { |
| 69 success = false; | 69 success = false; |
| 70 break; | 70 break; |
| 71 } | 71 } |
| 72 } | 72 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 100 compiled_blacklist_(compiled_blacklist), | 100 compiled_blacklist_(compiled_blacklist), |
| 101 transient_blacklists_(transient_blacklists) { | 101 transient_blacklists_(transient_blacklists) { |
| 102 } | 102 } |
| 103 | 103 |
| 104 virtual void Run() { | 104 virtual void Run() { |
| 105 scoped_ptr<Blacklist> blacklist(new Blacklist); | 105 scoped_ptr<Blacklist> blacklist(new Blacklist); |
| 106 if (!BlacklistIO::ReadBinary(blacklist.get(), compiled_blacklist_)) { | 106 if (!BlacklistIO::ReadBinary(blacklist.get(), compiled_blacklist_)) { |
| 107 ReportReadResult(NULL); | 107 ReportReadResult(NULL); |
| 108 return; | 108 return; |
| 109 } | 109 } |
| 110 | 110 |
| 111 std::string error_string; | 111 std::string error_string; |
| 112 std::vector<FilePath>::const_iterator i; | 112 std::vector<FilePath>::const_iterator i; |
| 113 for (i = transient_blacklists_.begin(); | 113 for (i = transient_blacklists_.begin(); |
| 114 i != transient_blacklists_.end(); ++i) { | 114 i != transient_blacklists_.end(); ++i) { |
| 115 if (!BlacklistIO::ReadText(blacklist.get(), *i, &error_string)) { | 115 if (!BlacklistIO::ReadText(blacklist.get(), *i, &error_string)) { |
| 116 ReportReadResult(NULL); | 116 ReportReadResult(NULL); |
| 117 return; | 117 return; |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 | 120 |
| 121 ReportReadResult(blacklist.release()); | 121 ReportReadResult(blacklist.release()); |
| 122 } | 122 } |
| 123 | 123 |
| 124 private: | 124 private: |
| 125 void ReportReadResult(Blacklist* blacklist) { | 125 void ReportReadResult(Blacklist* blacklist) { |
| 126 original_loop_->PostTask( | 126 original_loop_->PostTask( |
| 127 FROM_HERE, NewRunnableMethod(blacklist_manager(), | 127 FROM_HERE, NewRunnableMethod(blacklist_manager(), |
| 128 &BlacklistManager::OnBlacklistReadFinished, | 128 &BlacklistManager::OnBlacklistReadFinished, |
| 129 blacklist)); | 129 blacklist)); |
| 130 } | 130 } |
| 131 | 131 |
| 132 FilePath compiled_blacklist_; | 132 FilePath compiled_blacklist_; |
| 133 std::vector<FilePath> transient_blacklists_; | 133 std::vector<FilePath> transient_blacklists_; |
| 134 | 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(ReadBlacklistTask); | 135 DISALLOW_COPY_AND_ASSIGN(ReadBlacklistTask); |
| 136 }; | 136 }; |
| 137 | 137 |
| 138 BlacklistManager::BlacklistManager(Profile* profile, | 138 BlacklistManager::BlacklistManager(Profile* profile, |
| 139 BlacklistPathProvider* path_provider, | 139 BlacklistPathProvider* path_provider, |
| 140 base::Thread* backend_thread) | 140 base::Thread* backend_thread) |
| 141 : first_read_finished_(false), | 141 : first_read_finished_(false), |
| 142 profile_(profile), | 142 profile_(profile), |
| 143 compiled_blacklist_path_( | 143 compiled_blacklist_path_( |
| 144 profile->GetPath().Append(chrome::kPrivacyBlacklistFileName)), | 144 profile->GetPath().Append(chrome::kPrivacyBlacklistFileName)), |
| 145 path_provider_(path_provider), | 145 path_provider_(path_provider), |
| 146 backend_thread_(backend_thread) { | 146 backend_thread_(backend_thread) { |
| 147 registrar_.Add(this, | 147 registrar_.Add(this, |
| 148 NotificationType::BLACKLIST_PATH_PROVIDER_UPDATED, | 148 NotificationType::EXTENSION_LOADED, |
| 149 Source<Profile>(profile)); |
| 150 registrar_.Add(this, |
| 151 NotificationType::EXTENSION_UNLOADED, |
| 149 Source<Profile>(profile)); | 152 Source<Profile>(profile)); |
| 150 ReadBlacklist(); | 153 ReadBlacklist(); |
| 151 } | 154 } |
| 152 | 155 |
| 153 void BlacklistManager::Observe(NotificationType type, | 156 void BlacklistManager::Observe(NotificationType type, |
| 154 const NotificationSource& source, | 157 const NotificationSource& source, |
| 155 const NotificationDetails& details) { | 158 const NotificationDetails& details) { |
| 156 DCHECK(type == NotificationType::BLACKLIST_PATH_PROVIDER_UPDATED); | 159 switch (type.value) { |
| 157 CompileBlacklist(); | 160 case NotificationType::EXTENSION_LOADED: |
| 161 case NotificationType::EXTENSION_UNLOADED: |
| 162 CompileBlacklist(); |
| 163 break; |
| 164 default: |
| 165 NOTREACHED(); |
| 166 break; |
| 167 } |
| 158 } | 168 } |
| 159 | 169 |
| 160 void BlacklistManager::CompileBlacklist() { | 170 void BlacklistManager::CompileBlacklist() { |
| 161 DCHECK(CalledOnValidThread()); | 171 DCHECK(CalledOnValidThread()); |
| 162 | 172 |
| 163 RunTaskOnBackendThread(new CompileBlacklistTask( | 173 RunTaskOnBackendThread(new CompileBlacklistTask( |
| 164 this, compiled_blacklist_path_, | 174 this, compiled_blacklist_path_, |
| 165 path_provider_->GetPersistentBlacklistPaths())); | 175 path_provider_->GetPersistentBlacklistPaths())); |
| 166 } | 176 } |
| 167 | 177 |
| 168 void BlacklistManager::ReadBlacklist() { | 178 void BlacklistManager::ReadBlacklist() { |
| 169 DCHECK(CalledOnValidThread()); | 179 DCHECK(CalledOnValidThread()); |
| 170 | 180 |
| 171 RunTaskOnBackendThread(new ReadBlacklistTask( | 181 RunTaskOnBackendThread(new ReadBlacklistTask( |
| 172 this, compiled_blacklist_path_, | 182 this, compiled_blacklist_path_, |
| 173 path_provider_->GetTransientBlacklistPaths())); | 183 path_provider_->GetTransientBlacklistPaths())); |
| 174 } | 184 } |
| 175 | 185 |
| 176 void BlacklistManager::OnBlacklistCompilationFinished(bool success) { | 186 void BlacklistManager::OnBlacklistCompilationFinished(bool success) { |
| 177 DCHECK(CalledOnValidThread()); | 187 DCHECK(CalledOnValidThread()); |
| 178 | 188 |
| 179 if (success) { | 189 if (success) { |
| 180 ReadBlacklist(); | 190 ReadBlacklist(); |
| 181 } else { | 191 } else { |
| 182 string16 error_message(ASCIIToUTF16("Blacklist compilation failed.")); | 192 string16 error_message(ASCIIToUTF16("Blacklist compilation failed.")); |
| 183 NotificationService::current()->Notify( | 193 NotificationService::current()->Notify( |
| 184 NotificationType::BLACKLIST_MANAGER_ERROR, | 194 NotificationType::BLACKLIST_MANAGER_ERROR, |
| 185 Source<Profile>(profile_), | 195 Source<Profile>(profile_), |
| 186 Details<string16>(&error_message)); | 196 Details<string16>(&error_message)); |
| 187 } | 197 } |
| 188 } | 198 } |
| 189 | 199 |
| 190 void BlacklistManager::OnBlacklistReadFinished(Blacklist* blacklist) { | 200 void BlacklistManager::OnBlacklistReadFinished(Blacklist* blacklist) { |
| 191 DCHECK(CalledOnValidThread()); | 201 DCHECK(CalledOnValidThread()); |
| 192 | 202 |
| 193 if (!blacklist) { | 203 if (!blacklist) { |
| 194 if (!first_read_finished_) { | 204 if (!first_read_finished_) { |
| 195 // If we're loading for the first time, the compiled blacklist could | 205 // If we're loading for the first time, the compiled blacklist could |
| 196 // just not exist. Try compiling it once. | 206 // just not exist. Try compiling it once. |
| 197 first_read_finished_ = true; | 207 first_read_finished_ = true; |
| 198 CompileBlacklist(); | 208 CompileBlacklist(); |
| 199 } else { | 209 } else { |
| 200 string16 error_message(ASCIIToUTF16("Blacklist read failed.")); | 210 string16 error_message(ASCIIToUTF16("Blacklist read failed.")); |
| 201 NotificationService::current()->Notify( | 211 NotificationService::current()->Notify( |
| 202 NotificationType::BLACKLIST_MANAGER_ERROR, | 212 NotificationType::BLACKLIST_MANAGER_ERROR, |
| 203 Source<Profile>(profile_), | 213 Source<Profile>(profile_), |
| 204 Details<string16>(&error_message)); | 214 Details<string16>(&error_message)); |
| 205 } | 215 } |
| 206 return; | 216 return; |
| 207 } | 217 } |
| 208 first_read_finished_ = true; | 218 first_read_finished_ = true; |
| 209 compiled_blacklist_.reset(blacklist); | 219 compiled_blacklist_.reset(blacklist); |
| 210 | 220 |
| 211 NotificationService::current()->Notify( | 221 NotificationService::current()->Notify( |
| 212 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, | 222 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, |
| 213 Source<Profile>(profile_), | 223 Source<Profile>(profile_), |
| 214 Details<Blacklist>(blacklist)); | 224 Details<Blacklist>(blacklist)); |
| 215 } | 225 } |
| 216 | 226 |
| 217 void BlacklistManager::RunTaskOnBackendThread(Task* task) { | 227 void BlacklistManager::RunTaskOnBackendThread(Task* task) { |
| 218 if (backend_thread_) { | 228 if (backend_thread_) { |
| 219 backend_thread_->message_loop()->PostTask(FROM_HERE, task); | 229 backend_thread_->message_loop()->PostTask(FROM_HERE, task); |
| 220 } else { | 230 } else { |
| 221 task->Run(); | 231 task->Run(); |
| 222 delete task; | 232 delete task; |
| 223 } | 233 } |
| 224 } | 234 } |
| OLD | NEW |