Commit 05691904 authored by Justin DeWitt's avatar Justin DeWitt Committed by Commit Bot

[EoS] Enable scheduling the daily catalog update task.

- The ExploreSitesService is configured to start on startup if the
  feature is enabled, and will schedule the daily task directly in the
  service factory.
- The task will cancel itself if it runs and the feature is disabled.

Bug: 867488
Change-Id: I38b55c97d9b2d26a3c94cdff5bcce03bf341d404
Reviewed-on: https://chromium-review.googlesource.com/1244738
Commit-Queue: Justin DeWitt <dewittj@chromium.org>
Reviewed-by: default avatarDmitry Titov <dimich@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594726}
parent 2d56a039
...@@ -59,6 +59,11 @@ public class ExploreSitesBackgroundTask extends NativeBackgroundTask { ...@@ -59,6 +59,11 @@ public class ExploreSitesBackgroundTask extends NativeBackgroundTask {
protected void onStartTaskWithNative( protected void onStartTaskWithNative(
Context context, TaskParameters taskParameters, TaskFinishedCallback callback) { Context context, TaskParameters taskParameters, TaskFinishedCallback callback) {
assert taskParameters.getTaskId() == TaskIds.EXPLORE_SITES_REFRESH_JOB_ID; assert taskParameters.getTaskId() == TaskIds.EXPLORE_SITES_REFRESH_JOB_ID;
if (ExploreSitesBridge.getVariation() != ExploreSitesVariation.ENABLED) {
cancelTask();
return;
}
mTaskFinishedCallback = callback; mTaskFinishedCallback = callback;
ExploreSitesBridge.updateCatalogFromNetwork( ExploreSitesBridge.updateCatalogFromNetwork(
getProfile(), (ignored) -> mTaskFinishedCallback.taskFinished(false)); getProfile(), (ignored) -> mTaskFinishedCallback.taskFinished(false));
...@@ -78,7 +83,7 @@ public class ExploreSitesBackgroundTask extends NativeBackgroundTask { ...@@ -78,7 +83,7 @@ public class ExploreSitesBackgroundTask extends NativeBackgroundTask {
@Override @Override
public void reschedule(Context context) { public void reschedule(Context context) {
schedule(); schedule(true /* updateCurrent */);
} }
// Removes the task from the JobScheduler queue. Should be called when the feature is disabled. // Removes the task from the JobScheduler queue. Should be called when the feature is disabled.
...@@ -88,7 +93,7 @@ public class ExploreSitesBackgroundTask extends NativeBackgroundTask { ...@@ -88,7 +93,7 @@ public class ExploreSitesBackgroundTask extends NativeBackgroundTask {
} }
// Begins the periodic update task. // Begins the periodic update task.
public static void schedule() { public static void schedule(boolean updateCurrent) {
TaskInfo.Builder taskInfoBuilder = TaskInfo.Builder taskInfoBuilder =
TaskInfo.createPeriodicTask(TaskIds.EXPLORE_SITES_REFRESH_JOB_ID, TaskInfo.createPeriodicTask(TaskIds.EXPLORE_SITES_REFRESH_JOB_ID,
ExploreSitesBackgroundTask.class, ExploreSitesBackgroundTask.class,
...@@ -96,7 +101,7 @@ public class ExploreSitesBackgroundTask extends NativeBackgroundTask { ...@@ -96,7 +101,7 @@ public class ExploreSitesBackgroundTask extends NativeBackgroundTask {
TimeUnit.HOURS.toMillis(DEFAULT_FLEX_HOURS)) TimeUnit.HOURS.toMillis(DEFAULT_FLEX_HOURS))
.setRequiredNetworkType(TaskInfo.NETWORK_TYPE_ANY) .setRequiredNetworkType(TaskInfo.NETWORK_TYPE_ANY)
.setIsPersisted(true) .setIsPersisted(true)
.setUpdateCurrent(true); .setUpdateCurrent(updateCurrent);
BackgroundTaskSchedulerFactory.getScheduler().schedule( BackgroundTaskSchedulerFactory.getScheduler().schedule(
ContextUtils.getApplicationContext(), taskInfoBuilder.build()); ContextUtils.getApplicationContext(), taskInfoBuilder.build());
} }
......
...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.explore_sites; ...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.explore_sites;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import org.chromium.base.Callback; import org.chromium.base.Callback;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace; import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
...@@ -51,6 +52,11 @@ public class ExploreSitesBridge { ...@@ -51,6 +52,11 @@ public class ExploreSitesBridge {
return nativeGetVariation(); return nativeGetVariation();
} }
@CalledByNative
static void scheduleDailyTask() {
ExploreSitesBackgroundTask.schedule(false /* updateCurrent */);
}
static native int nativeGetVariation(); static native int nativeGetVariation();
private static native void nativeGetEspCatalog(Profile profile, private static native void nativeGetEspCatalog(Profile profile,
List<ExploreSitesCategory> result, Callback<List<ExploreSitesCategory>> callback); List<ExploreSitesCategory> result, Callback<List<ExploreSitesCategory>> callback);
......
...@@ -145,7 +145,7 @@ public class ExploreSitesBackgroundTaskUnitTest { ...@@ -145,7 +145,7 @@ public class ExploreSitesBackgroundTaskUnitTest {
@Test @Test
public void scheduleTask() { public void scheduleTask() {
ExploreSitesBackgroundTask.schedule(); ExploreSitesBackgroundTask.schedule(false /* updateCurrent */);
TaskInfo scheduledTask = TaskInfo scheduledTask =
mFakeTaskScheduler.getTaskInfo(TaskIds.EXPLORE_SITES_REFRESH_JOB_ID); mFakeTaskScheduler.getTaskInfo(TaskIds.EXPLORE_SITES_REFRESH_JOB_ID);
assertNotNull(scheduledTask); assertNotNull(scheduledTask);
...@@ -161,7 +161,7 @@ public class ExploreSitesBackgroundTaskUnitTest { ...@@ -161,7 +161,7 @@ public class ExploreSitesBackgroundTaskUnitTest {
mFakeTaskScheduler.getTaskInfo(TaskIds.EXPLORE_SITES_REFRESH_JOB_ID); mFakeTaskScheduler.getTaskInfo(TaskIds.EXPLORE_SITES_REFRESH_JOB_ID);
assertNull(scheduledTask); assertNull(scheduledTask);
ExploreSitesBackgroundTask.schedule(); ExploreSitesBackgroundTask.schedule(false /* updateCurrent */);
scheduledTask = mFakeTaskScheduler.getTaskInfo(TaskIds.EXPLORE_SITES_REFRESH_JOB_ID); scheduledTask = mFakeTaskScheduler.getTaskInfo(TaskIds.EXPLORE_SITES_REFRESH_JOB_ID);
assertNotNull(scheduledTask); assertNotNull(scheduledTask);
assertEquals(TimeUnit.HOURS.toMillis(ExploreSitesBackgroundTask.DEFAULT_DELAY_HOURS), assertEquals(TimeUnit.HOURS.toMillis(ExploreSitesBackgroundTask.DEFAULT_DELAY_HOURS),
......
...@@ -2135,6 +2135,7 @@ jumbo_split_static_library("browser") { ...@@ -2135,6 +2135,7 @@ jumbo_split_static_library("browser") {
"android/explore_sites/catalog.cc", "android/explore_sites/catalog.cc",
"android/explore_sites/catalog.h", "android/explore_sites/catalog.h",
"android/explore_sites/explore_sites_bridge.cc", "android/explore_sites/explore_sites_bridge.cc",
"android/explore_sites/explore_sites_bridge.h",
"android/explore_sites/explore_sites_bridge_experimental.cc", "android/explore_sites/explore_sites_bridge_experimental.cc",
"android/explore_sites/explore_sites_feature.cc", "android/explore_sites/explore_sites_feature.cc",
"android/explore_sites/explore_sites_feature.h", "android/explore_sites/explore_sites_feature.h",
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/android/jni_android.h" #include "base/android/jni_android.h"
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h" #include "base/android/scoped_java_ref.h"
#include "chrome/browser/android/explore_sites/explore_sites_bridge.h"
#include "chrome/browser/android/explore_sites/explore_sites_feature.h" #include "chrome/browser/android/explore_sites/explore_sites_feature.h"
#include "chrome/browser/android/explore_sites/explore_sites_service.h" #include "chrome/browser/android/explore_sites/explore_sites_service.h"
#include "chrome/browser/android/explore_sites/explore_sites_service_factory.h" #include "chrome/browser/android/explore_sites/explore_sites_service_factory.h"
...@@ -147,4 +148,10 @@ void JNI_ExploreSitesBridge_UpdateCatalogFromNetwork( ...@@ -147,4 +148,10 @@ void JNI_ExploreSitesBridge_UpdateCatalogFromNetwork(
&UpdateCatalogDone, ScopedJavaGlobalRef<jobject>(j_callback_obj))); &UpdateCatalogDone, ScopedJavaGlobalRef<jobject>(j_callback_obj)));
} }
// static
void ExploreSitesBridge::ScheduleDailyTask() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_ExploreSitesBridge_scheduleDailyTask(env);
}
} // namespace explore_sites } // namespace explore_sites
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_EXPLORE_SITES_EXPLORE_SITES_BRIDGE_H_
#define CHROME_BROWSER_ANDROID_EXPLORE_SITES_EXPLORE_SITES_BRIDGE_H_
namespace explore_sites {
// Methods for interacting with the Java side via JNI.
class ExploreSitesBridge {
public:
// Causes the Android JobScheduler to execute the catalog update daily.
// The catalog update task checks that the feature is enabled and if not,
// unschedules itself.
static void ScheduleDailyTask();
};
} // namespace explore_sites
#endif // CHROME_BROWSER_ANDROID_EXPLORE_SITES_EXPLORE_SITES_BRIDGE_H_
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "chrome/browser/android/explore_sites/explore_sites_bridge.h"
#include "chrome/browser/android/explore_sites/explore_sites_feature.h"
#include "chrome/browser/android/explore_sites/explore_sites_service.h" #include "chrome/browser/android/explore_sites/explore_sites_service.h"
#include "chrome/browser/android/explore_sites/explore_sites_service_impl.h" #include "chrome/browser/android/explore_sites/explore_sites_service_impl.h"
#include "chrome/browser/android/explore_sites/explore_sites_store.h" #include "chrome/browser/android/explore_sites/explore_sites_store.h"
...@@ -29,6 +31,7 @@ ExploreSitesServiceFactory::ExploreSitesServiceFactory() ...@@ -29,6 +31,7 @@ ExploreSitesServiceFactory::ExploreSitesServiceFactory()
: BrowserContextKeyedServiceFactory( : BrowserContextKeyedServiceFactory(
"ExploreSitesService", "ExploreSitesService",
BrowserContextDependencyManager::GetInstance()) {} BrowserContextDependencyManager::GetInstance()) {}
ExploreSitesServiceFactory::~ExploreSitesServiceFactory() = default;
// static // static
ExploreSitesServiceFactory* ExploreSitesServiceFactory::GetInstance() { ExploreSitesServiceFactory* ExploreSitesServiceFactory::GetInstance() {
...@@ -42,6 +45,11 @@ ExploreSitesService* ExploreSitesServiceFactory::GetForBrowserContext( ...@@ -42,6 +45,11 @@ ExploreSitesService* ExploreSitesServiceFactory::GetForBrowserContext(
GetInstance()->GetServiceForBrowserContext(context, true)); GetInstance()->GetServiceForBrowserContext(context, true));
} }
bool ExploreSitesServiceFactory::ServiceIsCreatedWithBrowserContext() const {
return chrome::android::explore_sites::GetExploreSitesVariation() ==
chrome::android::explore_sites::ExploreSitesVariation::ENABLED;
}
KeyedService* ExploreSitesServiceFactory::BuildServiceInstanceFor( KeyedService* ExploreSitesServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const { content::BrowserContext* context) const {
Profile* profile = Profile::FromBrowserContext(context); Profile* profile = Profile::FromBrowserContext(context);
...@@ -54,6 +62,8 @@ KeyedService* ExploreSitesServiceFactory::BuildServiceInstanceFor( ...@@ -54,6 +62,8 @@ KeyedService* ExploreSitesServiceFactory::BuildServiceInstanceFor(
scoped_refptr<network::SharedURLLoaderFactory> url_fetcher = scoped_refptr<network::SharedURLLoaderFactory> url_fetcher =
profile->GetURLLoaderFactory(); profile->GetURLLoaderFactory();
ExploreSitesBridge::ScheduleDailyTask();
return new ExploreSitesServiceImpl(std::move(explore_sites_store), return new ExploreSitesServiceImpl(std::move(explore_sites_store),
url_fetcher); url_fetcher);
} }
......
...@@ -29,7 +29,10 @@ class ExploreSitesServiceFactory : public BrowserContextKeyedServiceFactory { ...@@ -29,7 +29,10 @@ class ExploreSitesServiceFactory : public BrowserContextKeyedServiceFactory {
friend struct base::DefaultSingletonTraits<ExploreSitesServiceFactory>; friend struct base::DefaultSingletonTraits<ExploreSitesServiceFactory>;
ExploreSitesServiceFactory(); ExploreSitesServiceFactory();
~ExploreSitesServiceFactory() override {} ~ExploreSitesServiceFactory() override;
// BrowserContextKeyedServiceFactory implementation:
bool ServiceIsCreatedWithBrowserContext() const override;
KeyedService* BuildServiceInstanceFor( KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override; content::BrowserContext* context) const override;
......
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