Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / android_webview / browser / aw_cookie_access_policy.h
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
5 #ifndef ANDROID_WEBVIEW_BROWSER_AW_COOKIE_ACCESS_POLICY_H_
6 #define ANDROID_WEBVIEW_BROWSER_AW_COOKIE_ACCESS_POLICY_H_
7
8 #include "base/basictypes.h"
9 #include "base/lazy_instance.h"
10 #include "base/synchronization/lock.h"
11 #include "net/base/static_cookie_policy.h"
12 #include "net/cookies/canonical_cookie.h"
13 #include "net/url_request/url_request.h"
14
15 namespace content {
16 class ResourceContext;
17 }
18
19 namespace net {
20 class CookieOptions;
21 }
22
23 class GURL;
24
25 namespace android_webview {
26
27 // Manages the cookie access (both setting and getting) policy for WebView.
28 class AwCookieAccessPolicy {
29  public:
30   static AwCookieAccessPolicy* GetInstance();
31
32   // These manage the global access state shared across requests regardless of
33   // source (i.e. network or JavaScript).
34   bool GetGlobalAllowAccess();
35   void SetGlobalAllowAccess(bool allow);
36
37   // These allow more fine grained control over requests depending on whether
38   // the cookie is third party or not.
39   bool GetThirdPartyAllowAccess();
40   void SetThirdPartyAllowAccess(bool allow);
41
42   // These are the functions called when operating over cookies from the
43   // network. See NetworkDelegate for further descriptions.
44   bool OnCanGetCookies(const net::URLRequest& request,
45                        const net::CookieList& cookie_list);
46   bool OnCanSetCookie(const net::URLRequest& request,
47                       const std::string& cookie_line,
48                       net::CookieOptions* options);
49
50   // These are the functions called when operating over cookies from the
51   // renderer. See ContentBrowserClient for further descriptions.
52   bool AllowGetCookie(const GURL& url,
53                       const GURL& first_party,
54                       const net::CookieList& cookie_list,
55                       content::ResourceContext* context,
56                       int render_process_id,
57                       int render_frame_id);
58   bool AllowSetCookie(const GURL& url,
59                       const GURL& first_party,
60                       const std::string& cookie_line,
61                       content::ResourceContext* context,
62                       int render_process_id,
63                       int render_frame_id,
64                       net::CookieOptions* options);
65
66  private:
67   friend struct base::DefaultLazyInstanceTraits<AwCookieAccessPolicy>;
68
69   AwCookieAccessPolicy();
70   ~AwCookieAccessPolicy();
71   bool allow_access_;
72   bool allow_third_party_access_;
73   base::Lock lock_;
74
75   // We have two bits of state but only three different cases:
76   // If !GlobalAllowAccess then reject all cookies.
77   // If GlobalAllowAccess and !ThirdPartyAllowAccess then reject third party.
78   // If GlobalAllowAccess and ThirdPartyAllowAccess then allow all cookies.
79   net::StaticCookiePolicy::Type GetPolicy(void);
80
81   bool AllowGet(const GURL& url, const GURL& first_party);
82   bool AllowSet(const GURL& url, const GURL& first_party);
83
84   DISALLOW_COPY_AND_ASSIGN(AwCookieAccessPolicy);
85 };
86
87 }  // namespace android_webview
88
89 #endif  // ANDROID_WEBVIEW_BROWSER_AW_COOKIE_ACCESS_POLICY_H_