forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaw_cookie_access_policy.cc
166 lines (143 loc) · 6.27 KB
/
aw_cookie_access_policy.cc
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
// Copyright (c) 2012 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 "android_webview/browser/aw_cookie_access_policy.h"
#include <memory>
#include "android_webview/browser/aw_contents_io_thread_client.h"
#include "base/logging.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/websocket_handshake_request_info.h"
#include "net/base/net_errors.h"
using base::AutoLock;
using content::BrowserThread;
using content::ResourceRequestInfo;
using content::WebSocketHandshakeRequestInfo;
using net::StaticCookiePolicy;
namespace android_webview {
namespace {
base::LazyInstance<AwCookieAccessPolicy>::Leaky g_lazy_instance;
} // namespace
AwCookieAccessPolicy::~AwCookieAccessPolicy() {
}
AwCookieAccessPolicy::AwCookieAccessPolicy()
: accept_cookies_(true) {
}
AwCookieAccessPolicy* AwCookieAccessPolicy::GetInstance() {
return g_lazy_instance.Pointer();
}
bool AwCookieAccessPolicy::GetShouldAcceptCookies() {
AutoLock lock(lock_);
return accept_cookies_;
}
void AwCookieAccessPolicy::SetShouldAcceptCookies(bool allow) {
AutoLock lock(lock_);
accept_cookies_ = allow;
}
bool AwCookieAccessPolicy::GetShouldAcceptThirdPartyCookies(
int render_process_id,
int render_frame_id,
int frame_tree_node_id) {
std::unique_ptr<AwContentsIoThreadClient> io_thread_client =
(frame_tree_node_id != content::RenderFrameHost::kNoFrameTreeNodeId)
? AwContentsIoThreadClient::FromID(frame_tree_node_id)
: AwContentsIoThreadClient::FromID(render_process_id,
render_frame_id);
if (!io_thread_client) {
return false;
}
return io_thread_client->ShouldAcceptThirdPartyCookies();
}
bool AwCookieAccessPolicy::GetShouldAcceptThirdPartyCookies(
const net::URLRequest& request) {
int child_id = 0;
int render_frame_id = 0;
int frame_tree_node_id = content::RenderFrameHost::kNoFrameTreeNodeId;
const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
if (info) {
child_id = info->GetChildID();
render_frame_id = info->GetRenderFrameID();
frame_tree_node_id = info->GetFrameTreeNodeId();
} else {
const WebSocketHandshakeRequestInfo* websocket_info =
WebSocketHandshakeRequestInfo::ForRequest(&request);
if (!websocket_info)
return false;
child_id = websocket_info->GetChildId();
render_frame_id = websocket_info->GetRenderFrameId();
}
return GetShouldAcceptThirdPartyCookies(child_id, render_frame_id,
frame_tree_node_id);
}
bool AwCookieAccessPolicy::OnCanGetCookies(const net::URLRequest& request,
const net::CookieList& cookie_list) {
bool global = GetShouldAcceptCookies();
bool thirdParty = GetShouldAcceptThirdPartyCookies(request);
return AwStaticCookiePolicy(global, thirdParty)
.AllowGet(request.url(), request.site_for_cookies());
}
bool AwCookieAccessPolicy::OnCanSetCookie(const net::URLRequest& request,
const net::CanonicalCookie& cookie,
net::CookieOptions* options) {
bool global = GetShouldAcceptCookies();
bool thirdParty = GetShouldAcceptThirdPartyCookies(request);
return AwStaticCookiePolicy(global, thirdParty)
.AllowSet(request.url(), request.site_for_cookies());
}
bool AwCookieAccessPolicy::AllowGetCookie(const GURL& url,
const GURL& first_party,
const net::CookieList& cookie_list,
content::ResourceContext* context,
int render_process_id,
int render_frame_id) {
bool global = GetShouldAcceptCookies();
bool thirdParty = GetShouldAcceptThirdPartyCookies(
render_process_id, render_frame_id,
content::RenderFrameHost::kNoFrameTreeNodeId);
return AwStaticCookiePolicy(global, thirdParty).AllowGet(url, first_party);
}
bool AwCookieAccessPolicy::AllowSetCookie(const GURL& url,
const GURL& first_party,
const net::CanonicalCookie& cookie,
content::ResourceContext* context,
int render_process_id,
int render_frame_id,
const net::CookieOptions& options) {
bool global = GetShouldAcceptCookies();
bool thirdParty = GetShouldAcceptThirdPartyCookies(
render_process_id, render_frame_id,
content::RenderFrameHost::kNoFrameTreeNodeId);
return AwStaticCookiePolicy(global, thirdParty).AllowSet(url, first_party);
}
AwStaticCookiePolicy::AwStaticCookiePolicy(bool accept_cookies,
bool accept_third_party_cookies)
: accept_cookies_(accept_cookies),
accept_third_party_cookies_(accept_third_party_cookies) {
}
StaticCookiePolicy::Type AwStaticCookiePolicy::GetPolicy(const GURL& url)
const {
// File URLs are a special case. We want file URLs to be able to set cookies
// but (for the purpose of cookies) Chrome considers different file URLs to
// come from different origins so we use the 'allow all' cookie policy for
// file URLs.
bool isFile = url.SchemeIsFile();
if (!accept_cookies()) {
return StaticCookiePolicy::BLOCK_ALL_COOKIES;
}
if (accept_third_party_cookies() || isFile) {
return StaticCookiePolicy::ALLOW_ALL_COOKIES;
}
return StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES;
}
bool AwStaticCookiePolicy::AllowSet(const GURL& url,
const GURL& first_party) const {
return StaticCookiePolicy(GetPolicy(url))
.CanAccessCookies(url, first_party) == net::OK;
}
bool AwStaticCookiePolicy::AllowGet(const GURL& url,
const GURL& first_party) const {
return StaticCookiePolicy(GetPolicy(url))
.CanAccessCookies(url, first_party) == net::OK;
}
} // namespace android_webview