| 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" |
| 11 #include "chrome/browser/privacy_blacklist/blacklist.h" | 11 #include "chrome/browser/privacy_blacklist/blacklist.h" |
| 12 #include "chrome/browser/privacy_blacklist/blacklist_io.h" | 12 #include "chrome/browser/privacy_blacklist/blacklist_io.h" |
| 13 #include "chrome/browser/profile.h" | 13 #include "chrome/browser/profile.h" |
| 14 #include "chrome/common/chrome_constants.h" | 14 #include "chrome/common/chrome_constants.h" |
| 15 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/notification_service.h" | 16 #include "chrome/common/notification_service.h" |
| 16 #include "chrome/common/notification_source.h" | 17 #include "chrome/common/notification_source.h" |
| 17 #include "chrome/common/notification_type.h" | 18 #include "chrome/common/notification_type.h" |
| 18 | 19 |
| 19 BlacklistPathProvider::~BlacklistPathProvider() { | 20 BlacklistPathProvider::~BlacklistPathProvider() { |
| 20 } | 21 } |
| 21 | 22 |
| 22 BlacklistManager::BlacklistManager() | 23 BlacklistManager::BlacklistManager(Profile* profile, |
| 24 BlacklistPathProvider* path_provider) |
| 23 : first_read_finished_(false), | 25 : first_read_finished_(false), |
| 24 profile_(NULL), | 26 profile_(profile), |
| 25 path_provider_(NULL) { | 27 compiled_blacklist_path_( |
| 28 profile->GetPath().Append(chrome::kPrivacyBlacklistFileName)), |
| 29 path_provider_(path_provider) { |
| 26 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 30 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 27 } | 31 DCHECK(path_provider_); |
| 28 | 32 |
| 29 void BlacklistManager::Initialize(Profile* profile, | 33 registrar_.Add(this, |
| 30 BlacklistPathProvider* path_provider) { | 34 NotificationType::EXTENSIONS_READY, |
| 31 profile_ = profile; | 35 Source<Profile>(profile)); |
| 32 compiled_blacklist_path_ = | |
| 33 profile->GetPath().Append(chrome::kPrivacyBlacklistFileName); | |
| 34 path_provider_ = path_provider; | |
| 35 | |
| 36 registrar_.Add(this, | 36 registrar_.Add(this, |
| 37 NotificationType::EXTENSION_LOADED, | 37 NotificationType::EXTENSION_LOADED, |
| 38 Source<Profile>(profile)); | 38 Source<Profile>(profile)); |
| 39 registrar_.Add(this, | 39 registrar_.Add(this, |
| 40 NotificationType::EXTENSION_UNLOADED, | 40 NotificationType::EXTENSION_UNLOADED, |
| 41 Source<Profile>(profile)); | 41 Source<Profile>(profile)); |
| 42 ReadBlacklist(); | 42 } |
| 43 |
| 44 void BlacklistManager::Initialize() { |
| 45 if (path_provider_->AreBlacklistPathsReady()) |
| 46 ReadBlacklist(); |
| 47 } |
| 48 |
| 49 const Blacklist* BlacklistManager::GetCompiledBlacklist() const { |
| 50 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 51 return compiled_blacklist_.get(); |
| 43 } | 52 } |
| 44 | 53 |
| 45 void BlacklistManager::Observe(NotificationType type, | 54 void BlacklistManager::Observe(NotificationType type, |
| 46 const NotificationSource& source, | 55 const NotificationSource& source, |
| 47 const NotificationDetails& details) { | 56 const NotificationDetails& details) { |
| 48 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 57 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 49 | 58 |
| 50 switch (type.value) { | 59 switch (type.value) { |
| 60 case NotificationType::EXTENSIONS_READY: |
| 61 ReadBlacklist(); |
| 62 break; |
| 51 case NotificationType::EXTENSION_LOADED: | 63 case NotificationType::EXTENSION_LOADED: |
| 52 case NotificationType::EXTENSION_UNLOADED: | 64 case NotificationType::EXTENSION_UNLOADED: |
| 65 // Don't do anything if the path provider isn't ready. We're going to get |
| 66 // the paths when it becomes ready. |
| 67 // This makes an assumption that it isn't possible to install an extension |
| 68 // before ExtensionsService becomes ready. |
| 69 if (!path_provider_->AreBlacklistPathsReady()) |
| 70 break; |
| 71 |
| 72 // We don't need to recompile the on-disk blacklist when the extension |
| 73 // doesn't have any blacklist. |
| 74 if (Details<Extension>(details).ptr()->privacy_blacklists().empty()) |
| 75 break; |
| 76 |
| 53 CompileBlacklist(); | 77 CompileBlacklist(); |
| 54 break; | 78 break; |
| 55 default: | 79 default: |
| 56 NOTREACHED(); | 80 NOTREACHED(); |
| 57 break; | 81 break; |
| 58 } | 82 } |
| 59 } | 83 } |
| 60 | 84 |
| 85 BlacklistManager::~BlacklistManager() { |
| 86 } |
| 87 |
| 61 void BlacklistManager::CompileBlacklist() { | 88 void BlacklistManager::CompileBlacklist() { |
| 62 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 89 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 90 DCHECK(path_provider_->AreBlacklistPathsReady()); |
| 63 | 91 |
| 64 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, | 92 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, |
| 65 NewRunnableMethod(this, &BlacklistManager::DoCompileBlacklist, | 93 NewRunnableMethod(this, &BlacklistManager::DoCompileBlacklist, |
| 66 path_provider_->GetPersistentBlacklistPaths())); | 94 path_provider_->GetPersistentBlacklistPaths())); |
| 67 } | 95 } |
| 68 | 96 |
| 69 void BlacklistManager::DoCompileBlacklist( | 97 void BlacklistManager::DoCompileBlacklist( |
| 70 const std::vector<FilePath>& source_blacklists) { | 98 const std::vector<FilePath>& source_blacklists) { |
| 71 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 99 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 72 | 100 |
| 73 bool success = true; | 101 bool success = true; |
| 74 | |
| 75 Blacklist blacklist; | 102 Blacklist blacklist; |
| 76 std::string error_string; | 103 std::string error_string; |
| 77 | |
| 78 for (std::vector<FilePath>::const_iterator i = source_blacklists.begin(); | 104 for (std::vector<FilePath>::const_iterator i = source_blacklists.begin(); |
| 79 i != source_blacklists.end(); ++i) { | 105 i != source_blacklists.end(); ++i) { |
| 80 if (!BlacklistIO::ReadText(&blacklist, *i, &error_string)) { | 106 if (!BlacklistIO::ReadText(&blacklist, *i, &error_string)) { |
| 81 success = false; | 107 success = false; |
| 82 break; | 108 break; |
| 83 } | 109 } |
| 84 } | 110 } |
| 85 | 111 |
| 86 // Only overwrite the current compiled blacklist if we read all source | 112 // Only overwrite the current compiled blacklist if we read all source |
| 87 // files successfully. | 113 // files successfully. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 102 string16 error_message(ASCIIToUTF16("Blacklist compilation failed.")); | 128 string16 error_message(ASCIIToUTF16("Blacklist compilation failed.")); |
| 103 NotificationService::current()->Notify( | 129 NotificationService::current()->Notify( |
| 104 NotificationType::BLACKLIST_MANAGER_ERROR, | 130 NotificationType::BLACKLIST_MANAGER_ERROR, |
| 105 Source<Profile>(profile_), | 131 Source<Profile>(profile_), |
| 106 Details<string16>(&error_message)); | 132 Details<string16>(&error_message)); |
| 107 } | 133 } |
| 108 } | 134 } |
| 109 | 135 |
| 110 void BlacklistManager::ReadBlacklist() { | 136 void BlacklistManager::ReadBlacklist() { |
| 111 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 137 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 138 DCHECK(path_provider_->AreBlacklistPathsReady()); |
| 112 | 139 |
| 113 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, | 140 ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE, |
| 114 NewRunnableMethod(this, &BlacklistManager::DoReadBlacklist, | 141 NewRunnableMethod(this, &BlacklistManager::DoReadBlacklist, |
| 115 path_provider_->GetTransientBlacklistPaths())); | 142 path_provider_->GetTransientBlacklistPaths())); |
| 116 } | 143 } |
| 117 | 144 |
| 118 void BlacklistManager::DoReadBlacklist( | 145 void BlacklistManager::DoReadBlacklist( |
| 119 const std::vector<FilePath>& transient_blacklists) { | 146 const std::vector<FilePath>& transient_blacklists) { |
| 120 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 147 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 121 | 148 |
| 122 scoped_ptr<Blacklist> blacklist(new Blacklist); | 149 scoped_ptr<Blacklist> blacklist(new Blacklist); |
| 123 if (!BlacklistIO::ReadBinary(blacklist.get(), compiled_blacklist_path_)) { | 150 if (BlacklistIO::ReadBinary(blacklist.get(), compiled_blacklist_path_)) { |
| 124 ReportBlacklistReadResult(NULL); | 151 std::string error_string; |
| 125 return; | 152 std::vector<FilePath>::const_iterator i; |
| 153 for (i = transient_blacklists.begin(); |
| 154 i != transient_blacklists.end(); ++i) { |
| 155 if (!BlacklistIO::ReadText(blacklist.get(), *i, &error_string)) { |
| 156 blacklist.reset(); |
| 157 break; |
| 158 } |
| 159 } |
| 160 } else { |
| 161 blacklist.reset(); |
| 126 } | 162 } |
| 127 | 163 ChromeThread::PostTask(ChromeThread::IO, FROM_HERE, |
| 128 std::string error_string; | 164 NewRunnableMethod(this, |
| 129 std::vector<FilePath>::const_iterator i; | 165 &BlacklistManager::UpdatePublishedCompiledBlacklist, |
| 130 for (i = transient_blacklists.begin(); | 166 blacklist.release())); |
| 131 i != transient_blacklists.end(); ++i) { | |
| 132 if (!BlacklistIO::ReadText(blacklist.get(), *i, &error_string)) { | |
| 133 ReportBlacklistReadResult(NULL); | |
| 134 return; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 ReportBlacklistReadResult(blacklist.release()); | |
| 139 } | 167 } |
| 140 | 168 |
| 141 void BlacklistManager::ReportBlacklistReadResult(Blacklist* blacklist) { | 169 void BlacklistManager::UpdatePublishedCompiledBlacklist(Blacklist* blacklist) { |
| 142 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 170 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 171 if (blacklist) |
| 172 compiled_blacklist_.reset(blacklist); |
| 143 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, | 173 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
| 144 NewRunnableMethod(this, &BlacklistManager::OnBlacklistReadFinished, | 174 NewRunnableMethod(this, &BlacklistManager::OnBlacklistReadFinished, |
| 145 blacklist)); | 175 blacklist != NULL)); |
| 146 } | 176 } |
| 147 | 177 |
| 148 void BlacklistManager::OnBlacklistReadFinished(Blacklist* blacklist) { | 178 void BlacklistManager::OnBlacklistReadFinished(bool success) { |
| 149 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 179 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 150 | 180 |
| 151 if (!blacklist) { | 181 if (success) { |
| 182 NotificationService::current()->Notify( |
| 183 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, |
| 184 Source<Profile>(profile_), |
| 185 NotificationService::NoDetails()); |
| 186 } else { |
| 187 string16 error_message(ASCIIToUTF16("Blacklist read failed.")); |
| 188 NotificationService::current()->Notify( |
| 189 NotificationType::BLACKLIST_MANAGER_ERROR, |
| 190 Source<Profile>(profile_), |
| 191 Details<string16>(&error_message)); |
| 152 if (!first_read_finished_) { | 192 if (!first_read_finished_) { |
| 153 // If we're loading for the first time, the compiled blacklist could | 193 // If we're loading for the first time, the compiled blacklist could |
| 154 // just not exist. Try compiling it once. | 194 // just not exist. Try compiling it once. |
| 155 first_read_finished_ = true; | |
| 156 CompileBlacklist(); | 195 CompileBlacklist(); |
| 157 } else { | |
| 158 string16 error_message(ASCIIToUTF16("Blacklist read failed.")); | |
| 159 NotificationService::current()->Notify( | |
| 160 NotificationType::BLACKLIST_MANAGER_ERROR, | |
| 161 Source<Profile>(profile_), | |
| 162 Details<string16>(&error_message)); | |
| 163 } | 196 } |
| 164 return; | |
| 165 } | 197 } |
| 166 first_read_finished_ = true; | 198 first_read_finished_ = true; |
| 167 compiled_blacklist_.reset(blacklist); | |
| 168 | |
| 169 NotificationService::current()->Notify( | |
| 170 NotificationType::BLACKLIST_MANAGER_BLACKLIST_READ_FINISHED, | |
| 171 Source<Profile>(profile_), | |
| 172 Details<Blacklist>(blacklist)); | |
| 173 } | 199 } |
| OLD | NEW |