Bug 1678619: Implement a mechanism to fire pages-rank-changed event. r=mak

Depends on D100458

Differential Revision: https://phabricator.services.mozilla.com/D100459
This commit is contained in:
Daisuke Akatsuka 2021-01-14 23:33:38 +00:00
parent 450eb54843
commit e97c2c8e87
9 changed files with 82 additions and 3 deletions

View File

@ -46,6 +46,7 @@ class PlacesEvent : public nsWrapperCache {
virtual const PlacesHistoryCleared* AsPlacesHistoryCleared() const {
return nullptr;
}
virtual const PlacesRanking* AsPlacesRanking() const { return nullptr; }
protected:
virtual ~PlacesEvent() = default;

39
dom/base/PlacesRanking.h Normal file
View File

@ -0,0 +1,39 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_PlacesRanking_h
#define mozilla_dom_PlacesRanking_h
#include "mozilla/dom/PlacesEvent.h"
namespace mozilla {
namespace dom {
class PlacesRanking final : public PlacesEvent {
public:
explicit PlacesRanking() : PlacesEvent(PlacesEventType::Pages_rank_changed) {}
static already_AddRefed<PlacesRanking> Constructor(
const GlobalObject& aGlobal) {
RefPtr<PlacesRanking> event = new PlacesRanking();
return event.forget();
}
JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override {
return PlacesRanking_Binding::Wrap(aCx, this, aGivenProto);
}
const PlacesRanking* AsPlacesRanking() const override { return this; }
private:
~PlacesRanking() = default;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_PlacesRanking_h

View File

@ -222,6 +222,7 @@ EXPORTS.mozilla.dom += [
"PlacesFavicon.h",
"PlacesHistoryCleared.h",
"PlacesObservers.h",
"PlacesRanking.h",
"PlacesVisit.h",
"PlacesVisitTitle.h",
"PlacesWeakCallbackWrapper.h",

View File

@ -27,6 +27,10 @@ enum PlacesEventType {
* data: PlacesHistoryCleared. Fired whenever history is cleared.
*/
"history-cleared",
/**
* data: PlacesRanking. Fired whenever pages ranking is changed.
*/
"pages-rank-changed",
};
[ChromeOnly, Exposed=Window]
@ -255,3 +259,8 @@ interface PlacesVisitTitle : PlacesEvent {
interface PlacesHistoryCleared : PlacesEvent {
constructor();
};
[ChromeOnly, Exposed=Window]
interface PlacesRanking : PlacesEvent {
constructor();
};

View File

@ -3054,6 +3054,7 @@ var updateFrecency = async function(db, urls) {
const observers = PlacesUtils.history.getObservers();
notify(observers, "onManyFrecenciesChanged");
PlacesObservers.notifyListeners([new PlacesRanking()]);
};
/**

View File

@ -876,6 +876,7 @@ var invalidateFrecencies = async function(db, idList) {
const observers = PlacesUtils.history.getObservers();
notify(observers, "onManyFrecenciesChanged");
PlacesObservers.notifyListeners([new PlacesRanking()]);
// Trigger frecency updates for all affected origins.
await db.execute(`DELETE FROM moz_updateoriginsupdate_temp`);
@ -923,7 +924,10 @@ var clear = async function(db) {
// Notify frecency change observers.
notify(observers, "onManyFrecenciesChanged");
PlacesObservers.notifyListeners([new PlacesHistoryCleared()]);
PlacesObservers.notifyListeners([
new PlacesHistoryCleared(),
new PlacesRanking(),
]);
// Trigger frecency updates for all affected origins.
await db.execute(`DELETE FROM moz_updateoriginsupdate_temp`);

View File

@ -7,6 +7,11 @@
#ifndef mozilla_places_NotifyManyFrecenciesChanged_h_
#define mozilla_places_NotifyManyFrecenciesChanged_h_
#include "mozilla/dom/PlacesObservers.h"
#include "mozilla/dom/PlacesRanking.h"
using namespace mozilla::dom;
namespace mozilla {
namespace places {
@ -15,11 +20,21 @@ class NotifyManyFrecenciesChanged final : public Runnable {
NotifyManyFrecenciesChanged()
: Runnable("places::NotifyManyFrecenciesChanged") {}
// MOZ_CAN_RUN_SCRIPT_BOUNDARY until Runnable::Run is marked
// MOZ_CAN_RUN_SCRIPT. See bug 1535398.
MOZ_CAN_RUN_SCRIPT_BOUNDARY
NS_IMETHOD Run() override {
MOZ_ASSERT(NS_IsMainThread(), "This should be called on the main thread");
nsNavHistory* navHistory = nsNavHistory::GetHistoryService();
NS_ENSURE_STATE(navHistory);
navHistory->NotifyManyFrecenciesChanged();
RefPtr<PlacesRanking> event = new PlacesRanking();
Sequence<OwningNonNull<PlacesEvent>> events;
bool success = !!events.AppendElement(event.forget(), fallible);
MOZ_RELEASE_ASSERT(success);
PlacesObservers::NotifyListeners(events);
return NS_OK;
}
};

View File

@ -19,6 +19,7 @@
#include "DateTimeFormat.h"
#include "History.h"
#include "Helpers.h"
#include "NotifyManyFrecenciesChanged.h"
#include "nsTArray.h"
#include "nsCollationCID.h"
@ -222,13 +223,20 @@ class FixAndDecayFrecencyRunnable final : public Runnable {
mDecayRate(aDecayRate),
mDecayReason(mozIStorageStatementCallback::REASON_FINISHED) {}
NS_IMETHOD
Run() override {
// MOZ_CAN_RUN_SCRIPT_BOUNDARY until Runnable::Run is marked
// MOZ_CAN_RUN_SCRIPT. See bug 1535398.
MOZ_CAN_RUN_SCRIPT_BOUNDARY
NS_IMETHOD Run() override {
if (NS_IsMainThread()) {
nsNavHistory* navHistory = nsNavHistory::GetHistoryService();
NS_ENSURE_STATE(navHistory);
navHistory->DecayFrecencyCompleted(mDecayReason);
if (mozIStorageStatementCallback::REASON_FINISHED == mDecayReason) {
::NotifyManyFrecenciesChanged().Run();
}
return NS_OK;
}

View File

@ -431,6 +431,7 @@ module.exports = {
PlacesEvent: false,
PlacesHistoryCleared: false,
PlacesObservers: false,
PlacesRanking: false,
PlacesVisit: false,
PlacesVisitTitle: false,
PlacesWeakCallbackWrapper: false,