OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/garbled_text_service.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "chrome/browser/garbled_text_url_tracker.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/pref_names.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_details.h" |
| 19 #include "content/public/browser/notification_source.h" |
| 20 |
| 21 const char GarbledTextService::kPrefGarbledTextBlacklist[] = |
| 22 "garbled_text_sites"; |
| 23 |
| 24 // When the blacklist grows over |kBlacklistSizeLimit|, we reduce it to |
| 25 // kBlacklistReduceRatio * kBlacklistSizeLimit in manner of least recent added. |
| 26 const size_t kBlacklistSizeLimit = 2000; |
| 27 const size_t kBlacklistReduceRatio = 0.5; |
| 28 |
| 29 namespace { |
| 30 |
| 31 void SetPrefBlacklist(PrefService* user_prefs, |
| 32 const GarbledTextService::Blacklist& blacklist) { |
| 33 ListPrefUpdate update(user_prefs, |
| 34 GarbledTextService::kPrefGarbledTextBlacklist); |
| 35 ListValue* pref_blacklist = update.Get(); |
| 36 if (!pref_blacklist) { |
| 37 NOTREACHED(); |
| 38 return; |
| 39 } |
| 40 pref_blacklist->Clear(); |
| 41 |
| 42 for (GarbledTextService::Blacklist::const_iterator itr = blacklist.begin(); |
| 43 itr != blacklist.end(); ++itr) |
| 44 pref_blacklist->Append(base::Value::CreateStringValue(*itr)); |
| 45 } |
| 46 |
| 47 } // anonymous namespace |
| 48 |
| 49 GarbledTextServiceParams::GarbledTextServiceParams(PrefService* user_prefs, |
| 50 GarbledTextService* service) |
| 51 : service_(service->AsWeakPtr()) { |
| 52 DCHECK(user_prefs); |
| 53 DCHECK(service); |
| 54 |
| 55 const ListValue* pref_blacklist = user_prefs->GetList( |
| 56 GarbledTextService::kPrefGarbledTextBlacklist); |
| 57 if (pref_blacklist) { |
| 58 size_t i = 0; |
| 59 if (pref_blacklist->GetSize() > kBlacklistSizeLimit) { |
| 60 size_t new_size = kBlacklistSizeLimit * kBlacklistReduceRatio; |
| 61 i = pref_blacklist->GetSize() - new_size; |
| 62 } |
| 63 |
| 64 for (; i < pref_blacklist->GetSize(); ++i) { |
| 65 std::string value; |
| 66 if (pref_blacklist->GetString(i, &value)) |
| 67 blacklist_.insert(value); |
| 68 } |
| 69 |
| 70 if (pref_blacklist->GetSize() > kBlacklistSizeLimit) |
| 71 SetPrefBlacklist(user_prefs, blacklist_); |
| 72 } |
| 73 } |
| 74 |
| 75 GarbledTextServiceParams::~GarbledTextServiceParams() { |
| 76 } |
| 77 |
| 78 scoped_ptr<GarbledTextURLTracker> GarbledTextServiceParams::CreateTracker( |
| 79 const BooleanPrefMember& enabled) { |
| 80 scoped_ptr<GarbledTextURLTracker> tracker( |
| 81 new GarbledTextURLTracker(enabled, blacklist_)); |
| 82 |
| 83 content::BrowserThread::PostTask( |
| 84 content::BrowserThread::UI, FROM_HERE, |
| 85 base::Bind(&GarbledTextService::SetTracker, |
| 86 service_, tracker->AsWeakPtr())); |
| 87 return tracker.Pass(); |
| 88 } |
| 89 |
| 90 // static |
| 91 GarbledTextService* GarbledTextServiceFactory::GetForProfile( |
| 92 Profile* profile) { |
| 93 return static_cast<GarbledTextService*>( |
| 94 GetInstance()->GetServiceForProfile(profile, true)); |
| 95 } |
| 96 |
| 97 // static |
| 98 GarbledTextServiceFactory* GarbledTextServiceFactory::GetInstance() { |
| 99 return Singleton<GarbledTextServiceFactory>::get(); |
| 100 } |
| 101 |
| 102 GarbledTextServiceFactory::GarbledTextServiceFactory() |
| 103 : ProfileKeyedServiceFactory(ProfileDependencyManager::GetInstance()) { |
| 104 } |
| 105 |
| 106 GarbledTextServiceFactory::~GarbledTextServiceFactory() {} |
| 107 |
| 108 ProfileKeyedService* GarbledTextServiceFactory::BuildServiceInstanceFor( |
| 109 Profile* profile) const { |
| 110 return new GarbledTextService(profile); |
| 111 } |
| 112 |
| 113 void GarbledTextServiceFactory::RegisterUserPrefs(PrefService* user_prefs) { |
| 114 if (!user_prefs->FindPreference(prefs::kEnableAutoGarbledTextFix)) { |
| 115 user_prefs->RegisterBooleanPref(prefs::kEnableAutoGarbledTextFix, |
| 116 true, |
| 117 PrefService::UNSYNCABLE_PREF); |
| 118 } |
| 119 |
| 120 if (!user_prefs->FindPreference( |
| 121 GarbledTextService::kPrefGarbledTextBlacklist)) { |
| 122 user_prefs->RegisterListPref( |
| 123 GarbledTextService::kPrefGarbledTextBlacklist, |
| 124 PrefService::UNSYNCABLE_PREF); |
| 125 } |
| 126 } |
| 127 |
| 128 bool GarbledTextServiceFactory::ServiceHasOwnInstanceInIncognito() { |
| 129 return true; |
| 130 } |
| 131 |
| 132 bool GarbledTextServiceFactory::ServiceIsNULLWhileTesting() { |
| 133 return false; |
| 134 } |
| 135 |
| 136 GarbledTextService::GarbledTextService(Profile* profile) |
| 137 : profile_(profile) { |
| 138 DCHECK(profile_); |
| 139 } |
| 140 |
| 141 GarbledTextService::~GarbledTextService() { |
| 142 DCHECK(CalledOnValidThread()); |
| 143 } |
| 144 |
| 145 void GarbledTextService::SetTracker( |
| 146 base::WeakPtr<GarbledTextURLTracker> tracker) { |
| 147 DCHECK(CalledOnValidThread()); |
| 148 DCHECK(!tracker_.get()); |
| 149 tracker_ = tracker; |
| 150 } |
| 151 |
| 152 void GarbledTextService::UpdateBlacklist(const GarbledURLs& garbled_urls, |
| 153 const base::Closure& callback) { |
| 154 DCHECK(CalledOnValidThread()); |
| 155 |
| 156 scoped_ptr<Blacklist> blacklist_delta(new Blacklist); |
| 157 base::Closure post( |
| 158 base::Bind(&GarbledTextURLTracker::UpdateBlacklist, |
| 159 tracker_, garbled_urls, blacklist_delta.get())); |
| 160 base::Closure reply( |
| 161 base::Bind(&GarbledTextService::UpdatePref, AsWeakPtr(), |
| 162 base::Owned(blacklist_delta.release()), callback)); |
| 163 content::BrowserThread::PostTaskAndReply( |
| 164 content::BrowserThread::IO, FROM_HERE, post, reply); |
| 165 } |
| 166 |
| 167 void GarbledTextService::UpdatePref(const Blacklist* blacklist_delta, |
| 168 const base::Closure& callback) { |
| 169 DCHECK(CalledOnValidThread()); |
| 170 |
| 171 PrefService* user_prefs = profile_->GetPrefs(); |
| 172 DCHECK(user_prefs); |
| 173 |
| 174 ListPrefUpdate update(user_prefs, kPrefGarbledTextBlacklist); |
| 175 ListValue* blacklist = update.Get(); |
| 176 if (!blacklist) { |
| 177 NOTREACHED(); |
| 178 return; |
| 179 } |
| 180 for (Blacklist::const_iterator itr = blacklist_delta->begin(); |
| 181 itr != blacklist_delta->end(); ++itr) |
| 182 blacklist->Append(base::Value::CreateStringValue(*itr)); |
| 183 |
| 184 callback.Run(); |
| 185 } |
OLD | NEW |