Commit 7a4a2d1f authored by Pete Williamson's avatar Pete Williamson Committed by Commit Bot

[EoS] Add scale factor header to catalog request.

This adds the scale factor of the android device into a new header
called "X-Device-Scale-Factor". The scale factor is derived from
android.util.DisplayMetrics.density.

Change-Id: Ia8f26435fdc780354e753f87632aca03811c144a

Bug: 893238
Change-Id: Ia8f26435fdc780354e753f87632aca03811c144a
Reviewed-on: https://chromium-review.googlesource.com/c/1260208
Commit-Queue: Peter Williamson <petewil@chromium.org>
Reviewed-by: default avatarJustin DeWitt <dewittj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#597636}
parent e4e144c2
......@@ -4,9 +4,13 @@
package org.chromium.chrome.browser.explore_sites;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.browser.profiles.Profile;
......@@ -65,6 +69,21 @@ public class ExploreSitesBridge {
ExploreSitesBackgroundTask.schedule(false /* updateCurrent */);
}
/**
* Returns the scale factor on this device.
*/
@CalledByNative
static float getScaleFactorFromDevice() {
// Get DeviceMetrics from context.
DisplayMetrics metrics = new DisplayMetrics();
((WindowManager) ContextUtils.getApplicationContext().getSystemService(
Context.WINDOW_SERVICE))
.getDefaultDisplay()
.getMetrics(metrics);
// Get density and return it.
return metrics.density;
}
static native int nativeGetVariation();
private static native void nativeGetEspCatalog(Profile profile,
List<ExploreSitesCategory> result, Callback<List<ExploreSitesCategory>> callback);
......
......@@ -175,4 +175,10 @@ void ExploreSitesBridge::ScheduleDailyTask() {
Java_ExploreSitesBridge_scheduleDailyTask(env);
}
float ExploreSitesBridge::GetScaleFactorFromDevice() {
JNIEnv* env = base::android::AttachCurrentThread();
// Get scale factor from Java as a float.
return Java_ExploreSitesBridge_getScaleFactorFromDevice(env);
}
} // namespace explore_sites
......@@ -14,6 +14,9 @@ class ExploreSitesBridge {
// The catalog update task checks that the feature is enabled and if not,
// unschedules itself.
static void ScheduleDailyTask();
// Gets the device screen scale factor from Android.
static float GetScaleFactorFromDevice();
};
} // namespace explore_sites
......
......@@ -16,6 +16,7 @@
#include "base/version.h"
#include "chrome/browser/android/chrome_feature_list.h"
#include "chrome/browser/android/explore_sites/catalog.pb.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_types.h"
#include "chrome/browser/android/explore_sites/url_util.h"
......@@ -153,6 +154,7 @@ ExploreSitesFetcher::ExploreSitesFetcher(
max_failure_count_(is_immediate_fetch
? kMaxFailureCountForImmediateFetch
: kMaxFailureCountForBackgroundFetch),
device_delegate_(std::make_unique<DeviceDelegate>()),
callback_(std::move(callback)),
url_loader_factory_(loader_factory),
weak_factory_(this) {
......@@ -183,6 +185,10 @@ void ExploreSitesFetcher::Start() {
resource_request->headers.SetHeader("X-Client-Version", client_version_);
resource_request->headers.SetHeader(net::HttpRequestHeaders::kContentType,
kRequestContentType);
std::string scale_factor =
std::to_string(device_delegate_->GetScaleFactorFromDevice());
resource_request->headers.SetHeader("X-Device-Scale-Factor", scale_factor);
if (!accept_languages_.empty()) {
resource_request->headers.SetHeader(
net::HttpRequestHeaders::kAcceptLanguage, accept_languages_);
......@@ -204,6 +210,15 @@ void ExploreSitesFetcher::Start() {
weak_factory_.GetWeakPtr()));
}
float ExploreSitesFetcher::DeviceDelegate::GetScaleFactorFromDevice() {
return ExploreSitesBridge::GetScaleFactorFromDevice();
}
void ExploreSitesFetcher::SetDeviceDelegateForTest(
std::unique_ptr<ExploreSitesFetcher::DeviceDelegate> device_delegate) {
device_delegate_ = std::move(device_delegate);
}
void ExploreSitesFetcher::OnSimpleLoaderComplete(
std::unique_ptr<std::string> response_body) {
ExploreSitesRequestStatus status = HandleResponseCode();
......
......@@ -57,6 +57,18 @@ class ExploreSitesFetcher {
void disable_retry_for_testing() { disable_retry_for_testing_ = true; }
// Delegate that knows how to get data from the device. Can be overridden for
// testing.
class DeviceDelegate {
public:
DeviceDelegate() = default;
virtual ~DeviceDelegate() = default;
virtual float GetScaleFactorFromDevice();
};
// Allow overriding device specific functionality for testing
void SetDeviceDelegateForTest(std::unique_ptr<DeviceDelegate> delegate);
private:
explicit ExploreSitesFetcher(
bool is_immediate_fetch,
......@@ -81,6 +93,7 @@ class ExploreSitesFetcher {
int max_failure_count_;
bool disable_retry_for_testing_ = false;
std::unique_ptr<DeviceDelegate> device_delegate_;
Callback callback_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
......
......@@ -41,6 +41,12 @@ const char kTestData[] = "Any data.";
namespace explore_sites {
class TestingDeviceDelegate : public ExploreSitesFetcher::DeviceDelegate {
public:
TestingDeviceDelegate() = default;
float GetScaleFactorFromDevice() override { return 1.5; }
};
// TODO(freedjm): Add tests for the headers.
class ExploreSitesFetcherTest : public testing::Test {
public:
......@@ -249,6 +255,7 @@ std::unique_ptr<ExploreSitesFetcher> ExploreSitesFetcherTest::CreateFetcher(
test_shared_url_loader_factory_, StoreResult());
if (disable_retry)
fetcher->disable_retry_for_testing();
fetcher->SetDeviceDelegateForTest(std::make_unique<TestingDeviceDelegate>());
fetcher->Start();
return fetcher;
}
......@@ -328,6 +335,7 @@ TEST_F(ExploreSitesFetcherTest, TestHeaders) {
net::HttpRequestHeaders headers = last_resource_request.headers;
std::string content_type;
std::string languages;
std::string scale_factor;
bool success;
success = headers.HasHeader("x-goog-api-key");
......@@ -336,15 +344,19 @@ TEST_F(ExploreSitesFetcherTest, TestHeaders) {
success = headers.HasHeader("X-Client-Version");
EXPECT_TRUE(success);
success = headers.GetHeader("X-Device-Scale-Factor", &scale_factor);
EXPECT_TRUE(success);
EXPECT_EQ(1.5, std::stof(scale_factor));
success =
headers.GetHeader(net::HttpRequestHeaders::kContentType, &content_type);
EXPECT_TRUE(success);
EXPECT_EQ(content_type, "application/x-protobuf");
EXPECT_EQ("application/x-protobuf", content_type);
success =
headers.GetHeader(net::HttpRequestHeaders::kAcceptLanguage, &languages);
EXPECT_TRUE(success);
EXPECT_EQ(languages, kAcceptLanguages);
EXPECT_EQ(kAcceptLanguages, languages);
// The finch header should not be set since the experiment is not on.
success = headers.HasHeader("X-Google-Chrome-Experiment-Tag");
......
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