[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / ssl / ssl_error_handler.h
blob26fc147c3f4e8d465661b152d4674dfef0d4f39b
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 #ifndef CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_
6 #define CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_
8 #include <string>
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"
18 namespace net {
19 class SSLInfo;
20 class URLRequest;
21 } // namespace net
23 namespace content {
25 class ResourceDispatcherHostImpl;
26 class SSLCertErrorHandler;
27 class SSLManager;
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.
37 // IMPORTANT NOTE:
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.
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;
54 // Called when SSLErrorHandler decides to continue the request despite the
55 // SSL error.
56 virtual void ContinueSSLRequest() = 0;
58 protected:
59 virtual ~Delegate() {}
62 virtual SSLCertErrorHandler* AsSSLCertErrorHandler();
64 // Find the appropriate SSLManager for the net::URLRequest and begin handling
65 // this error.
67 // Call on UI thread.
68 void Dispatch();
70 // Available on either thread.
71 const GURL& request_url() const { return request_url_; }
73 // Available on either thread.
74 ResourceType resource_type() const { return resource_type_; }
76 // Cancels the associated net::URLRequest.
77 // This method can be called from OnDispatchFailed and OnDispatched.
78 CONTENT_EXPORT void CancelRequest();
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();
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();
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();
99 int render_process_id() const { return render_process_id_; }
100 int render_frame_id() const { return render_frame_id_; }
102 protected:
103 friend class base::RefCountedThreadSafe<SSLErrorHandler>;
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);
112 virtual ~SSLErrorHandler();
114 // The following 2 methods are the methods subclasses should implement.
115 virtual void OnDispatchFailed();
117 // Can use the manager_ member.
118 virtual void OnDispatched();
120 // Should only be accessed on the UI thread.
121 SSLManager* manager_; // Our manager.
123 // The delegate we are associated with.
124 base::WeakPtr<Delegate> delegate_;
126 private:
127 // Completes the CancelRequest operation on the IO thread.
128 // Call on the IO thread.
129 void CompleteCancelRequest(int error);
131 // Completes the ContinueRequest operation on the IO thread.
133 // Call on the IO thread.
134 void CompleteContinueRequest();
136 // Derefs this instance.
137 // Call on the IO thread.
138 void CompleteTakeNoAction();
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_;
145 // The URL that we requested.
146 // This read-only member can be accessed on any thread.
147 const GURL request_url_;
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_;
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_;
158 DISALLOW_COPY_AND_ASSIGN(SSLErrorHandler);
161 } // namespace content
163 #endif // CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_