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