aw_cookie_access_policy.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.
  4. #include "android_webview/browser/aw_cookie_access_policy.h"
  5. #include <memory>
  6. #include "android_webview/browser/aw_contents_io_thread_client.h"
  7. #include "base/check_op.h"
  8. #include "base/no_destructor.h"
  9. #include "content/public/browser/browser_thread.h"
  10. #include "content/public/browser/global_routing_id.h"
  11. #include "content/public/browser/render_frame_host.h"
  12. #include "content/public/browser/websocket_handshake_request_info.h"
  13. #include "net/base/net_errors.h"
  14. #include "net/cookies/site_for_cookies.h"
  15. #include "net/cookies/static_cookie_policy.h"
  16. #include "url/gurl.h"
  17. using base::AutoLock;
  18. using content::BrowserThread;
  19. using content::WebSocketHandshakeRequestInfo;
  20. namespace android_webview {
  21. AwCookieAccessPolicy::~AwCookieAccessPolicy() = default;
  22. AwCookieAccessPolicy::AwCookieAccessPolicy()
  23. : accept_cookies_(true) {
  24. }
  25. AwCookieAccessPolicy* AwCookieAccessPolicy::GetInstance() {
  26. static base::NoDestructor<AwCookieAccessPolicy> instance;
  27. return instance.get();
  28. }
  29. bool AwCookieAccessPolicy::GetShouldAcceptCookies() {
  30. AutoLock lock(lock_);
  31. return accept_cookies_;
  32. }
  33. void AwCookieAccessPolicy::SetShouldAcceptCookies(bool allow) {
  34. AutoLock lock(lock_);
  35. accept_cookies_ = allow;
  36. }
  37. bool AwCookieAccessPolicy::GetShouldAcceptThirdPartyCookies(
  38. int render_process_id,
  39. int render_frame_id,
  40. int frame_tree_node_id) {
  41. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  42. const content::GlobalRenderFrameHostId rfh_id(render_process_id,
  43. render_frame_id);
  44. std::unique_ptr<AwContentsIoThreadClient> io_thread_client =
  45. (frame_tree_node_id != content::RenderFrameHost::kNoFrameTreeNodeId)
  46. ? AwContentsIoThreadClient::FromID(frame_tree_node_id)
  47. : AwContentsIoThreadClient::FromID(rfh_id);
  48. if (!io_thread_client) {
  49. return false;
  50. }
  51. return io_thread_client->ShouldAcceptThirdPartyCookies();
  52. }
  53. bool AwCookieAccessPolicy::AllowCookies(
  54. const GURL& url,
  55. const net::SiteForCookies& site_for_cookies,
  56. int render_process_id,
  57. int render_frame_id) {
  58. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  59. bool third_party = GetShouldAcceptThirdPartyCookies(
  60. render_process_id, render_frame_id,
  61. content::RenderFrameHost::kNoFrameTreeNodeId);
  62. return CanAccessCookies(url, site_for_cookies, third_party);
  63. }
  64. bool AwCookieAccessPolicy::CanAccessCookies(
  65. const GURL& url,
  66. const net::SiteForCookies& site_for_cookies,
  67. bool accept_third_party_cookies) {
  68. if (!accept_cookies_)
  69. return false;
  70. if (accept_third_party_cookies)
  71. return true;
  72. // File URLs are a special case. We want file URLs to be able to set cookies
  73. // but (for the purpose of cookies) Chrome considers different file URLs to
  74. // come from different origins so we use the 'allow all' cookie policy for
  75. // file URLs.
  76. if (url.SchemeIsFile())
  77. return true;
  78. // Otherwise, block third-party cookies.
  79. return net::StaticCookiePolicy(
  80. net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES)
  81. .CanAccessCookies(url, site_for_cookies) == net::OK;
  82. }
  83. } // namespace android_webview