Commit 837bac24 authored by Tarun Bansal's avatar Tarun Bansal Committed by Commit Bot

Expose client hints as a keyed service

Instead of static class, expose client hints as a keyed service.
Next CL would add a new abstract class client_hints_controller
in //content, and make client hints in //chrome, a derived class
of the client_hints_controller. This would allow
content to directly call the methods to determine which client
hint headers should be added.

Change-Id: I8971e77a23dd49f2f25ba8036ca8ea76c24ca2a4
Bug: 875703
Reviewed-on: https://chromium-review.googlesource.com/c/1368208
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#615826}
parent f8596800
......@@ -278,6 +278,8 @@ jumbo_split_static_library("browser") {
"chrome_service.h",
"client_hints/client_hints.cc",
"client_hints/client_hints.h",
"client_hints/client_hints_factory.cc",
"client_hints/client_hints_factory.h",
"client_hints/client_hints_observer.cc",
"client_hints/client_hints_observer.h",
"clipboard/clipboard_read_permission_context.cc",
......
......@@ -40,6 +40,7 @@
#include "chrome/browser/chrome_content_browser_client_parts.h"
#include "chrome/browser/chrome_quota_permission_context.h"
#include "chrome/browser/client_hints/client_hints.h"
#include "chrome/browser/client_hints/client_hints_factory.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
......@@ -2272,9 +2273,9 @@ void ChromeContentBrowserClient::NavigationRequestStarted(
WebContents* web_contents =
WebContents::FromFrameTreeNodeId(frame_tree_node_id);
content::BrowserContext* browser_context = web_contents->GetBrowserContext();
*extra_headers =
client_hints::GetAdditionalNavigationRequestClientHintsHeaders(
browser_context, url);
*extra_headers = ClientHintsFactory::GetForBrowserContext(browser_context)
->GetAdditionalNavigationRequestClientHintsHeaders(url);
prerender::PrerenderContents* prerender_contents =
prerender::PrerenderContents::FromWebContents(web_contents);
if (prerender_contents &&
......@@ -2318,8 +2319,8 @@ void ChromeContentBrowserClient::NavigationRequestRedirected(
}
std::unique_ptr<net::HttpRequestHeaders> client_hints_extra_headers =
client_hints::GetAdditionalNavigationRequestClientHintsHeaders(
browser_context, url);
ClientHintsFactory::GetForBrowserContext(browser_context)
->GetAdditionalNavigationRequestClientHintsHeaders(url);
if (client_hints_extra_headers) {
if (!modified_request_headers->has_value())
*modified_request_headers = net::HttpRequestHeaders();
......
......@@ -210,10 +210,14 @@ double RoundKbpsToMbps(const std::string& host,
} // namespace internal
ClientHints::ClientHints(content::BrowserContext* context)
: context_(context) {}
ClientHints::~ClientHints() = default;
std::unique_ptr<net::HttpRequestHeaders>
GetAdditionalNavigationRequestClientHintsHeaders(
content::BrowserContext* context,
const GURL& url) {
ClientHints::GetAdditionalNavigationRequestClientHintsHeaders(
const GURL& url) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK_EQ(blink::kWebEffectiveConnectionTypeMappingCount,
net::EFFECTIVE_CONNECTION_TYPE_4G + 1u);
......@@ -233,7 +237,7 @@ GetAdditionalNavigationRequestClientHintsHeaders(
DCHECK(url.SchemeIs(url::kHttpsScheme) ||
(url.SchemeIs(url::kHttpScheme) && net::IsLocalhost(url)));
Profile* profile = Profile::FromBrowserContext(context);
Profile* profile = Profile::FromBrowserContext(context_);
if (!profile)
return nullptr;
......@@ -274,7 +278,7 @@ GetAdditionalNavigationRequestClientHintsHeaders(
if (web_client_hints.IsEnabled(blink::mojom::WebClientHintsType::kDpr)) {
double device_scale_factor = GetDeviceScaleFactor();
double zoom_factor = GetZoomFactor(context, url);
double zoom_factor = GetZoomFactor(context_, url);
additional_headers->SetHeader(
blink::kClientHintsHeaderMapping[static_cast<int>(
blink::mojom::WebClientHintsType::kDpr)],
......@@ -293,7 +297,7 @@ GetAdditionalNavigationRequestClientHintsHeaders(
->GetPrimaryDisplay()
.GetSizeInPixel()
.width()) /
GetZoomFactor(context, url) / device_scale_factor;
GetZoomFactor(context_, url) / device_scale_factor;
#endif // !OS_ANDROID
DCHECK_LT(0, viewport_width);
if (viewport_width > 0) {
......
......@@ -9,6 +9,7 @@
#include "base/memory/ref_counted.h"
#include "base/optional.h"
#include "components/keyed_service/core/keyed_service.h"
class GURL;
......@@ -37,12 +38,21 @@ double RoundKbpsToMbps(const std::string& host,
} // namespace internal
// Allow the embedder to return additional headers related to client hints that
// should be sent when fetching |url|. May return a nullptr.
std::unique_ptr<net::HttpRequestHeaders>
GetAdditionalNavigationRequestClientHintsHeaders(
content::BrowserContext* context,
const GURL& url);
class ClientHints : public KeyedService {
public:
explicit ClientHints(content::BrowserContext* context);
~ClientHints() override;
// Allow the embedder to return additional headers related to client hints
// that should be sent when fetching |url|. May return a nullptr.
std::unique_ptr<net::HttpRequestHeaders>
GetAdditionalNavigationRequestClientHintsHeaders(const GURL& url) const;
private:
content::BrowserContext* context_;
DISALLOW_COPY_AND_ASSIGN(ClientHints);
};
} // namespace client_hints
......
// 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 "chrome/browser/client_hints/client_hints_factory.h"
#include "chrome/browser/client_hints/client_hints.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_context.h"
namespace {
base::LazyInstance<ClientHintsFactory>::DestructorAtExit
g_previews_service_factory = LAZY_INSTANCE_INITIALIZER;
} // namespace
// static
client_hints::ClientHints* ClientHintsFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<client_hints::ClientHints*>(
GetInstance()->GetServiceForContext(context, true));
}
// static
ClientHintsFactory* ClientHintsFactory::GetInstance() {
return g_previews_service_factory.Pointer();
}
ClientHintsFactory::ClientHintsFactory()
: BrowserContextKeyedServiceFactory(
"ClientHints",
BrowserContextDependencyManager::GetInstance()) {}
ClientHintsFactory::~ClientHintsFactory() {}
KeyedService* ClientHintsFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new client_hints::ClientHints(context);
}
content::BrowserContext* ClientHintsFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextOwnInstanceInIncognito(context);
}
// 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_CLIENT_HINTS_CLIENT_HINTS_FACTORY_H_
#define CHROME_BROWSER_CLIENT_HINTS_CLIENT_HINTS_FACTORY_H_
#include "base/lazy_instance.h"
#include "base/macros.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
namespace content {
class BrowserContext;
}
namespace client_hints {
class ClientHints;
}
class ClientHintsFactory : public BrowserContextKeyedServiceFactory {
public:
static client_hints::ClientHints* GetForBrowserContext(
content::BrowserContext* context);
static ClientHintsFactory* GetInstance();
private:
friend struct base::LazyInstanceTraitsBase<ClientHintsFactory>;
ClientHintsFactory();
~ClientHintsFactory() override;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
DISALLOW_COPY_AND_ASSIGN(ClientHintsFactory);
};
#endif // CHROME_BROWSER_CLIENT_HINTS_CLIENT_HINTS_FACTORY_H_
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