Commit 2f061798 authored by jkrcal's avatar jkrcal Committed by Commit bot

[Remote suggestions] Prioritize wifi for soft fetches.

This CL allows to set different intervals for soft fetches for wifi and
fallback connections. This puts soft fetches on par to persistent
fetches with respect to parameters.

Previously we prioritized soft fetches triggered by NTP_OPENED. This
feature is removed by this CL for the sake of simplicity (NTP_OPENED
is the only trigger that we employ on M57 Stable).

BUG=708146

Review-Url: https://codereview.chromium.org/2794313002
Cr-Commit-Position: refs/heads/master@{#462937}
parent d821806d
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
package org.chromium.chrome.browser.ntp.snippets; package org.chromium.chrome.browser.ntp.snippets;
import android.content.Context;
import android.net.ConnectivityManager;
import com.google.android.gms.gcm.GcmNetworkManager; import com.google.android.gms.gcm.GcmNetworkManager;
import com.google.android.gms.gcm.PeriodicTask; import com.google.android.gms.gcm.PeriodicTask;
import com.google.android.gms.gcm.Task; import com.google.android.gms.gcm.Task;
...@@ -165,6 +168,14 @@ public class SnippetsLauncher { ...@@ -165,6 +168,14 @@ public class SnippetsLauncher {
return schedule(0, 0); return schedule(0, 0);
} }
@CalledByNative
public boolean isOnUnmeteredConnection() {
Context context = ContextUtils.getApplicationContext();
ConnectivityManager manager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return !manager.isActiveNetworkMetered();
}
public static boolean shouldRescheduleTasksOnUpgrade() { public static boolean shouldRescheduleTasksOnUpgrade() {
// Reschedule the periodic tasks if they were enabled previously. // Reschedule the periodic tasks if they were enabled previously.
return ContextUtils.getAppSharedPreferences().getBoolean(PREF_IS_SCHEDULED, false); return ContextUtils.getAppSharedPreferences().getBoolean(PREF_IS_SCHEDULED, false);
......
...@@ -39,6 +39,13 @@ bool NTPSnippetsLauncher::Unschedule() { ...@@ -39,6 +39,13 @@ bool NTPSnippetsLauncher::Unschedule() {
return Java_SnippetsLauncher_unschedule(env, java_launcher_); return Java_SnippetsLauncher_unschedule(env, java_launcher_);
} }
bool NTPSnippetsLauncher::IsOnUnmeteredConnection() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
JNIEnv* env = base::android::AttachCurrentThread();
return Java_SnippetsLauncher_isOnUnmeteredConnection(env, java_launcher_);
}
NTPSnippetsLauncher::NTPSnippetsLauncher() { NTPSnippetsLauncher::NTPSnippetsLauncher() {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
......
...@@ -22,6 +22,7 @@ class NTPSnippetsLauncher : public ntp_snippets::PersistentScheduler { ...@@ -22,6 +22,7 @@ class NTPSnippetsLauncher : public ntp_snippets::PersistentScheduler {
bool Schedule(base::TimeDelta period_wifi, bool Schedule(base::TimeDelta period_wifi,
base::TimeDelta period_fallback) override; base::TimeDelta period_fallback) override;
bool Unschedule() override; bool Unschedule() override;
bool IsOnUnmeteredConnection() override;
private: private:
friend struct base::LazyInstanceTraitsBase<NTPSnippetsLauncher>; friend struct base::LazyInstanceTraitsBase<NTPSnippetsLauncher>;
......
...@@ -13,11 +13,11 @@ const char kRemoteSuggestionCategories[] = "ntp_snippets.remote_categories"; ...@@ -13,11 +13,11 @@ const char kRemoteSuggestionCategories[] = "ntp_snippets.remote_categories";
const char kSnippetLastFetchAttempt[] = "ntp_snippets.last_fetch_attempt"; const char kSnippetLastFetchAttempt[] = "ntp_snippets.last_fetch_attempt";
const char kSnippetSoftFetchingIntervalOnUsageEvent[] = const char kSnippetSoftFetchingIntervalWifi[] =
"ntp_snippets.soft_fetching_interval_on_usage_event"; "ntp_snippets.soft_fetching_interval_wifi";
const char kSnippetSoftFetchingIntervalOnNtpOpened[] = const char kSnippetSoftFetchingIntervalFallback[] =
"ntp_snippets.soft_fetching_interval_on_ntp_opened"; "ntp_snippets.soft_fetching_interval_fallback";
const char kSnippetPersistentFetchingIntervalWifi[] = const char kSnippetPersistentFetchingIntervalWifi[] =
"ntp_snippets.fetching_interval_wifi"; "ntp_snippets.fetching_interval_wifi";
......
...@@ -20,11 +20,12 @@ extern const char kRemoteSuggestionCategories[]; ...@@ -20,11 +20,12 @@ extern const char kRemoteSuggestionCategories[];
extern const char kSnippetLastFetchAttempt[]; extern const char kSnippetLastFetchAttempt[];
// The pref name for the currently applied minimal interval between two // The pref name for the currently applied minimal interval between two
// successive soft background fetches that react to user activity (such as // successive soft background fetches that react to user activity (such as
// opening Chrome). // opening Chrome) when there is a WiFi connectivity.
extern const char kSnippetSoftFetchingIntervalOnUsageEvent[]; extern const char kSnippetSoftFetchingIntervalWifi[];
// The pref name for the currently applied minimal interval between two // The pref name for the currently applied minimal interval between two
// successive soft brackground fetches when the New Tab Page is opened. // successive soft background fetches that react to user activity (such as
extern const char kSnippetSoftFetchingIntervalOnNtpOpened[]; // opening Chrome) when there is no WiFi connectivity.
extern const char kSnippetSoftFetchingIntervalFallback[];
// The pref name for the currently-scheduled background fetching interval when // The pref name for the currently-scheduled background fetching interval when
// there is WiFi connectivity. // there is WiFi connectivity.
......
...@@ -35,6 +35,10 @@ class PersistentScheduler { ...@@ -35,6 +35,10 @@ class PersistentScheduler {
// the scheduling was successful. // the scheduling was successful.
virtual bool Unschedule() = 0; virtual bool Unschedule() = 0;
// TODO(jkrcal): Get this information exposed in the platform-independent
// net::NetworkChangeNotifier and remove this function.
virtual bool IsOnUnmeteredConnection() = 0;
protected: protected:
PersistentScheduler() = default; PersistentScheduler() = default;
......
...@@ -61,7 +61,8 @@ class RemoteSuggestionsSchedulerImpl : public RemoteSuggestionsScheduler { ...@@ -61,7 +61,8 @@ class RemoteSuggestionsSchedulerImpl : public RemoteSuggestionsScheduler {
void OnNTPOpened() override; void OnNTPOpened() override;
private: private:
// Abstract description of the fetching schedule. // Abstract description of the fetching schedule. See the enum
// FetchingInterval for more documentation.
struct FetchingSchedule { struct FetchingSchedule {
static FetchingSchedule Empty(); static FetchingSchedule Empty();
bool operator==(const FetchingSchedule& other) const; bool operator==(const FetchingSchedule& other) const;
...@@ -70,8 +71,8 @@ class RemoteSuggestionsSchedulerImpl : public RemoteSuggestionsScheduler { ...@@ -70,8 +71,8 @@ class RemoteSuggestionsSchedulerImpl : public RemoteSuggestionsScheduler {
base::TimeDelta interval_persistent_wifi; base::TimeDelta interval_persistent_wifi;
base::TimeDelta interval_persistent_fallback; base::TimeDelta interval_persistent_fallback;
base::TimeDelta interval_soft_on_usage_event; base::TimeDelta interval_soft_wifi;
base::TimeDelta interval_soft_on_ntp_opened; base::TimeDelta interval_soft_fallback;
}; };
enum class TriggerType; enum class TriggerType;
...@@ -86,15 +87,13 @@ class RemoteSuggestionsSchedulerImpl : public RemoteSuggestionsScheduler { ...@@ -86,15 +87,13 @@ class RemoteSuggestionsSchedulerImpl : public RemoteSuggestionsScheduler {
// schedule. // schedule.
void StopScheduling(); void StopScheduling();
// Trigger a background refetch for the given |trigger| if enabled. // Trigger a background refetch for the given |trigger| if enabled and if the
void RefetchInTheBackgroundIfEnabled(TriggerType trigger); // timing is appropriate for another fetch.
void RefetchInTheBackgroundIfAppropriate(TriggerType trigger);
// Trigger the background refetch.
void RefetchInTheBackground();
// Checks whether it is time to perform a soft background fetch, according to // Checks whether it is time to perform a soft background fetch, according to
// |schedule|. // |schedule|.
bool ShouldRefetchInTheBackgroundNow(TriggerType trigger); bool ShouldRefetchInTheBackgroundNow();
// Returns whether background fetching (for the given |trigger|) is disabled. // Returns whether background fetching (for the given |trigger|) is disabled.
bool BackgroundFetchesDisabled(TriggerType trigger) const; bool BackgroundFetchesDisabled(TriggerType trigger) const;
......
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