| // Copyright 2017 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include <jni.h> | |
| #include <stddef.h> | |
| #include <memory> | |
| #include <set> | |
| #include <string> | |
| #include <vector> | |
| #include "base/android/callback_android.h" | |
| #include "base/android/jni_android.h" | |
| #include "base/android/jni_string.h" | |
| #include "base/android/jni_weak_ref.h" | |
| #include "base/feature_list.h" | |
| #include "base/functional/bind.h" | |
| #include "base/memory/scoped_refptr.h" | |
| #include "base/supports_user_data.h" | |
| #include "base/trace_event/trace_event.h" | |
| #include "base/values.h" | |
| #include "chrome/browser/android/browsing_data/browsing_data_model_android_holder.h" | |
| #include "chrome/browser/browsing_data/browsing_data_important_sites_util.h" | |
| #include "chrome/browser/browsing_data/chrome_browsing_data_model_delegate.h" | |
| #include "chrome/browser/browsing_data/chrome_browsing_data_remover_constants.h" | |
| #include "chrome/browser/engagement/important_sites_util.h" | |
| #include "chrome/browser/history/web_history_service_factory.h" | |
| #include "chrome/browser/profiles/profile.h" | |
| #include "chrome/browser/sync/sync_service_factory.h" | |
| #include "chrome/common/channel_info.h" | |
| #include "components/browsing_data/content/android/browsing_data_model_android.h" | |
| #include "components/browsing_data/content/browsing_data_model.h" | |
| #include "components/browsing_data/core/browsing_data_utils.h" | |
| #include "components/browsing_data/core/history_notice_utils.h" | |
| #include "components/browsing_data/core/pref_names.h" | |
| #include "components/prefs/pref_service.h" | |
| #include "components/sync/service/sync_service.h" | |
| #include "content/public/browser/browser_context.h" | |
| #include "content/public/browser/browsing_data_filter_builder.h" | |
| #include "content/public/browser/browsing_data_remover.h" | |
| #include "content/public/browser/web_contents.h" | |
| #include "ui/base/l10n/l10n_util.h" | |
| // Must come after all headers that specialize FromJniType() / ToJniType(). | |
| #include "chrome/browser/browsing_data/android/jni_headers/BrowsingDataBridge_jni.h" | |
| using base::android::AttachCurrentThread; | |
| using base::android::JavaRef; | |
| using base::android::ScopedJavaGlobalRef; | |
| using base::android::ScopedJavaLocalRef; | |
| using content::BrowsingDataRemover; | |
| namespace { | |
| void OnBrowsingDataRemoverDone(const ScopedJavaGlobalRef<jobject>& callback, | |
| uint64_t failed_data_types) { | |
| if (!callback) { | |
| return; | |
| } | |
| JNIEnv* env = AttachCurrentThread(); | |
| Java_OnClearBrowsingDataListener_onBrowsingDataCleared(env, callback); | |
| } | |
| PrefService* GetPrefService(Profile* profile) { | |
| return profile->GetOriginalProfile()->GetPrefs(); | |
| } | |
| class PendingCallbacksHolder : public base::SupportsUserData::Data { | |
| public: | |
| PendingCallbacksHolder() = default; | |
| ~PendingCallbacksHolder() override = default; | |
| void AddCallback(const JavaRef<jobject>& callback) { | |
| callbacks_.emplace_back(callback); | |
| } | |
| const std::vector<ScopedJavaGlobalRef<jobject>>& callbacks() const { | |
| return callbacks_; | |
| } | |
| static const void* UserDataKey() { return &kUserDataKey; } | |
| private: | |
| static inline const int kUserDataKey = 0; | |
| std::vector<ScopedJavaGlobalRef<jobject>> callbacks_; | |
| }; | |
| void OnBrowsingDataModelBuiltForAll(Profile* profile, | |
| std::unique_ptr<BrowsingDataModel> model) { | |
| scoped_refptr<BrowsingDataModelShared> shared_model = | |
| base::MakeRefCounted<BrowsingDataModelShared>(std::move(model)); | |
| profile->SetUserData( | |
| BrowsingDataModelAndroidHolder::UserDataKey(), | |
| std::make_unique<BrowsingDataModelAndroidHolder>(shared_model.get())); | |
| JNIEnv* env = AttachCurrentThread(); | |
| auto* pending_callbacks_holder = static_cast<PendingCallbacksHolder*>( | |
| profile->GetUserData(PendingCallbacksHolder::UserDataKey())); | |
| CHECK(pending_callbacks_holder); | |
| for (const auto& callback : pending_callbacks_holder->callbacks()) { | |
| Java_BrowsingDataBridge_onBrowsingDataModelBuilt( | |
| env, callback, | |
| BrowsingDataModelAndroid::CreateJavaObject(env, shared_model)); | |
| } | |
| profile->RemoveUserData(PendingCallbacksHolder::UserDataKey()); | |
| } | |
| } // namespace | |
| static void JNI_BrowsingDataBridge_ClearBrowsingData( | |
| JNIEnv* env, | |
| Profile* profile, | |
| const JavaRef<jobject>& jcallback, | |
| const std::vector<int>& data_types_vector, | |
| int32_t time_period, | |
| const std::vector<std::string>& excluding_domains, | |
| const std::vector<std::string>& ignoring_domains) { | |
| TRACE_EVENT0("browsing_data", "BrowsingDataBridge_ClearBrowsingData"); | |
| BrowsingDataRemover* browsing_data_remover = | |
| profile->GetBrowsingDataRemover(); | |
| uint64_t remove_mask = 0; | |
| for (const int data_type : data_types_vector) { | |
| switch (static_cast<browsing_data::BrowsingDataType>(data_type)) { | |
| case browsing_data::BrowsingDataType::HISTORY: | |
| remove_mask |= chrome_browsing_data_remover::DATA_TYPE_HISTORY; | |
| break; | |
| case browsing_data::BrowsingDataType::CACHE: | |
| remove_mask |= BrowsingDataRemover::DATA_TYPE_CACHE; | |
| break; | |
| case browsing_data::BrowsingDataType::SITE_DATA: | |
| remove_mask |= chrome_browsing_data_remover::DATA_TYPE_SITE_DATA; | |
| break; | |
| case browsing_data::BrowsingDataType::PASSWORDS: | |
| remove_mask |= chrome_browsing_data_remover::DATA_TYPE_PASSWORDS; | |
| remove_mask |= | |
| chrome_browsing_data_remover::DATA_TYPE_ACCOUNT_PASSWORDS; | |
| break; | |
| case browsing_data::BrowsingDataType::FORM_DATA: | |
| remove_mask |= chrome_browsing_data_remover::DATA_TYPE_FORM_DATA; | |
| break; | |
| case browsing_data::BrowsingDataType::TABS: | |
| remove_mask |= chrome_browsing_data_remover::DATA_TYPE_TABS; | |
| break; | |
| case browsing_data::BrowsingDataType::SITE_SETTINGS: | |
| remove_mask |= chrome_browsing_data_remover::DATA_TYPE_CONTENT_SETTINGS; | |
| break; | |
| case browsing_data::BrowsingDataType::DOWNLOADS: | |
| case browsing_data::BrowsingDataType::HOSTED_APPS_DATA: | |
| case browsing_data::BrowsingDataType::SKILLS: | |
| // Only implemented on Desktop. | |
| NOTREACHED(); | |
| } | |
| } | |
| std::unique_ptr<content::BrowsingDataFilterBuilder> filter_builder( | |
| content::BrowsingDataFilterBuilder::Create( | |
| content::BrowsingDataFilterBuilder::Mode::kPreserve)); | |
| for (const std::string& domain : excluding_domains) { | |
| filter_builder->AddRegisterableDomain(domain); | |
| } | |
| if (!excluding_domains.empty() || !ignoring_domains.empty()) { | |
| site_engagement::ImportantSitesUtil::RecordExcludedAndIgnoredImportantSites( | |
| profile, excluding_domains, ignoring_domains); | |
| } | |
| base::OnceCallback<void(uint64_t)> callback = base::BindOnce( | |
| &OnBrowsingDataRemoverDone, ScopedJavaGlobalRef<jobject>(env, jcallback)); | |
| browsing_data::TimePeriod period = | |
| static_cast<browsing_data::TimePeriod>(time_period); | |
| browsing_data_important_sites_util::Remove( | |
| remove_mask, BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, period, | |
| std::move(filter_builder), browsing_data_remover, std::move(callback)); | |
| } | |
| static void EnableDialogAboutOtherFormsOfBrowsingHistory( | |
| const JavaRef<jobject>& listener, | |
| bool enabled) { | |
| JNIEnv* env = AttachCurrentThread(); | |
| if (!enabled) | |
| return; | |
| Java_OtherFormsOfBrowsingHistoryListener_enableDialogAboutOtherFormsOfBrowsingHistory( | |
| env, listener); | |
| } | |
| static void JNI_BrowsingDataBridge_RequestInfoAboutOtherFormsOfBrowsingHistory( | |
| JNIEnv* env, | |
| Profile* profile, | |
| const JavaRef<jobject>& listener) { | |
| TRACE_EVENT0( | |
| "browsing_data", | |
| "BrowsingDataBridge_RequestInfoAboutOtherFormsOfBrowsingHistory"); | |
| // The one-time notice in the dialog. | |
| browsing_data::ShouldPopupDialogAboutOtherFormsOfBrowsingHistory( | |
| SyncServiceFactory::GetForProfile(profile), | |
| WebHistoryServiceFactory::GetForProfile(profile), chrome::GetChannel(), | |
| base::BindOnce(&EnableDialogAboutOtherFormsOfBrowsingHistory, | |
| ScopedJavaGlobalRef<jobject>(env, listener))); | |
| } | |
| static void JNI_BrowsingDataBridge_FetchImportantSites( | |
| JNIEnv* env, | |
| Profile* profile, | |
| const JavaRef<jobject>& java_callback) { | |
| TRACE_EVENT0("browsing_data", "BrowsingDataBridge_FetchImportantSites"); | |
| std::vector<site_engagement::ImportantSitesUtil::ImportantDomainInfo> | |
| important_sites = | |
| site_engagement::ImportantSitesUtil::GetImportantRegisterableDomains( | |
| profile, site_engagement::ImportantSitesUtil::kMaxImportantSites); | |
| bool dialog_disabled = | |
| site_engagement::ImportantSitesUtil::IsDialogDisabled(profile); | |
| std::vector<std::string> important_domains; | |
| std::vector<int32_t> important_domain_reasons; | |
| std::vector<std::string> important_domain_examples; | |
| for (const site_engagement::ImportantSitesUtil::ImportantDomainInfo& info : | |
| important_sites) { | |
| important_domains.push_back(info.registerable_domain); | |
| important_domain_reasons.push_back(info.reason_bitfield); | |
| important_domain_examples.push_back(info.example_origin.spec()); | |
| } | |
| Java_ImportantSitesCallback_onImportantRegisterableDomainsReady( | |
| env, java_callback, important_domains, important_domain_examples, | |
| important_domain_reasons, dialog_disabled); | |
| } | |
| // This value should not change during a sessions, as it's used for UMA metrics. | |
| static int32_t JNI_BrowsingDataBridge_GetMaxImportantSites(JNIEnv* env) { | |
| return site_engagement::ImportantSitesUtil::kMaxImportantSites; | |
| } | |
| static void JNI_BrowsingDataBridge_MarkOriginAsImportantForTesting( | |
| JNIEnv* env, | |
| Profile* profile, | |
| const std::string& jorigin) { | |
| GURL origin(jorigin); | |
| CHECK(origin.is_valid()); | |
| site_engagement::ImportantSitesUtil::MarkOriginAsImportantForTesting(profile, | |
| origin); | |
| } | |
| static bool JNI_BrowsingDataBridge_GetBrowsingDataDeletionPreference( | |
| JNIEnv* env, | |
| Profile* profile, | |
| int32_t data_type) { | |
| CHECK_GE(data_type, 0, base::NotFatalUntil::M161); | |
| CHECK_LE(data_type, | |
| static_cast<int>(browsing_data::BrowsingDataType::MAX_VALUE), | |
| base::NotFatalUntil::M161); | |
| // If there is no corresponding preference for this |data_type|, pretend | |
| // that it's set to false. | |
| // TODO(msramek): Consider defining native-side preferences for all Java UI | |
| // data types for consistency. | |
| std::string pref; | |
| if (!browsing_data::GetDeletionPreferenceFromDataType( | |
| static_cast<browsing_data::BrowsingDataType>(data_type), &pref)) { | |
| return false; | |
| } | |
| return GetPrefService(profile)->GetBoolean(pref); | |
| } | |
| static void JNI_BrowsingDataBridge_SetBrowsingDataDeletionPreference( | |
| JNIEnv* env, | |
| Profile* profile, | |
| int32_t data_type, | |
| bool value) { | |
| CHECK_GE(data_type, 0, base::NotFatalUntil::M161); | |
| CHECK_LE(data_type, | |
| static_cast<int>(browsing_data::BrowsingDataType::MAX_VALUE), | |
| base::NotFatalUntil::M161); | |
| std::string pref; | |
| if (!browsing_data::GetDeletionPreferenceFromDataType( | |
| static_cast<browsing_data::BrowsingDataType>(data_type), &pref)) { | |
| return; | |
| } | |
| GetPrefService(profile)->SetBoolean(pref, value); | |
| } | |
| static int32_t JNI_BrowsingDataBridge_GetBrowsingDataDeletionTimePeriod( | |
| JNIEnv* env, | |
| Profile* profile) { | |
| return GetPrefService(profile)->GetInteger( | |
| browsing_data::GetTimePeriodPreferenceName()); | |
| } | |
| static void JNI_BrowsingDataBridge_SetBrowsingDataDeletionTimePeriod( | |
| JNIEnv* env, | |
| Profile* profile, | |
| int32_t time_period) { | |
| CHECK_GE(time_period, 0, base::NotFatalUntil::M161); | |
| CHECK_LE(time_period, | |
| static_cast<int>(browsing_data::TimePeriod::TIME_PERIOD_LAST), | |
| base::NotFatalUntil::M161); | |
| const char* pref_name = browsing_data::GetTimePeriodPreferenceName(); | |
| PrefService* prefs = GetPrefService(profile); | |
| int previous_value = prefs->GetInteger(pref_name); | |
| if (time_period != previous_value) { | |
| browsing_data::RecordTimePeriodChange( | |
| static_cast<browsing_data::TimePeriod>(time_period)); | |
| prefs->SetInteger(pref_name, time_period); | |
| } | |
| } | |
| static void JNI_BrowsingDataBridge_BuildBrowsingDataModelFromDisk( | |
| JNIEnv* env, | |
| Profile* profile, | |
| const JavaRef<jobject>& java_callback) { | |
| auto* holder = static_cast<BrowsingDataModelAndroidHolder*>( | |
| profile->GetUserData(BrowsingDataModelAndroidHolder::UserDataKey())); | |
| if (holder) { | |
| if (BrowsingDataModelShared* shared_model = holder->model()) { | |
| Java_BrowsingDataBridge_onBrowsingDataModelBuilt( | |
| env, java_callback, | |
| BrowsingDataModelAndroid::CreateJavaObject( | |
| env, base::WrapRefCounted(shared_model))); | |
| return; | |
| } | |
| // If the holder exists but the model is null that means the model has | |
| // been built and destroyed yet the holder was not removed from user data, | |
| // therefore we should remove the holder and rebuild the model from disk. | |
| profile->RemoveUserData(BrowsingDataModelAndroidHolder::UserDataKey()); | |
| } | |
| auto* pending_callbacks_holder = static_cast<PendingCallbacksHolder*>( | |
| profile->GetUserData(PendingCallbacksHolder::UserDataKey())); | |
| if (pending_callbacks_holder) { | |
| pending_callbacks_holder->AddCallback(java_callback); | |
| return; | |
| } | |
| auto new_holder = std::make_unique<PendingCallbacksHolder>(); | |
| new_holder->AddCallback(java_callback); | |
| profile->SetUserData(PendingCallbacksHolder::UserDataKey(), | |
| std::move(new_holder)); | |
| BrowsingDataModel::BuildFromDisk( | |
| profile, ChromeBrowsingDataModelDelegate::CreateForProfile(profile), | |
| base::BindOnce(&OnBrowsingDataModelBuiltForAll, profile)); | |
| } | |
| DEFINE_JNI(BrowsingDataBridge) |