Commit 0688b152 authored by Scott Violet's avatar Scott Violet Committed by Chromium LUCI CQ

android: makes UsageStatsBridge properly remove observer

It looks like UsageStatsBridge is never destroyed. This ensures
the observer it adds to HistoryService is properly removed.

BUG=1158388
TEST=none

Change-Id: I8640dde7b2eaf2d5701827cf800e5c52edb605d5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2613587Reviewed-by: default avatarPatrick Noland <pnoland@chromium.org>
Commit-Queue: Scott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840831}
parent c7a7c55c
......@@ -62,16 +62,10 @@ UsageStatsBridge::UsageStatsBridge(
HistoryServiceFactory::GetForProfile(profile_,
ServiceAccessType::IMPLICIT_ACCESS);
if (history_service)
history_service->AddObserver(this);
scoped_history_service_observer_.Observe(history_service);
}
UsageStatsBridge::~UsageStatsBridge() {
history::HistoryService* history_service =
HistoryServiceFactory::GetForProfile(profile_,
ServiceAccessType::IMPLICIT_ACCESS);
if (history_service)
history_service->RemoveObserver(this);
}
UsageStatsBridge::~UsageStatsBridge() = default;
void UsageStatsBridge::Destroy(JNIEnv* env, const JavaRef<jobject>& j_this) {
delete this;
......@@ -346,4 +340,9 @@ void UsageStatsBridge::OnURLsDeleted(
}
}
void UsageStatsBridge::HistoryServiceBeingDeleted(
history::HistoryService* history_service) {
scoped_history_service_observer_.Reset();
}
} // namespace usage_stats
......@@ -11,7 +11,9 @@
#include "base/android/scoped_java_ref.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/android/usage_stats/usage_stats_database.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_service_observer.h"
namespace history {
......@@ -94,6 +96,8 @@ class UsageStatsBridge : public history::HistoryServiceObserver {
// Overridden from history::HistoryServiceObserver.
void OnURLsDeleted(history::HistoryService* history_service,
const history::DeletionInfo& deletion_info) override;
void HistoryServiceBeingDeleted(
history::HistoryService* history_service) override;
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
......@@ -119,6 +123,10 @@ class UsageStatsBridge : public history::HistoryServiceObserver {
base::android::ScopedJavaGlobalRef<jobject> j_this_;
base::ScopedObservation<history::HistoryService,
history::HistoryServiceObserver>
scoped_history_service_observer_{this};
base::WeakPtrFactory<UsageStatsBridge> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(UsageStatsBridge);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment