| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_ | 5 #ifndef CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_ |
| 6 #define CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_ | 6 #define CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 14 #include "content/public/browser/global_request_id.h" | 14 #include "content/public/browser/global_request_id.h" |
| 15 #include "content/public/common/resource_type.h" | 15 #include "content/public/common/resource_type.h" |
| 16 #include "net/ssl/ssl_info.h" | 16 #include "net/ssl/ssl_info.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| 20 class URLRequest; | 20 class URLRequest; |
| 21 } // namespace net | 21 } // namespace net |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 | 24 |
| 25 class ResourceDispatcherHostImpl; | 25 class ResourceDispatcherHostImpl; |
| 26 class SSLManager; | 26 class SSLManager; |
| 27 class WebContents; | 27 class WebContents; |
| 28 | 28 |
| 29 // An SSLErrorHandler carries information from the IO thread to the UI thread | 29 // SSLErrorHandler is the UI-thread class for handling SSL certificate |
| 30 // and is dispatched to the appropriate SSLManager when it arrives on the | 30 // errors. Users of this class can call CancelRequest(), |
| 31 // UI thread. Subclasses should override the OnDispatched/OnDispatchFailed | 31 // ContinueRequest(), or DenyRequest() when a decision about how to |
| 32 // methods to implement the actions that should be taken on the UI thread. | 32 // handle the error has been made. Users of this class must |
| 33 // These methods can call the different convenience methods ContinueRequest/ | 33 // call exactly one of those methods exactly once. |
| 34 // CancelRequest to perform any required action on the net::URLRequest the | 34 class SSLErrorHandler { |
| 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: | 35 public: |
| 45 // Delegate functions must be called from IO thread. Finally, | 36 // SSLErrorHandler's delegate lives on the IO thread, and thus these |
| 46 // CancelSSLRequest() or ContinueSSLRequest() will be called after | 37 // delegate methods must be called on the IO thread only. |
| 47 // SSLErrorHandler makes a decision on the SSL error. | |
| 48 class CONTENT_EXPORT Delegate { | 38 class CONTENT_EXPORT Delegate { |
| 49 public: | 39 public: |
| 50 // Called when SSLErrorHandler decides to cancel the request because of | 40 // Called when SSLErrorHandler decides to cancel the request because of |
| 51 // the SSL error. | 41 // the SSL error. |
| 52 virtual void CancelSSLRequest(int error, const net::SSLInfo* ssl_info) = 0; | 42 virtual void CancelSSLRequest(int error, const net::SSLInfo* ssl_info) = 0; |
| 53 | 43 |
| 54 // Called when SSLErrorHandler decides to continue the request despite the | 44 // Called when SSLErrorHandler decides to continue the request despite the |
| 55 // SSL error. | 45 // SSL error. |
| 56 virtual void ContinueSSLRequest() = 0; | 46 virtual void ContinueSSLRequest() = 0; |
| 57 | 47 |
| 58 protected: | 48 protected: |
| 59 virtual ~Delegate() {} | 49 virtual ~Delegate() {} |
| 60 }; | 50 }; |
| 61 | 51 |
| 62 // Construct on the IO thread. | 52 SSLErrorHandler(WebContents* web_contents, |
| 63 SSLErrorHandler(const base::WeakPtr<Delegate>& delegate, | 53 const base::WeakPtr<Delegate>& delegate, |
| 64 ResourceType resource_type, | 54 ResourceType resource_type, |
| 65 const GURL& url, | 55 const GURL& url, |
| 66 const net::SSLInfo& ssl_info, | 56 const net::SSLInfo& ssl_info, |
| 67 bool fatal); | 57 bool fatal); |
| 68 | 58 |
| 69 // Find the appropriate SSLManager for the net::URLRequest and begin handling | 59 virtual ~SSLErrorHandler(); |
| 70 // this error. | |
| 71 // | |
| 72 // Call on UI thread. | |
| 73 void Dispatch(const base::Callback<WebContents*(void)>& web_contents_getter); | |
| 74 | 60 |
| 75 // These accessors are available on either thread | |
| 76 const net::SSLInfo& ssl_info() const { return ssl_info_; } | 61 const net::SSLInfo& ssl_info() const { return ssl_info_; } |
| 62 |
| 63 const GURL& request_url() const { return request_url_; } |
| 64 |
| 65 ResourceType resource_type() const { return resource_type_; } |
| 66 |
| 67 WebContents* web_contents() const { return web_contents_; } |
| 68 |
| 77 int cert_error() const { return cert_error_; } | 69 int cert_error() const { return cert_error_; } |
| 70 |
| 78 bool fatal() const { return fatal_; } | 71 bool fatal() const { return fatal_; } |
| 79 const GURL& request_url() const { return request_url_; } | |
| 80 ResourceType resource_type() const { return resource_type_; } | |
| 81 | 72 |
| 82 // Cancels the associated net::URLRequest. | 73 // Cancels the associated net::URLRequest. |
| 83 CONTENT_EXPORT void CancelRequest(); | 74 CONTENT_EXPORT void CancelRequest(); |
| 84 | 75 |
| 85 // Continue the net::URLRequest ignoring any previous errors. Note that some | 76 // Continue the net::URLRequest ignoring any previous errors. Note that some |
| 86 // errors cannot be ignored, in which case this will result in the request | 77 // errors cannot be ignored, in which case this will result in the request |
| 87 // being canceled. | 78 // being canceled. |
| 88 void ContinueRequest(); | 79 void ContinueRequest(); |
| 89 | 80 |
| 90 // Cancels the associated net::URLRequest and mark it as denied. The renderer | 81 // Cancels the associated net::URLRequest and mark it as denied. The renderer |
| 91 // processes such request in a special manner, optionally replacing them | 82 // processes such request in a special manner, optionally replacing them |
| 92 // with alternate content (typically frames content is replaced with a | 83 // with alternate content (typically frames content is replaced with a |
| 93 // warning message). | 84 // warning message). |
| 94 void DenyRequest(); | 85 void DenyRequest(); |
| 95 | 86 |
| 96 // Does nothing on the net::URLRequest but ensures the current instance ref | |
| 97 // count is decremented appropriately. | |
| 98 void TakeNoAction(); | |
| 99 | |
| 100 // Returns the manager associated with this SSLErrorHandler. | |
| 101 // Should only be accessed on the UI thread. | |
| 102 SSLManager* GetManager() const; | |
| 103 | |
| 104 private: | 87 private: |
| 105 friend class base::RefCountedThreadSafe<SSLErrorHandler>; | 88 // This must not be dereferenced on the UI thread. SSLErrorHandler |
| 106 | 89 // simply holds on to the reference to be passed back to the IO thread |
| 107 virtual ~SSLErrorHandler(); | 90 // to enact a decision about the error once one has been made. |
| 108 | |
| 109 virtual void OnDispatchFailed(); | |
| 110 | |
| 111 // Can use the manager_ member. | |
| 112 virtual void OnDispatched(); | |
| 113 | |
| 114 // Should only be accessed on the UI thread. | |
| 115 SSLManager* manager_; // Our manager. | |
| 116 | |
| 117 // The delegate we are associated with. | |
| 118 base::WeakPtr<Delegate> delegate_; | 91 base::WeakPtr<Delegate> delegate_; |
| 119 | 92 |
| 120 // Completes the CancelRequest operation on the IO thread. | 93 // The URL for the request that generated the error. |
| 121 // Call on the IO thread. | |
| 122 void CompleteCancelRequest(int error); | |
| 123 | |
| 124 // Completes the ContinueRequest operation on the IO thread. | |
| 125 // | |
| 126 // Call on the IO thread. | |
| 127 void CompleteContinueRequest(); | |
| 128 | |
| 129 // Derefs this instance. | |
| 130 // Call on the IO thread. | |
| 131 void CompleteTakeNoAction(); | |
| 132 | |
| 133 // A flag to make sure we notify the net::URLRequest exactly once. | |
| 134 // Should only be accessed on the IO thread | |
| 135 bool request_has_been_notified_; | |
| 136 | |
| 137 // The below read-only members may be accessed on any thread. | |
| 138 | |
| 139 // The URL that we requested. | |
| 140 const GURL request_url_; | 94 const GURL request_url_; |
| 141 | 95 |
| 142 // What kind of resource is associated with the request that generated | 96 // What kind of resource is associated with the request that generated |
| 143 // the error. | 97 // the error. |
| 144 const ResourceType resource_type_; | 98 const ResourceType resource_type_; |
| 145 | 99 |
| 146 // The SSLInfo associated with the request that generated the error. | 100 // The net::SSLInfo associated with the request that generated the error. |
| 147 const net::SSLInfo ssl_info_; | 101 const net::SSLInfo ssl_info_; |
| 148 | 102 |
| 149 // The net error code that occurred on the request. | 103 // A net error code describing the error that occurred. |
| 150 const int cert_error_; | 104 const int cert_error_; |
| 151 | 105 |
| 152 // True if the error is from a host requiring certificate errors to be fatal. | 106 // True if the error is from a host requiring certificate errors to be fatal. |
| 153 const bool fatal_; | 107 const bool fatal_; |
| 154 | 108 |
| 109 // The WebContents associated with the request that generated the error. |
| 110 WebContents* web_contents_; |
| 111 |
| 155 DISALLOW_COPY_AND_ASSIGN(SSLErrorHandler); | 112 DISALLOW_COPY_AND_ASSIGN(SSLErrorHandler); |
| 156 }; | 113 }; |
| 157 | 114 |
| 158 } // namespace content | 115 } // namespace content |
| 159 | 116 |
| 160 #endif // CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_ | 117 #endif // CONTENT_BROWSER_SSL_SSL_ERROR_HANDLER_H_ |
| OLD | NEW |