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-07 10:55:24 +00:00
Родитель 7c59083d88
Коммит 6c3dc38ad9
1 изменённых файлов: 12 добавлений и 29 удалений

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

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