1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/locale/locale_template_url_loader.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/jni_weak_ref.h"
#include "base/check_deref.h"
#include "base/debug/dump_without_crashing.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "components/search_engines/template_url_data.h"
#include "components/search_engines/template_url_prepopulate_data.h"
#include "components/search_engines/template_url_service.h"
#include "components/search_engines/util.h"
#include "third_party/search_engines_data/resources/definitions/prepopulated_engines.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/locale/jni_headers/LocaleTemplateUrlLoader_jni.h"
using base::android::JavaParamRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF8;
static jlong JNI_LocaleTemplateUrlLoader_Init(JNIEnv* env,
std::string& locale,
Profile* profile) {
return reinterpret_cast<intptr_t>(new LocaleTemplateUrlLoader(
locale, TemplateURLServiceFactory::GetForProfile(profile), profile));
}
LocaleTemplateUrlLoader::LocaleTemplateUrlLoader(const std::string& locale,
TemplateURLService* service,
Profile* profile)
: locale_(locale), template_url_service_(service) {
profile_observation_.Observe(profile);
}
void LocaleTemplateUrlLoader::Destroy(JNIEnv* env) {
delete this;
}
void LocaleTemplateUrlLoader::OnProfileWillBeDestroyed(Profile* profile) {
// There is a risk that java keeps a reference to this loader and attempts to
// use it even if we started destroying the profile on the native side. To
// protect against this we remove access to the `template_url_service_` and
// stub out subsequent the calls.
profile_observation_.Reset();
template_url_service_ = nullptr;
}
jboolean LocaleTemplateUrlLoader::LoadTemplateUrls(JNIEnv* env) {
DCHECK(locale_.length() == 2);
if (!template_url_service_) {
// TODO(b/318339172): Test profile state from Java, switch to CHECK here.
base::debug::DumpWithoutCrashing(); // Investigating b/317335096.
return false;
}
std::vector<std::unique_ptr<TemplateURLData>> prepopulated_list =
GetLocalPrepopulatedEngines();
if (prepopulated_list.empty()) {
return false;
}
for (const auto& data_url : prepopulated_list) {
// Attempt to see if the URL already exists in the list of template URLs.
//
// Special case Google because the keyword is mutated based on the results
// of the GoogleUrlTracker, so we need to rely on prepopulate ID instead
// of keyword only for Google.
//
// Otherwise, matching based on keyword is sufficient and preferred as
// some logically distinct search engines share the same prepopulate ID and
// only differ on keyword.
const TemplateURL* matching_url =
template_url_service_->GetTemplateURLForKeyword(data_url->keyword());
bool exists = matching_url != nullptr;
if (!exists &&
data_url->prepopulate_id == TemplateURLPrepopulateData::google.id) {
auto existing_urls = template_url_service_->GetTemplateURLs();
for (TemplateURL* existing_url : existing_urls) {
if (existing_url->prepopulate_id() ==
TemplateURLPrepopulateData::google.id) {
matching_url = existing_url;
exists = true;
break;
}
}
}
if (exists) {
continue;
}
data_url.get()->safe_for_autoreplace = true;
std::unique_ptr<TemplateURL> turl(
new TemplateURL(*data_url, TemplateURL::LOCAL));
TemplateURL* added_turl = template_url_service_->Add(std::move(turl));
if (added_turl) {
prepopulate_ids_.push_back(added_turl->prepopulate_id());
}
}
return true;
}
void LocaleTemplateUrlLoader::RemoveTemplateUrls(JNIEnv* env) {
if (!template_url_service_) {
// TODO(b/318339172): Test profile state from Java, switch to CHECK here.
base::debug::DumpWithoutCrashing(); // Investigating b/317335096.
return;
}
while (!prepopulate_ids_.empty()) {
TemplateURL* turl = FindURLByPrepopulateID(
template_url_service_->GetTemplateURLs(), prepopulate_ids_.back());
if (turl && template_url_service_->GetDefaultSearchProvider() != turl) {
template_url_service_->Remove(turl);
}
prepopulate_ids_.pop_back();
}
}
void LocaleTemplateUrlLoader::OverrideDefaultSearchProvider(JNIEnv* env) {
if (!template_url_service_) {
// TODO(b/318339172): Test profile state from Java, switch to CHECK here.
base::debug::DumpWithoutCrashing(); // Investigating b/317335096.
return;
}
// If the user has changed their default search provider, no-op.
const TemplateURL* current_dsp =
template_url_service_->GetDefaultSearchProvider();
if (!current_dsp ||
current_dsp->prepopulate_id() != TemplateURLPrepopulateData::google.id) {
return;
}
TemplateURL* turl =
FindURLByPrepopulateID(template_url_service_->GetTemplateURLs(),
GetDesignatedSearchEngineForChina());
if (turl) {
template_url_service_->SetUserSelectedDefaultSearchProvider(turl);
}
}
void LocaleTemplateUrlLoader::SetGoogleAsDefaultSearch(JNIEnv* env) {
if (!template_url_service_) {
// TODO(b/318339172): Test profile state from Java, switch to CHECK here.
base::debug::DumpWithoutCrashing(); // Investigating b/317335096.
return;
}
// If the user has changed their default search provider, no-op.
const TemplateURL* current_dsp =
template_url_service_->GetDefaultSearchProvider();
if (!current_dsp ||
current_dsp->prepopulate_id() != GetDesignatedSearchEngineForChina()) {
return;
}
TemplateURL* turl =
FindURLByPrepopulateID(template_url_service_->GetTemplateURLs(),
TemplateURLPrepopulateData::google.id);
if (turl) {
template_url_service_->SetUserSelectedDefaultSearchProvider(turl);
}
}
std::vector<std::unique_ptr<TemplateURLData>>
LocaleTemplateUrlLoader::GetLocalPrepopulatedEngines() {
if (!template_url_service_) {
// TODO(b/318339172): Test profile state from Java, switch to CHECK here.
base::debug::DumpWithoutCrashing(); // Investigating b/317335096.
return std::vector<std::unique_ptr<TemplateURLData>>();
}
return template_url_service_->GetTemplateURLsForCountry(locale_);
}
int LocaleTemplateUrlLoader::GetDesignatedSearchEngineForChina() {
return TemplateURLPrepopulateData::sogou.id;
}
LocaleTemplateUrlLoader::~LocaleTemplateUrlLoader() = default;
|