1 // Copyright (c) 2012 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.
5 #include "android_webview/browser/aw_cookie_access_policy.h"
7 #include "android_webview/browser/aw_contents_io_thread_client.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/resource_request_info.h"
13 #include "net/base/net_errors.h"
16 using content::BrowserThread
;
17 using content::ResourceRequestInfo
;
18 using net::StaticCookiePolicy
;
20 namespace android_webview
{
23 base::LazyInstance
<AwCookieAccessPolicy
>::Leaky g_lazy_instance
;
26 AwCookieAccessPolicy::~AwCookieAccessPolicy() {
29 AwCookieAccessPolicy::AwCookieAccessPolicy()
30 : accept_cookies_(true) {
33 AwCookieAccessPolicy
* AwCookieAccessPolicy::GetInstance() {
34 return g_lazy_instance
.Pointer();
37 bool AwCookieAccessPolicy::GetShouldAcceptCookies() {
39 return accept_cookies_
;
42 void AwCookieAccessPolicy::SetShouldAcceptCookies(bool allow
) {
44 accept_cookies_
= allow
;
47 bool AwCookieAccessPolicy::GetShouldAcceptThirdPartyCookies(
48 int render_process_id
,
49 int render_frame_id
) {
50 scoped_ptr
<AwContentsIoThreadClient
> io_thread_client
=
51 AwContentsIoThreadClient::FromID(render_process_id
, render_frame_id
);
52 if (!io_thread_client
) {
55 return io_thread_client
->ShouldAcceptThirdPartyCookies();
58 bool AwCookieAccessPolicy::GetShouldAcceptThirdPartyCookies(
59 const net::URLRequest
& request
) {
60 const ResourceRequestInfo
* info
= ResourceRequestInfo::ForRequest(&request
);
64 return GetShouldAcceptThirdPartyCookies(info
->GetChildID(),
65 info
->GetRenderFrameID());
68 bool AwCookieAccessPolicy::OnCanGetCookies(const net::URLRequest
& request
,
69 const net::CookieList
& cookie_list
) {
70 bool global
= GetShouldAcceptCookies();
71 bool thirdParty
= GetShouldAcceptThirdPartyCookies(request
);
72 return AwStaticCookiePolicy(global
, thirdParty
)
73 .AllowGet(request
.url(), request
.first_party_for_cookies());
76 bool AwCookieAccessPolicy::OnCanSetCookie(const net::URLRequest
& request
,
77 const std::string
& cookie_line
,
78 net::CookieOptions
* options
) {
79 bool global
= GetShouldAcceptCookies();
80 bool thirdParty
= GetShouldAcceptThirdPartyCookies(request
);
81 return AwStaticCookiePolicy(global
, thirdParty
)
82 .AllowSet(request
.url(), request
.first_party_for_cookies());
85 bool AwCookieAccessPolicy::AllowGetCookie(const GURL
& url
,
86 const GURL
& first_party
,
87 const net::CookieList
& cookie_list
,
88 content::ResourceContext
* context
,
89 int render_process_id
,
90 int render_frame_id
) {
91 bool global
= GetShouldAcceptCookies();
93 GetShouldAcceptThirdPartyCookies(render_process_id
, render_frame_id
);
94 return AwStaticCookiePolicy(global
, thirdParty
).AllowGet(url
, first_party
);
97 bool AwCookieAccessPolicy::AllowSetCookie(const GURL
& url
,
98 const GURL
& first_party
,
99 const std::string
& cookie_line
,
100 content::ResourceContext
* context
,
101 int render_process_id
,
103 net::CookieOptions
* options
) {
104 bool global
= GetShouldAcceptCookies();
106 GetShouldAcceptThirdPartyCookies(render_process_id
, render_frame_id
);
107 return AwStaticCookiePolicy(global
, thirdParty
).AllowSet(url
, first_party
);
110 AwStaticCookiePolicy::AwStaticCookiePolicy(bool accept_cookies
,
111 bool accept_third_party_cookies
)
112 : accept_cookies_(accept_cookies
),
113 accept_third_party_cookies_(accept_third_party_cookies
) {
116 StaticCookiePolicy::Type
AwStaticCookiePolicy::GetPolicy() const {
117 if (!accept_cookies()) {
118 return StaticCookiePolicy::BLOCK_ALL_COOKIES
;
119 } else if (!accept_third_party_cookies()) {
120 return StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES
;
122 return StaticCookiePolicy::ALLOW_ALL_COOKIES
;
125 bool AwStaticCookiePolicy::AllowSet(const GURL
& url
,
126 const GURL
& first_party
) const {
127 return StaticCookiePolicy(GetPolicy()).CanSetCookie(url
, first_party
) ==
131 bool AwStaticCookiePolicy::AllowGet(const GURL
& url
,
132 const GURL
& first_party
) const {
133 return StaticCookiePolicy(GetPolicy()).CanGetCookies(url
, first_party
) ==
137 } // namespace android_webview