Commit dd28f282 authored by Sky Malice's avatar Sky Malice Committed by Commit Bot

[Feed] Track the last fetch attempt within scheduler.

This incremental change adds the pref dependency and starts to register
feed preferences.

Bug: 831648
Change-Id: Ifb5ed97ac1de87a6319539839f9c57be46fb89b1
Reviewed-on: https://chromium-review.googlesource.com/1093982Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Commit-Queue: Sky Malice <skym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567112}
parent 225aa5c0
......@@ -2396,6 +2396,7 @@ jumbo_split_static_library("browser") {
"//chrome/services/media_gallery_util/public/cpp",
"//components/cdm/browser",
"//components/data_usage/android",
"//components/feed:buildflags",
"//components/feed:feature_list",
"//components/payments/content/android",
"//components/resources:components_resources",
......
......@@ -9,6 +9,7 @@
#include <utility>
#include "base/files/file_path.h"
#include "base/time/default_clock.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/suggestions/image_decoder_impl.h"
#include "chrome/browser/signin/identity_manager_factory.h"
......@@ -82,7 +83,8 @@ KeyedService* FeedHostServiceFactory::BuildServiceInstanceFor(
auto image_manager = std::make_unique<FeedImageManager>(
std::move(image_fetcher), std::move(image_database));
auto scheduler_host = std::make_unique<FeedSchedulerHost>();
auto scheduler_host = std::make_unique<FeedSchedulerHost>(
profile->GetPrefs(), base::DefaultClock::GetInstance());
return new FeedHostService(std::move(image_manager),
std::move(networking_host),
......
......@@ -184,10 +184,14 @@
#include "chrome/browser/geolocation/geolocation_permission_context_android.h"
#include "chrome/browser/ntp_snippets/download_suggestions_provider.h"
#include "components/cdm/browser/media_drm_storage_impl.h"
#include "components/feed/buildflags.h"
#include "components/ntp_snippets/breaking_news/breaking_news_gcm_app_handler.h"
#include "components/ntp_snippets/breaking_news/subscription_manager_impl.h"
#include "components/ntp_snippets/category_rankers/click_based_category_ranker.h"
#include "components/ntp_tiles/popular_sites_impl.h"
#if BUILDFLAG(ENABLE_FEED_IN_CHROME)
#include "components/feed/core/feed_scheduler_host.h"
#endif // BUILDFLAG(ENABLE_FEED_IN_CHROME)
#else
#include "chrome/browser/gcm/gcm_product_util.h"
#include "chrome/browser/media/router/media_router_feature.h"
......@@ -585,13 +589,16 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
GeolocationPermissionContextAndroid::RegisterProfilePrefs(registry);
PartnerBookmarksShim::RegisterProfilePrefs(registry);
RecentTabsPagePrefs::RegisterProfilePrefs(registry);
#if BUILDFLAG(ENABLE_FEED_IN_CHROME)
feed::FeedSchedulerHost::RegisterProfilePrefs(registry);
#endif // BUILDFLAG(ENABLE_FEED_IN_CHROME)
#else
AppShortcutManager::RegisterProfilePrefs(registry);
DeviceIDFetcher::RegisterProfilePrefs(registry);
DevToolsWindow::RegisterProfilePrefs(registry);
#if BUILDFLAG(ENABLE_APP_LIST)
app_list::AppListSyncableService::RegisterProfilePrefs(registry);
#endif
#endif // BUILDFLAG(ENABLE_APP_LIST)
extensions::CommandService::RegisterProfilePrefs(registry);
extensions::TabsCaptureVisibleTabFunction::RegisterProfilePrefs(registry);
NewTabUI::RegisterProfilePrefs(registry);
......
......@@ -3,6 +3,7 @@ include_rules = [
"+components/image_fetcher",
"+components/keyed_service/core",
"+components/leveldb_proto",
"+components/prefs",
"+components/variations",
"+components/version_info",
"+net/base",
......
......@@ -37,6 +37,7 @@ source_set("feed_core") {
deps = [
"//components/data_use_measurement/core",
"//components/prefs",
"//components/variations",
"//components/variations/net",
"//components/variations/service",
......@@ -62,6 +63,7 @@ source_set("core_unit_tests") {
"feed_image_database_unittest.cc",
"feed_image_manager_unittest.cc",
"feed_networking_host_unittest.cc",
"feed_scheduler_host_unittest.cc",
"feed_storage_database_unittest.cc",
]
......@@ -70,6 +72,7 @@ source_set("core_unit_tests") {
"//base",
"//base/test:test_support",
"//components/leveldb_proto:test_support",
"//components/prefs:test_support",
"//net:test_support",
"//services/identity/public/cpp",
"//services/identity/public/cpp:test_support",
......
......@@ -6,25 +6,41 @@
#include <utility>
#include "base/time/clock.h"
#include "base/time/time.h"
#include "components/feed/core/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
namespace feed {
FeedSchedulerHost::FeedSchedulerHost() {}
FeedSchedulerHost::FeedSchedulerHost(PrefService* pref_service,
base::Clock* clock)
: pref_service_(pref_service), clock_(clock) {}
FeedSchedulerHost::~FeedSchedulerHost() {}
// static
void FeedSchedulerHost::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterTimePref(prefs::kLastFetchAttemptTime, base::Time());
}
NativeRequestBehavior FeedSchedulerHost::ShouldSessionRequestData(
bool has_content,
base::Time content_creation_date_time,
bool has_outstanding_request) {
return REQUEST_WITH_CONTENT;
return REQUEST_WITH_WAIT;
}
void FeedSchedulerHost::OnReceiveNewContent(
base::Time content_creation_date_time) {}
base::Time content_creation_date_time) {
pref_service_->SetTime(prefs::kLastFetchAttemptTime, clock_->Now());
ScheduleFixedTimerWakeUp();
}
void FeedSchedulerHost::OnRequestError(int network_response_code) {}
void FeedSchedulerHost::OnRequestError(int network_response_code) {
pref_service_->SetTime(prefs::kLastFetchAttemptTime, clock_->Now());
}
void FeedSchedulerHost::RegisterTriggerRefreshCallback(
base::RepeatingClosure callback) {
......@@ -35,4 +51,8 @@ void FeedSchedulerHost::RegisterTriggerRefreshCallback(
trigger_refresh_ = std::move(callback);
}
void FeedSchedulerHost::ScheduleFixedTimerWakeUp() {
// TODO(skym): Implementation, call out to injected scheduling dependency.
}
} // namespace feed
......@@ -8,7 +8,11 @@
#include "base/callback.h"
#include "base/macros.h"
class PrefRegistrySimple;
class PrefService;
namespace base {
class Clock;
class Time;
} // namespace base
......@@ -34,9 +38,11 @@ enum NativeRequestBehavior {
// content.
class FeedSchedulerHost {
public:
FeedSchedulerHost();
FeedSchedulerHost(PrefService* pref_service, base::Clock* clock);
~FeedSchedulerHost();
static void RegisterProfilePrefs(PrefRegistrySimple* registry);
// Called when the NTP is opened to decide how to handle displaying and
// refreshing content.
NativeRequestBehavior ShouldSessionRequestData(
......@@ -54,8 +60,17 @@ class FeedSchedulerHost {
void RegisterTriggerRefreshCallback(base::RepeatingClosure callback);
private:
void ScheduleFixedTimerWakeUp();
// Callback to request that an async refresh be started.
base::RepeatingClosure trigger_refresh_;
// Non-owning reference to pref service providing durable storage.
PrefService* pref_service_;
// Non-owning reference to clock to get current time.
base::Clock* clock_;
DISALLOW_COPY_AND_ASSIGN(FeedSchedulerHost);
};
......
// 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.
#include "components/feed/core/feed_scheduler_host.h"
#include "base/test/simple_test_clock.h"
#include "components/feed/core/pref_names.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace feed {
namespace {
// Fixed "now" to make tests more deterministic.
char kNowString[] = "2018-06-11 15:41";
using base::Time;
class FeedSchedulerHostTest : public ::testing::Test {
public:
protected:
FeedSchedulerHostTest() : scheduler_(&pref_service_, &test_clock_) {
base::Time now;
CHECK(base::Time::FromUTCString(kNowString, &now));
test_clock_.SetNow(now);
FeedSchedulerHost::RegisterProfilePrefs(pref_service_.registry());
}
PrefService* pref_service() { return &pref_service_; }
base::SimpleTestClock* test_clock() { return &test_clock_; }
FeedSchedulerHost* scheduler() { return &scheduler_; }
private:
TestingPrefServiceSimple pref_service_;
base::SimpleTestClock test_clock_;
FeedSchedulerHost scheduler_;
};
TEST_F(FeedSchedulerHostTest, OnReceiveNewContent) {
EXPECT_EQ(Time(), pref_service()->GetTime(prefs::kLastFetchAttemptTime));
scheduler()->OnReceiveNewContent(Time());
EXPECT_EQ(test_clock()->Now(),
pref_service()->GetTime(prefs::kLastFetchAttemptTime));
}
TEST_F(FeedSchedulerHostTest, OnRequestError) {
EXPECT_EQ(Time(), pref_service()->GetTime(prefs::kLastFetchAttemptTime));
scheduler()->OnRequestError(0);
EXPECT_EQ(test_clock()->Now(),
pref_service()->GetTime(prefs::kLastFetchAttemptTime));
}
} // namespace
} // namespace feed
......@@ -6,6 +6,10 @@
namespace feed {
namespace prefs {} // namespace prefs
namespace prefs {
const char kLastFetchAttemptTime[] = "feed.last_fetch_attempt";
} // namespace prefs
} // namespace feed
......@@ -7,7 +7,12 @@
namespace feed {
namespace prefs {} // namespace prefs
namespace prefs {
// The pref name for the last time when a background fetch was attempted.
extern const char kLastFetchAttemptTime[];
} // namespace prefs
} // namespace feed
......
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