Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / ssl / ssl_error_handler.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 CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_
6 #define CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/common/content_export.h"
14 #include "content/public/browser/global_request_id.h"
15 #include "content/public/common/resource_type.h"
16 #include "url/gurl.h"
17
18 namespace net {
19 class SSLInfo;
20 class URLRequest;
21 }  // namespace net
22
23 namespace content {
24
25 class ResourceDispatcherHostImpl;
26 class SSLCertErrorHandler;
27 class SSLManager;
28
29 // An SSLErrorHandler carries information from the IO thread to the UI thread
30 // and is dispatched to the appropriate SSLManager when it arrives on the
31 // UI thread.  Subclasses should override the OnDispatched/OnDispatchFailed
32 // methods to implement the actions that should be taken on the UI thread.
33 // These methods can call the different convenience methods ContinueRequest/
34 // CancelRequest to perform any required action on the net::URLRequest the
35 // ErrorHandler was created with.
36 //
37 // IMPORTANT NOTE:
38 //
39 //   If you are not doing anything in OnDispatched/OnDispatchFailed, make sure
40 //   you call TakeNoAction().  This is necessary for ensuring the instance is
41 //   not leaked.
42 //
43 class SSLErrorHandler : public base::RefCountedThreadSafe<SSLErrorHandler> {
44  public:
45   // Delegate functions must be called from IO thread. Finally,
46   // CancelSSLRequest() or ContinueSSLRequest() will be called after
47   // SSLErrorHandler makes a decision on the SSL error.
48   class CONTENT_EXPORT Delegate {
49    public:
50     // Called when SSLErrorHandler decides to cancel the request because of
51     // the SSL error.
52     virtual void CancelSSLRequest(int error, const net::SSLInfo* ssl_info) = 0;
53
54     // Called when SSLErrorHandler decides to continue the request despite the
55     // SSL error.
56     virtual void ContinueSSLRequest() = 0;
57
58    protected:
59     virtual ~Delegate() {}
60   };
61
62   virtual SSLCertErrorHandler* AsSSLCertErrorHandler();
63
64   // Find the appropriate SSLManager for the net::URLRequest and begin handling
65   // this error.
66   //
67   // Call on UI thread.
68   void Dispatch();
69
70   // Available on either thread.
71   const GURL& request_url() const { return request_url_; }
72
73   // Available on either thread.
74   ResourceType resource_type() const { return resource_type_; }
75
76   // Cancels the associated net::URLRequest.
77   // This method can be called from OnDispatchFailed and OnDispatched.
78   CONTENT_EXPORT void CancelRequest();
79
80   // Continue the net::URLRequest ignoring any previous errors.  Note that some
81   // errors cannot be ignored, in which case this will result in the request
82   // being canceled.
83   // This method can be called from OnDispatchFailed and OnDispatched.
84   void ContinueRequest();
85
86   // Cancels the associated net::URLRequest and mark it as denied.  The renderer
87   // processes such request in a special manner, optionally replacing them
88   // with alternate content (typically frames content is replaced with a
89   // warning message).
90   // This method can be called from OnDispatchFailed and OnDispatched.
91   void DenyRequest();
92
93   // Does nothing on the net::URLRequest but ensures the current instance ref
94   // count is decremented appropriately.  Subclasses that do not want to
95   // take any specific actions in their OnDispatched/OnDispatchFailed should
96   // call this.
97   void TakeNoAction();
98
99   int render_process_id() const { return render_process_id_; }
100   int render_frame_id() const { return render_frame_id_; }
101
102  protected:
103   friend class base::RefCountedThreadSafe<SSLErrorHandler>;
104
105   // Construct on the IO thread.
106   SSLErrorHandler(const base::WeakPtr<Delegate>& delegate,
107                   ResourceType resource_type,
108                   const GURL& url,
109                   int render_process_id,
110                   int render_frame_id);
111
112   virtual ~SSLErrorHandler();
113
114   // The following 2 methods are the methods subclasses should implement.
115   virtual void OnDispatchFailed();
116
117   // Can use the manager_ member.
118   virtual void OnDispatched();
119
120   // Should only be accessed on the UI thread.
121   SSLManager* manager_;  // Our manager.
122
123   // The delegate we are associated with.
124   base::WeakPtr<Delegate> delegate_;
125
126  private:
127   // Completes the CancelRequest operation on the IO thread.
128   // Call on the IO thread.
129   void CompleteCancelRequest(int error);
130
131   // Completes the ContinueRequest operation on the IO thread.
132   //
133   // Call on the IO thread.
134   void CompleteContinueRequest();
135
136   // Derefs this instance.
137   // Call on the IO thread.
138   void CompleteTakeNoAction();
139
140   // We use these members to find the correct SSLManager when we arrive on
141   // the UI thread.
142   int render_process_id_;
143   int render_frame_id_;
144
145   // The URL that we requested.
146   // This read-only member can be accessed on any thread.
147   const GURL request_url_;
148
149   // What kind of resource is associated with the requested that generated
150   // that error.
151   // This read-only member can be accessed on any thread.
152   const ResourceType resource_type_;
153
154   // A flag to make sure we notify the net::URLRequest exactly once.
155   // Should only be accessed on the IO thread
156   bool request_has_been_notified_;
157
158   DISALLOW_COPY_AND_ASSIGN(SSLErrorHandler);
159 };
160
161 }  // namespace content
162
163 #endif  // CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_