OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/metrics/histogram.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" |
| 13 #include "chrome/browser/profile_resetter/automatic_profile_resetter.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 16 #include "chrome/browser/search_engines/template_url_service.h" |
| 17 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 base::DictionaryValue* BuildSubTreeFromTemplateURL( |
| 23 const TemplateURL* template_url) { |
| 24 base::DictionaryValue* tree = new base::DictionaryValue; |
| 25 tree->SetString("search_url", template_url->url()); |
| 26 tree->SetString("search_terms_replacement_key", |
| 27 template_url->search_terms_replacement_key()); |
| 28 tree->SetString("suggest_url", template_url->suggestions_url()); |
| 29 tree->SetString("instant_url", template_url->instant_url()); |
| 30 tree->SetString("image_url", template_url->image_url()); |
| 31 tree->SetString("new_tab_url", template_url->new_tab_url()); |
| 32 tree->SetString("search_url_post_params", |
| 33 template_url->search_url_post_params()); |
| 34 tree->SetString("suggest_url_post_params", |
| 35 template_url->suggestions_url_post_params()); |
| 36 tree->SetString("instant_url_post_params", |
| 37 template_url->instant_url_post_params()); |
| 38 tree->SetString("image_url_post_params", |
| 39 template_url->image_url_post_params()); |
| 40 tree->SetString("icon_url", template_url->favicon_url().spec()); |
| 41 tree->SetString("name", template_url->short_name()); |
| 42 tree->SetString("keyword", template_url->keyword()); |
| 43 // For consistency with Prefs. |
| 44 tree->SetString("encodings", |
| 45 JoinString(template_url->input_encodings(), ';')); |
| 46 tree->SetString("prepopulate_id", |
| 47 base::IntToString(template_url->prepopulate_id())); |
| 48 base::ListValue* alternate_urls = new base::ListValue; |
| 49 alternate_urls->AppendStrings(template_url->alternate_urls()); |
| 50 tree->Set("alternate_urls", alternate_urls); |
| 51 return tree; |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 56 // AutomaticProfileResetterDelegateImpl -------------------------------------- |
| 57 |
| 58 AutomaticProfileResetterDelegateImpl::AutomaticProfileResetterDelegateImpl( |
| 59 Profile* profile) |
| 60 : profile_(profile) { |
| 61 TemplateURLService* template_url_service = |
| 62 TemplateURLServiceFactory::GetForProfile(profile_); |
| 63 if (template_url_service) { |
| 64 template_url_service->AddObserver(this); |
| 65 // Needed so that |template_url_service_ready_event_| will be signaled even |
| 66 // when TemplateURLService is already initialized when we are constructed. |
| 67 OnTemplateURLServiceChanged(); |
| 68 } |
| 69 } |
| 70 |
| 71 AutomaticProfileResetterDelegateImpl::~AutomaticProfileResetterDelegateImpl() { |
| 72 TemplateURLService* template_url_service = |
| 73 TemplateURLServiceFactory::GetForProfile(profile_); |
| 74 if (template_url_service) { |
| 75 template_url_service->RemoveObserver(this); |
| 76 } |
| 77 } |
| 78 |
| 79 void AutomaticProfileResetterDelegateImpl::LoadTemplateURLServiceIfNeeded() { |
| 80 TemplateURLService* template_url_service = |
| 81 TemplateURLServiceFactory::GetForProfile(profile_); |
| 82 DCHECK(template_url_service); |
| 83 template_url_service->Load(); // Safe to call even if it has loaded already. |
| 84 } |
| 85 |
| 86 void AutomaticProfileResetterDelegateImpl::WaitOnTemplateURLService( |
| 87 const base::Closure& ready_callback) { |
| 88 DCHECK(!ready_callback.is_null()); |
| 89 template_url_service_ready_event_.Post(FROM_HERE, ready_callback); |
| 90 } |
| 91 |
| 92 base::DictionaryValue* |
| 93 AutomaticProfileResetterDelegateImpl::GetDefaultSearchProviderDetails() const { |
| 94 TemplateURLService* template_url_service = |
| 95 TemplateURLServiceFactory::GetForProfile(profile_); |
| 96 DCHECK(template_url_service); |
| 97 DCHECK(template_url_service->loaded()); |
| 98 |
| 99 TemplateURL* default_search_provider = |
| 100 template_url_service->GetDefaultSearchProvider(); |
| 101 |
| 102 // Having a NULL default search provider is due to either: |
| 103 // 1.) default search providers being disabled by policy, |
| 104 // 2.) directly tampering the Preferences and/or the SQLite DBs. |
| 105 // In this state, Omnibox non-keyword search functionality is disabled. |
| 106 // Unfortunately, we cannot really tell apart the two underlying causes. |
| 107 if (default_search_provider) |
| 108 return BuildSubTreeFromTemplateURL(default_search_provider); |
| 109 else |
| 110 return NULL; |
| 111 } |
| 112 |
| 113 bool AutomaticProfileResetterDelegateImpl::IsDefaultSearchProviderManaged() |
| 114 const { |
| 115 TemplateURLService* template_url_service = |
| 116 TemplateURLServiceFactory::GetForProfile(profile_); |
| 117 DCHECK(template_url_service); |
| 118 DCHECK(template_url_service->loaded()); |
| 119 return template_url_service->is_default_search_managed(); |
| 120 } |
| 121 |
| 122 base::ListValue* |
| 123 AutomaticProfileResetterDelegateImpl::GetPrepopulatedSearchProvidersDetails() |
| 124 const { |
| 125 ScopedVector<TemplateURL> engines; |
| 126 size_t default_search_index = 0; |
| 127 engines = TemplateURLPrepopulateData::GetPrepopulatedEngines( |
| 128 profile_, &default_search_index); |
| 129 base::ListValue* engines_details_list = new base::ListValue; |
| 130 for (ScopedVector<TemplateURL>::const_iterator it = engines.begin(); |
| 131 it != engines.end(); |
| 132 ++it) { |
| 133 engines_details_list->Append(BuildSubTreeFromTemplateURL(*it)); |
| 134 } |
| 135 return engines_details_list; |
| 136 } |
| 137 |
| 138 void AutomaticProfileResetterDelegateImpl::ShowPrompt() { |
| 139 // TODO(engedy): Call the UI from here once we have it. |
| 140 } |
| 141 |
| 142 void AutomaticProfileResetterDelegateImpl::ReportStatistics( |
| 143 uint32 satisfied_criteria_mask, |
| 144 uint32 combined_status_mask) { |
| 145 UMA_HISTOGRAM_ENUMERATION( |
| 146 "AutomaticProfileReset.SatisfiedCriteriaMask", |
| 147 satisfied_criteria_mask, |
| 148 AutomaticProfileResetter::kSatisfiedCriteriaMaskMaximumValue); |
| 149 UMA_HISTOGRAM_ENUMERATION( |
| 150 "AutomaticProfileReset.CombinedStatusMask", |
| 151 combined_status_mask, |
| 152 AutomaticProfileResetter::kCombinedStatusMaskMaximumValue); |
| 153 } |
| 154 |
| 155 void AutomaticProfileResetterDelegateImpl::OnTemplateURLServiceChanged() { |
| 156 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 157 |
| 158 TemplateURLService* template_url_service = |
| 159 TemplateURLServiceFactory::GetForProfile(profile_); |
| 160 DCHECK(template_url_service); |
| 161 if (template_url_service->loaded() && |
| 162 !template_url_service_ready_event_.is_signaled()) { |
| 163 template_url_service_ready_event_.Signal(); |
| 164 } |
| 165 } |
OLD | NEW |