1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ANDROID_WEBVIEW_BROWSER_NETWORK_SERVICE_AW_PROXYING_RESTRICTED_COOKIE_MANAGER_H_
6 #define ANDROID_WEBVIEW_BROWSER_NETWORK_SERVICE_AW_PROXYING_RESTRICTED_COOKIE_MANAGER_H_
7 
8 #include <string>
9 
10 #include "base/macros.h"
11 #include "base/memory/weak_ptr.h"
12 #include "mojo/public/cpp/bindings/remote.h"
13 #include "services/network/public/mojom/restricted_cookie_manager.mojom.h"
14 
15 class GURL;
16 
17 namespace android_webview {
18 
19 // A RestrictedCookieManager which conditionally proxies to an underlying
20 // RestrictedCookieManager, first consulting WebView's cookie settings.
21 class AwProxyingRestrictedCookieManager
22     : public network::mojom::RestrictedCookieManager {
23  public:
24   // Creates a AwProxyingRestrictedCookieManager that lives on IO thread,
25   // binding it to handle communications from |receiver|. The requests will be
26   // delegated to |underlying_rcm|. The resulting object will be owned by the
27   // pipe corresponding to |request| and will in turn own |underlying_rcm|.
28   //
29   // Expects to be called on the UI thread.
30   static void CreateAndBind(
31       mojo::PendingRemote<network::mojom::RestrictedCookieManager>
32           underlying_rcm,
33       bool is_service_worker,
34       int process_id,
35       int frame_id,
36       mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver);
37 
38   ~AwProxyingRestrictedCookieManager() override;
39 
40   // network::mojom::RestrictedCookieManager interface:
41   void GetAllForUrl(const GURL& url,
42                     const net::SiteForCookies& site_for_cookies,
43                     const url::Origin& top_frame_origin,
44                     network::mojom::CookieManagerGetOptionsPtr options,
45                     GetAllForUrlCallback callback) override;
46   void SetCanonicalCookie(const net::CanonicalCookie& cookie,
47                           const GURL& url,
48                           const net::SiteForCookies& site_for_cookies,
49                           const url::Origin& top_frame_origin,
50                           SetCanonicalCookieCallback callback) override;
51   void AddChangeListener(
52       const GURL& url,
53       const net::SiteForCookies& site_for_cookies,
54       const url::Origin& top_frame_origin,
55       mojo::PendingRemote<network::mojom::CookieChangeListener> listener,
56       AddChangeListenerCallback callback) override;
57 
58   void SetCookieFromString(const GURL& url,
59                            const net::SiteForCookies& site_for_cookies,
60                            const url::Origin& top_frame_origin,
61                            const std::string& cookie,
62                            SetCookieFromStringCallback callback) override;
63 
64   void GetCookiesString(const GURL& url,
65                         const net::SiteForCookies& site_for_cookies,
66                         const url::Origin& top_frame_origin,
67                         GetCookiesStringCallback callback) override;
68 
69   void CookiesEnabledFor(const GURL& url,
70                          const net::SiteForCookies& site_for_cookies,
71                          const url::Origin& top_frame_origin,
72                          CookiesEnabledForCallback callback) override;
73 
74   // This one is internal.
75   bool AllowCookies(const GURL& url,
76                     const net::SiteForCookies& site_for_cookies) const;
77 
78  private:
79   AwProxyingRestrictedCookieManager(
80       mojo::PendingRemote<network::mojom::RestrictedCookieManager>
81           underlying_restricted_cookie_manager,
82       bool is_service_worker,
83       int process_id,
84       int frame_id);
85 
86   static void CreateAndBindOnIoThread(
87       mojo::PendingRemote<network::mojom::RestrictedCookieManager>
88           underlying_rcm,
89       bool is_service_worker,
90       int process_id,
91       int frame_id,
92       mojo::PendingReceiver<network::mojom::RestrictedCookieManager> receiver);
93 
94   mojo::Remote<network::mojom::RestrictedCookieManager>
95       underlying_restricted_cookie_manager_;
96   bool is_service_worker_;
97   int process_id_;
98   int frame_id_;
99 
100   base::WeakPtrFactory<AwProxyingRestrictedCookieManager> weak_factory_{this};
101 
102   DISALLOW_COPY_AND_ASSIGN(AwProxyingRestrictedCookieManager);
103 };
104 
105 }  // namespace android_webview
106 
107 #endif  // ANDROID_WEBVIEW_BROWSER_NETWORK_SERVICE_AW_PROXYING_RESTRICTED_COOKIE_MANAGER_H_
108