summaryrefslogtreecommitdiffstats
path: root/src/core/net/proxying_restricted_cookie_manager_qt.cpp
blob: d4d5cc4ab3cc663cc294a7e87c63ebc25006aebf (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
// Copyright (C) 2019 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

// originally based on android_webview/browser/network_service/aw_proxying_restricted_cookie_manager.cc:
// 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 "proxying_restricted_cookie_manager_qt.h"

#include "api/qwebenginecookiestore.h"
#include "api/qwebenginecookiestore_p.h"
#include "profile_io_data_qt.h"
#include "type_conversion.h"

#include "base/memory/ptr_util.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"

namespace QtWebEngineCore {

// static
void ProxyingRestrictedCookieManagerQt::CreateAndBind(ProfileIODataQt *profileIoData,
                                                      mojo::PendingRemote<network::mojom::RestrictedCookieManager> underlying_rcm,
                                                      mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

    content::GetIOThreadTaskRunner({})->PostTask(FROM_HERE,
                   base::BindOnce(&ProxyingRestrictedCookieManagerQt::CreateAndBindOnIoThread,
                                  profileIoData,
                                  std::move(underlying_rcm),
                                  std::move(receiver)));
}


// static
void ProxyingRestrictedCookieManagerQt::CreateAndBindOnIoThread(ProfileIODataQt *profileIoData,
                                                                mojo::PendingRemote<network::mojom::RestrictedCookieManager> underlying_rcm,
                                                                mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

    auto wrapper = base::WrapUnique(new ProxyingRestrictedCookieManagerQt(
                                        profileIoData->getWeakPtrOnIOThread(),
                                        std::move(underlying_rcm)));
    mojo::MakeSelfOwnedReceiver(std::move(wrapper), std::move(receiver));
}

ProxyingRestrictedCookieManagerQt::ProxyingRestrictedCookieManagerQt(
        base::WeakPtr<ProfileIODataQt> profileIoData,
        mojo::PendingRemote<network::mojom::RestrictedCookieManager> underlyingRestrictedCookieManager)
        : m_profileIoData(std::move(profileIoData))
        , underlying_restricted_cookie_manager_(std::move(underlyingRestrictedCookieManager))
        , weak_factory_(this)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
}

ProxyingRestrictedCookieManagerQt::~ProxyingRestrictedCookieManagerQt()
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
}

void ProxyingRestrictedCookieManagerQt::GetAllForUrl(const GURL &url,
                                                     const net::SiteForCookies &site_for_cookies,
                                                     const url::Origin &top_frame_origin, bool has_storage_access,
                                                     network::mojom::CookieManagerGetOptionsPtr options,
                                                     GetAllForUrlCallback callback)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

    if (allowCookies(url, site_for_cookies)) {
        underlying_restricted_cookie_manager_->GetAllForUrl(url, site_for_cookies, top_frame_origin, has_storage_access,
                                                            std::move(options), std::move(callback));
    } else {
        std::move(callback).Run(std::vector<net::CookieWithAccessResult>());
    }
}

void ProxyingRestrictedCookieManagerQt::SetCanonicalCookie(const net::CanonicalCookie &cookie,
                                                           const GURL &url,
                                                           const net::SiteForCookies &site_for_cookies,
                                                           const url::Origin &top_frame_origin,
                                                           bool has_storage_access,
                                                           net::CookieInclusionStatus status,
                                                           SetCanonicalCookieCallback callback)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

    if (allowCookies(url, site_for_cookies)) {
        underlying_restricted_cookie_manager_->SetCanonicalCookie(cookie, url, site_for_cookies, top_frame_origin,
                                                                  has_storage_access, status, std::move(callback));
    } else {
        std::move(callback).Run(false);
    }
}

void ProxyingRestrictedCookieManagerQt::AddChangeListener(const GURL &url,
                                                          const net::SiteForCookies &site_for_cookies,
                                                          const url::Origin &top_frame_origin,
                                                          bool has_storage_access,
                                                          mojo::PendingRemote<network::mojom::CookieChangeListener> listener,
                                                          AddChangeListenerCallback callback)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
    underlying_restricted_cookie_manager_->AddChangeListener(url, site_for_cookies, top_frame_origin, has_storage_access,
                                                             std::move(listener), std::move(callback));
}

void ProxyingRestrictedCookieManagerQt::SetCookieFromString(const GURL &url,
                                                            const net::SiteForCookies &site_for_cookies,
                                                            const url::Origin &top_frame_origin, bool has_storage_access,
                                                            const std::string &cookie,
                                                            SetCookieFromStringCallback callback)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

    if (allowCookies(url, site_for_cookies)) {
        underlying_restricted_cookie_manager_->SetCookieFromString(url, site_for_cookies, top_frame_origin, has_storage_access,
                                                                   cookie, std::move(callback));
    } else {
        std::move(callback).Run(false, false); // FIXME: is true, true in aw_proxying_restricted_cookie_manager.cc though..
    }
}

void ProxyingRestrictedCookieManagerQt::GetCookiesString(const GURL &url,
                                                         const net::SiteForCookies &site_for_cookies,
                                                         const url::Origin &top_frame_origin,
                                                         bool has_storage_access, bool get_version_shared_memory,
                                                         GetCookiesStringCallback callback)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

    if (allowCookies(url, site_for_cookies)) {
        underlying_restricted_cookie_manager_->GetCookiesString(url, site_for_cookies, top_frame_origin,
                                                                has_storage_access, get_version_shared_memory,
                                                                std::move(callback));
    } else {
        std::move(callback).Run(network::mojom::kInvalidCookieVersion, base::ReadOnlySharedMemoryRegion(), "");
    }
}

void ProxyingRestrictedCookieManagerQt::CookiesEnabledFor(const GURL &url,
                                                          const net::SiteForCookies &site_for_cookies,
                                                          const url::Origin & /*top_frame_origin*/,
                                                          bool /*has_storage_access*/,
                                                          CookiesEnabledForCallback callback)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
    std::move(callback).Run(allowCookies(url, site_for_cookies));
}

bool ProxyingRestrictedCookieManagerQt::allowCookies(const GURL &url, const net::SiteForCookies &site_for_cookies) const
{
    if (!m_profileIoData)
        return false;
    return m_profileIoData->canGetCookies(toQt(site_for_cookies.first_party_url()), toQt(url));
}

}  // namespace QtWebEngineCore