summaryrefslogtreecommitdiffstats
path: root/src/core/client_hints.cpp
blob: 9fa7531da18b3d2074a4b7a55ae75d6c74bd79b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "client_hints.h"

#include "profile_qt.h"
#include "web_contents_delegate_qt.h"
#include "web_engine_settings.h"

#include "components/embedder_support/user_agent_utils.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/network_service_instance.h"
#include "extensions/buildflags/buildflags.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "services/network/public/cpp/network_quality_tracker.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "components/guest_view/browser/guest_view_base.h"
#endif

namespace QtWebEngineCore {

// based on weblayer/browser/client_hints_factory.cc:
// Copyright 2020 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.

// static
ClientHints *ClientHintsFactory::GetForBrowserContext(content::BrowserContext *browser_context)
{
    return static_cast<ClientHints*>(GetInstance()->GetServiceForBrowserContext(browser_context, true));
}

// static
ClientHintsFactory *ClientHintsFactory::GetInstance()
{
    static base::NoDestructor<ClientHintsFactory> factory;
    return factory.get();
}

ClientHintsFactory::ClientHintsFactory()
        : BrowserContextKeyedServiceFactory("ClientHints", BrowserContextDependencyManager::GetInstance())
{
}

ClientHintsFactory::~ClientHintsFactory() = default;

KeyedService *ClientHintsFactory::BuildServiceInstanceFor(content::BrowserContext *context) const
{
    return new ClientHints(context);
}

content::BrowserContext *ClientHintsFactory::GetBrowserContextToUse(content::BrowserContext *context) const
{
    return context;
}

// based on components/client_hints/browser/in_memory_client_hints_controller_delegate.cc:
// Copyright 2022 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.

ClientHints::ClientHints(content::BrowserContext *context)
    : context_(context)
{
}

ClientHints::~ClientHints() = default;

// Enabled Client Hints are only cached and not persisted in this
// implementation.
void ClientHints::PersistClientHints(const url::Origin &primary_origin,
                                     content::RenderFrameHost *parent_rfh,
                                     const std::vector<network::mojom::WebClientHintsType> &client_hints)
{
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    const GURL primary_url = primary_origin.GetURL();
    DCHECK(primary_url.is_valid());
    if (!network::IsUrlPotentiallyTrustworthy(primary_url))
        return;

    // Client hints should only be enabled when JavaScript is enabled.
    if (!IsJavaScriptAllowed(primary_url, parent_rfh))
        return;

    blink::EnabledClientHints enabled_hints;
    ProfileAdapter *profileAdapter = static_cast<ProfileQt *>(context_)->profileAdapter();
    for (auto hint : client_hints) {
        enabled_hints.SetIsEnabled(hint, profileAdapter->clientHintsEnabled());
    }
    accept_ch_cache_[primary_origin] = enabled_hints;
}

// Looks up enabled Client Hints for the URL origin, and adds additional Client
// Hints if set.
void ClientHints::GetAllowedClientHintsFromSource(const url::Origin &origin,
                                                  blink::EnabledClientHints *client_hints)
{
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    DCHECK(client_hints);
    // Can not assert this, as we get here for unregistered custom schemes:
    if (!network::IsOriginPotentiallyTrustworthy(origin))
        return;

    const auto &it = accept_ch_cache_.find(origin);
    if (it != accept_ch_cache_.end()) {
        *client_hints = it->second;
    }

    ProfileAdapter *profileAdapter = static_cast<ProfileQt *>(context_)->profileAdapter();
    for (auto hint : additional_hints_)
        client_hints->SetIsEnabled(hint, profileAdapter->clientHintsEnabled());
}

void ClientHints::SetAdditionalClientHints(const std::vector<network::mojom::WebClientHintsType> &hints)
{
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    additional_hints_ = hints;
}

void ClientHints::ClearAdditionalClientHints()
{
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    additional_hints_.clear();
}

network::NetworkQualityTracker *ClientHints::GetNetworkQualityTracker()
{
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    if (!network_quality_tracker_) {
        network_quality_tracker_ =
                std::make_unique<network::NetworkQualityTracker>(
                    base::BindRepeating(&content::GetNetworkService));
    }
    return network_quality_tracker_.get();
}

bool ClientHints::IsJavaScriptAllowed(const GURL &url, content::RenderFrameHost *parent_rfh)
{
    content::WebContents *webContents = content::WebContents::FromRenderFrameHost(parent_rfh);

#if BUILDFLAG(ENABLE_EXTENSIONS)
    if (webContents && guest_view::GuestViewBase::IsGuest(webContents))
        webContents = guest_view::GuestViewBase::GetTopLevelWebContents(webContents);
#endif

    if (webContents) {
        WebContentsDelegateQt* delegate =
                static_cast<WebContentsDelegateQt*>(webContents->GetDelegate());
        if (delegate) {
            WebEngineSettings *settings = delegate->webEngineSettings();
            if (settings)
                return settings->testAttribute(QWebEngineSettings::JavascriptEnabled);
        }
    }
    return true;
}

bool ClientHints::AreThirdPartyCookiesBlocked(const GURL &url, content::RenderFrameHost *rfh)
{
    return false; // we probably can not report anything more specific
}

blink::UserAgentMetadata ClientHints::GetUserAgentMetadata()
{
    return static_cast<ProfileQt *>(context_)->userAgentMetadata();
}

void ClientHints::SetMostRecentMainFrameViewportSize(
    const gfx::Size& viewport_size) {
  viewport_size_ = viewport_size;
}

gfx::Size
ClientHints::GetMostRecentMainFrameViewportSize() {
  return viewport_size_;
}
} // namespace