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