Skip to content

Commit

Permalink
Creates the shell for PreviewsOnePlatformHintsFetcher and implements …
Browse files Browse the repository at this point in the history
…a private method CreateGetHintsRequestFromHosts.

PreviewsOnePlatformHintsFetcher provides the functionality for handling
the SimpleURLLoader request to the Optimization Guide Service for
OnePlatform hints to be requested based on individual users.


Bug: 932707
Change-Id: Ib3ee6b5bccf82870d196253b48f5961ddd4054d5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1506532
Reviewed-by: Tarun Bansal <tbansal@chromium.org>
Commit-Queue: Michael Crouse <mcrouse@chromium.org>
Auto-Submit: Michael Crouse <mcrouse@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638831}
  • Loading branch information
Michael Crouse authored and Commit Bot committed Mar 8, 2019
1 parent 0f33e6e commit a8778e8
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 7 deletions.
2 changes: 2 additions & 0 deletions components/previews/content/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ static_library("content") {
"hint_cache_leveldb_store.cc",
"hint_cache_leveldb_store.h",
"hint_cache_store.h",
"hints_fetcher.cc",
"hints_fetcher.h",
"previews_decider_impl.cc",
"previews_decider_impl.h",
"previews_hints.cc",
Expand Down
63 changes: 63 additions & 0 deletions components/previews/content/hints_fetcher.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2019 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/previews/content/hints_fetcher.h"

#include "base/feature_list.h"
#include "base/metrics/histogram_macros.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/previews/content/hint_cache.h"
#include "components/previews/core/previews_experiments.h"

namespace previews {

HintsFetcher::HintsFetcher(HintCache* hint_cache) : hint_cache_(hint_cache) {}

HintsFetcher::~HintsFetcher() {}

void HintsFetcher::FetchHintsForHosts(const std::vector<std::string>& hosts) {
SEQUENCE_CHECKER(sequence_checker_);

GetRemoteHints(hosts);
}

void HintsFetcher::GetRemoteHints(const std::vector<std::string>& hosts) {
get_hints_request_ =
std::make_unique<optimization_guide::proto::GetHintsRequest>();

// Add all the optimizations supported by the current version of Chrome,
// regardless of whether the session is in holdback for either of them.
get_hints_request_->add_supported_optimizations(
optimization_guide::proto::NOSCRIPT);
get_hints_request_->add_supported_optimizations(
optimization_guide::proto::RESOURCE_LOADING);
static_assert(static_cast<int>(PreviewsType::LITE_PAGE_REDIRECT) + 1 ==
static_cast<int>(PreviewsType::LAST),
"PreviewsType has been updated, make sure OnePlatform hints "
"are not affected");

get_hints_request_->set_context(
optimization_guide::proto::CONTEXT_BATCH_UPDATE);

for (const auto& host : hosts) {
optimization_guide::proto::HostInfo* host_info =
get_hints_request_->add_hosts();
host_info->set_host(host);
}
}

void HintsFetcher::HandleResponse(const std::string& config_data,
int status,
int response_code) {}

void HintsFetcher::OnURLLoadComplete(
std::unique_ptr<std::string> response_body) {}

bool HintsFetcher::ParseGetHintsResponseAndApplyHints(
const optimization_guide::proto::GetHintsResponse& get_hints_response) {
ALLOW_UNUSED_LOCAL(hint_cache_);
return false;
}

} // namespace previews
78 changes: 78 additions & 0 deletions components/previews/content/hints_fetcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2019 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 COMPONENTS_PREVIEWS_CONTENT_HINTS_FETCHER_H_
#define COMPONENTS_PREVIEWS_CONTENT_HINTS_FETCHER_H_

#include <memory>
#include <string>
#include <vector>

#include "base/macros.h"
#include "base/sequence_checker.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/previews/core/previews_experiments.h"
#include "url/gurl.h"

namespace previews {

class HintCache;

// A class to handle OnePlatform client requests for optimization hints from
// the remote Optimization Guide Service.
//
// When Chrome starts up, if the client's OnePlatform hints have not been
// updated in over day, this class will be triggered to fetch new hints from the
// remote Optimization Guide Service. Owner must ensure that |hint_cache|
// remains alive for the lifetime of |HintsFetcher|.
class HintsFetcher {
public:
explicit HintsFetcher(HintCache* hint_cache);
~HintsFetcher();

// Handles any configuration or checking needed and then
// initiates the fetch of OnePlatform Hints.
void FetchHintsForHosts(const std::vector<std::string>& hosts);

private:
// Creates the GetHintsResponse proto and executes the SimpleURLLoader request
// to the remote Optimization Guide Service with the |get_hints_request_|.
void GetRemoteHints(const std::vector<std::string>& hosts);

// Handles the response from the remote Optimization Guide Service.
// |response| is the response body, |status| is the
// |net::Error| of the response, and response_code is the HTTP
// response code (if available).
void HandleResponse(const std::string& response,
int status,
int response_code);

// URL loader completion callback.
void OnURLLoadComplete(std::unique_ptr<std::string> response_body);

// Parses the hints component of |get_hints_response| and applies it to
// |hints_|. Returns true if |get_hints_response| was successfully
// parsed and applied.
bool ParseGetHintsResponseAndApplyHints(
const optimization_guide::proto::GetHintsResponse& get_hints_response);

// Used to hold the GetHintsRequest being constructed and sent as a remote
// request.
std::unique_ptr<optimization_guide::proto::GetHintsRequest>
get_hints_request_;

// The URL for the remote Optimization Guide Service.
const GURL oneplatform_service_url_;

// The caller must ensure that the |hints_| outlives this instance.
HintCache* hint_cache_ = nullptr;

SEQUENCE_CHECKER(sequence_checker_);

DISALLOW_COPY_AND_ASSIGN(HintsFetcher);
};

} // namespace previews

#endif // COMPONENTS_PREVIEWS_CONTENT_HINTS_FETCHER_H_
8 changes: 8 additions & 0 deletions components/previews/content/previews_optimization_guide.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "components/optimization_guide/optimization_guide_service.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/previews/content/hint_cache_leveldb_store.h"
#include "components/previews/content/hints_fetcher.h"
#include "components/previews/content/previews_hints.h"
#include "components/previews/content/previews_hints_util.h"
#include "components/previews/content/previews_top_host_provider.h"
Expand Down Expand Up @@ -226,6 +227,7 @@ void PreviewsOptimizationGuide::OnHintCacheInitialized() {
// feature state:
// (1) Data saver should be enabled
// (2) Infobar notification does not need to be shown to the user.

if (previews::params::IsOnePlatformHintsEnabled()) {
// TODO(mcrouse): We will likely need to an async call and likely
// within a timer that will call GetOnePlatformClientHints().
Expand Down Expand Up @@ -273,6 +275,12 @@ void PreviewsOptimizationGuide::GetOnePlatformClientHints() {
LOCAL_HISTOGRAM_COUNTS_100("Previews.HintsFetcher.GetHintsRequest.HostCount",
top_hosts.size());

if (!hintsfetcher_) {
hintsfetcher_ = std::make_unique<HintsFetcher>(hint_cache_.get());
}

hintsfetcher_->FetchHintsForHosts(top_hosts);

// TODO(mcrouse) to build SimpleURLLoader to perform request from service
// for per-user client hints.
// Pass callback for when URLLoader request is successful to call
Expand Down
18 changes: 11 additions & 7 deletions components/previews/content/previews_optimization_guide.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Hint;

namespace previews {

class HintsFetcher;
class PreviewsHints;
class PreviewsTopHostProvider;
class PreviewsUserData;
Expand Down Expand Up @@ -119,14 +120,13 @@ class PreviewsOptimizationGuide
const GURL& document_url,
const optimization_guide::proto::Hint* loaded_hint) const;

// Method to request OnePlatform client hints for user's sites with
// top engagement scores and creates a remote request to the OnePlatform
// Service. On request success OnOnePlatformHintsReceived callback will be
// called.
// Method to request OnePlatform client hints for user's sites with top
// engagement scores and creates a remote request using |hints_fetcher_| On
// request success OnOnePlatformHintsReceived callback will be called.
void GetOnePlatformClientHints();

// Called when the response form the OnePlatform Service for hints is
// received.
// Called when the response from the OnePlatform Guide Service is handled and
// stored by the |hints_fetcher_|. received.
void OnOnePlatformHintsReceived();

// The OptimizationGuideService that this guide is listening to. Not owned.
Expand All @@ -150,8 +150,12 @@ class PreviewsOptimizationGuide
// Used in testing to subscribe to an update event in this class.
base::OnceClosure next_update_closure_;

// HintsFetcher handles the request to update Hints from OnePlatform Guide
// Service.
std::unique_ptr<HintsFetcher> hintsfetcher_;

// TopHostProvider that this guide can query. Not owned.
PreviewsTopHostProvider* previews_top_host_provider_;
PreviewsTopHostProvider* previews_top_host_provider_ = nullptr;

// Used to get |weak_ptr_| to self on the UI thread.
base::WeakPtrFactory<PreviewsOptimizationGuide> ui_weak_ptr_factory_;
Expand Down

0 comments on commit a8778e8

Please sign in to comment.