Bug 1613497 - Use the background thread pool for the predictor r=dragana

Differential Revision: https://phabricator.services.mozilla.com/D61744

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Valentin Gosu 2020-02-06 09:18:03 +00:00
Родитель 614e851352
Коммит 605f7f8a09
1 изменённых файлов: 12 добавлений и 29 удалений

Просмотреть файл

@ -63,13 +63,6 @@ static LazyLogModule gPredictorLog("NetworkPredictor");
#define PREDICTOR_LOG(args) \ #define PREDICTOR_LOG(args) \
MOZ_LOG(gPredictorLog, mozilla::LogLevel::Debug, args) MOZ_LOG(gPredictorLog, mozilla::LogLevel::Debug, args)
#define RETURN_IF_FAILED(_rv) \
do { \
if (NS_FAILED(_rv)) { \
return; \
} \
} while (0)
#define NOW_IN_SECONDS() static_cast<uint32_t>(PR_Now() / PR_USEC_PER_SEC) #define NOW_IN_SECONDS() static_cast<uint32_t>(PR_Now() / PR_USEC_PER_SEC)
static const char PREDICTOR_CLEANED_UP_PREF[] = "network.predictor.cleaned-up"; static const char PREDICTOR_CLEANED_UP_PREF[] = "network.predictor.cleaned-up";
@ -427,10 +420,8 @@ nsresult Predictor::Init() {
namespace { namespace {
class PredictorThreadShutdownRunner : public Runnable { class PredictorThreadShutdownRunner : public Runnable {
public: public:
PredictorThreadShutdownRunner(nsIThread* ioThread, bool success) PredictorThreadShutdownRunner(bool success)
: Runnable("net::PredictorThreadShutdownRunner"), : Runnable("net::PredictorThreadShutdownRunner"), mSuccess(success) {}
mIOThread(ioThread),
mSuccess(success) {}
~PredictorThreadShutdownRunner() = default; ~PredictorThreadShutdownRunner() = default;
NS_IMETHOD Run() override { NS_IMETHOD Run() override {
@ -440,20 +431,17 @@ class PredictorThreadShutdownRunner : public Runnable {
// future. // future.
Preferences::SetBool(PREDICTOR_CLEANED_UP_PREF, true); Preferences::SetBool(PREDICTOR_CLEANED_UP_PREF, true);
} }
return mIOThread->AsyncShutdown(); return NS_OK;
} }
private: private:
nsCOMPtr<nsIThread> mIOThread;
bool mSuccess; bool mSuccess;
}; };
class PredictorOldCleanupRunner : public Runnable { class PredictorOldCleanupRunner : public Runnable {
public: public:
PredictorOldCleanupRunner(nsIThread* ioThread, nsIFile* dbFile) PredictorOldCleanupRunner(nsIFile* dbFile)
: Runnable("net::PredictorOldCleanupRunner"), : Runnable("net::PredictorOldCleanupRunner"), mDBFile(dbFile) {}
mIOThread(ioThread),
mDBFile(dbFile) {}
~PredictorOldCleanupRunner() = default; ~PredictorOldCleanupRunner() = default;
@ -461,7 +449,7 @@ class PredictorOldCleanupRunner : public Runnable {
MOZ_ASSERT(!NS_IsMainThread(), "Cleaning up old files on main thread!"); MOZ_ASSERT(!NS_IsMainThread(), "Cleaning up old files on main thread!");
nsresult rv = CheckForAndDeleteOldDBFiles(); nsresult rv = CheckForAndDeleteOldDBFiles();
RefPtr<PredictorThreadShutdownRunner> runner = RefPtr<PredictorThreadShutdownRunner> runner =
new PredictorThreadShutdownRunner(mIOThread, NS_SUCCEEDED(rv)); new PredictorThreadShutdownRunner(NS_SUCCEEDED(rv));
NS_DispatchToMainThread(runner); NS_DispatchToMainThread(runner);
return NS_OK; return NS_OK;
} }
@ -495,7 +483,6 @@ class PredictorOldCleanupRunner : public Runnable {
return rv; return rv;
} }
nsCOMPtr<nsIThread> mIOThread;
nsCOMPtr<nsIFile> mDBFile; nsCOMPtr<nsIFile> mDBFile;
}; };
@ -556,17 +543,13 @@ void Predictor::MaybeCleanupOldDBFiles() {
nsCOMPtr<nsIFile> dbFile; nsCOMPtr<nsIFile> dbFile;
nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
getter_AddRefs(dbFile)); getter_AddRefs(dbFile));
RETURN_IF_FAILED(rv); NS_ENSURE_SUCCESS_VOID(rv);
rv = dbFile->AppendNative(NS_LITERAL_CSTRING("netpredictions.sqlite")); rv = dbFile->AppendNative(NS_LITERAL_CSTRING("netpredictions.sqlite"));
RETURN_IF_FAILED(rv); NS_ENSURE_SUCCESS_VOID(rv);
nsCOMPtr<nsIThread> ioThread;
rv = NS_NewNamedThread("NetPredictClean", getter_AddRefs(ioThread));
RETURN_IF_FAILED(rv);
RefPtr<PredictorOldCleanupRunner> runner = RefPtr<PredictorOldCleanupRunner> runner =
new PredictorOldCleanupRunner(ioThread, dbFile); new PredictorOldCleanupRunner(dbFile);
ioThread->Dispatch(runner, NS_DISPATCH_NORMAL); NS_DispatchBackgroundTask(runner.forget());
} }
void Predictor::Shutdown() { void Predictor::Shutdown() {
@ -1699,11 +1682,11 @@ void Predictor::LearnForSubresource(nsICacheEntry* entry, nsIURI* targetURI) {
uint32_t lastLoad; uint32_t lastLoad;
nsresult rv = entry->GetLastFetched(&lastLoad); nsresult rv = entry->GetLastFetched(&lastLoad);
RETURN_IF_FAILED(rv); NS_ENSURE_SUCCESS_VOID(rv);
int32_t loadCount; int32_t loadCount;
rv = entry->GetFetchCount(&loadCount); rv = entry->GetFetchCount(&loadCount);
RETURN_IF_FAILED(rv); NS_ENSURE_SUCCESS_VOID(rv);
nsCString key; nsCString key;
key.AssignLiteral(META_DATA_PREFIX); key.AssignLiteral(META_DATA_PREFIX);