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 CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ | 5 #ifndef CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ |
6 #define CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ | 6 #define CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/sequenced_task_runner_helpers.h" | 15 #include "base/sequenced_task_runner_helpers.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/shell_integration.h" | 18 #include "chrome/browser/shell_integration.h" |
18 #include "chrome/common/custom_handlers/protocol_handler.h" | 19 #include "chrome/common/custom_handlers/protocol_handler.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
21 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
22 #include "net/url_request/url_request_job.h" | 23 #include "net/url_request/url_request_job.h" |
23 #include "net/url_request/url_request_job_factory.h" | 24 #include "net/url_request/url_request_job_factory.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( | 78 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( |
78 ShellIntegration::DefaultWebClientObserver* observer, | 79 ShellIntegration::DefaultWebClientObserver* observer, |
79 const std::string& protocol); | 80 const std::string& protocol); |
80 virtual DefaultClientObserver* CreateShellObserver( | 81 virtual DefaultClientObserver* CreateShellObserver( |
81 ProtocolHandlerRegistry* registry); | 82 ProtocolHandlerRegistry* registry); |
82 virtual void RegisterWithOSAsDefaultClient( | 83 virtual void RegisterWithOSAsDefaultClient( |
83 const std::string& protocol, | 84 const std::string& protocol, |
84 ProtocolHandlerRegistry* registry); | 85 ProtocolHandlerRegistry* registry); |
85 }; | 86 }; |
86 | 87 |
| 88 // Forward declaration of the internal implementation class. |
| 89 class IOThreadDelegate; |
| 90 |
| 91 // JobInterceptorFactory intercepts URLRequestJob creation for URLRequests the |
| 92 // ProtocolHandlerRegistry is registered to handle. When no handler is |
| 93 // registered, the URLRequest is passed along to the chained |
| 94 // URLRequestJobFactory (set with |JobInterceptorFactory::Chain|). |
| 95 // JobInterceptorFactory's are created via |
| 96 // |ProtocolHandlerRegistry::CreateJobInterceptorFactory|. |
| 97 class JobInterceptorFactory : public net::URLRequestJobFactory { |
| 98 public: |
| 99 // |io_thread_delegate| is used to perform actual job creation work. |
| 100 explicit JobInterceptorFactory(IOThreadDelegate* io_thread_delegate); |
| 101 virtual ~JobInterceptorFactory(); |
| 102 |
| 103 // |job_factory| is set as the URLRequestJobFactory where requests are |
| 104 // forwarded if JobInterceptorFactory decides to pass on them. |
| 105 void Chain(scoped_ptr<net::URLRequestJobFactory> job_factory); |
| 106 |
| 107 // URLRequestJobFactory implementation. |
| 108 virtual bool SetProtocolHandler(const std::string& scheme, |
| 109 ProtocolHandler* protocol_handler) OVERRIDE; |
| 110 virtual void AddInterceptor(Interceptor* interceptor) OVERRIDE; |
| 111 virtual net::URLRequestJob* MaybeCreateJobWithInterceptor( |
| 112 net::URLRequest* request, |
| 113 net::NetworkDelegate* network_delegate) const OVERRIDE; |
| 114 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 115 const std::string& scheme, |
| 116 net::URLRequest* request, |
| 117 net::NetworkDelegate* network_delegate) const OVERRIDE; |
| 118 virtual net::URLRequestJob* MaybeInterceptRedirect( |
| 119 const GURL& location, |
| 120 net::URLRequest* request, |
| 121 net::NetworkDelegate* network_delegate) const OVERRIDE; |
| 122 virtual net::URLRequestJob* MaybeInterceptResponse( |
| 123 net::URLRequest* request, |
| 124 net::NetworkDelegate* network_delegate) const OVERRIDE; |
| 125 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE; |
| 126 virtual bool IsHandledURL(const GURL& url) const OVERRIDE; |
| 127 |
| 128 private: |
| 129 // When JobInterceptorFactory decides to pass on particular requests, |
| 130 // they're forwarded to the chained URLRequestJobFactory, |job_factory_|. |
| 131 scoped_ptr<URLRequestJobFactory> job_factory_; |
| 132 // |io_thread_delegate_| performs the actual job creation decisions by |
| 133 // mirroring the ProtocolHandlerRegistry on the IO thread. |
| 134 scoped_refptr<IOThreadDelegate> io_thread_delegate_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(JobInterceptorFactory); |
| 137 }; |
| 138 |
87 typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap; | 139 typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap; |
88 typedef std::vector<ProtocolHandler> ProtocolHandlerList; | 140 typedef std::vector<ProtocolHandler> ProtocolHandlerList; |
89 typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap; | 141 typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap; |
90 typedef std::vector<DefaultClientObserver*> DefaultClientObserverList; | 142 typedef std::vector<DefaultClientObserver*> DefaultClientObserverList; |
91 | 143 |
92 // Creates a new instance. Assumes ownership of |delegate|. | 144 // Creates a new instance. Assumes ownership of |delegate|. |
93 ProtocolHandlerRegistry(Profile* profile, Delegate* delegate); | 145 ProtocolHandlerRegistry(Profile* profile, Delegate* delegate); |
94 virtual ~ProtocolHandlerRegistry(); | 146 virtual ~ProtocolHandlerRegistry(); |
95 | 147 |
96 // Returns a net::URLRequestJobFactory::Interceptor suitable | 148 // Returns a net::URLRequestJobFactory suitable for use on the IO thread, but |
97 // for use on the IO thread, but is initialized on the UI thread. | 149 // is initialized on the UI thread. |
98 // Callers assume responsibility for deleting this object. | 150 scoped_ptr<JobInterceptorFactory> CreateJobInterceptorFactory(); |
99 net::URLRequestJobFactory::Interceptor* CreateURLInterceptor(); | |
100 | 151 |
101 // Called when a site tries to register as a protocol handler. If the request | 152 // Called when a site tries to register as a protocol handler. If the request |
102 // can be handled silently by the registry - either to ignore the request | 153 // can be handled silently by the registry - either to ignore the request |
103 // or to update an existing handler - the request will succeed. If this | 154 // or to update an existing handler - the request will succeed. If this |
104 // function returns false the user needs to be prompted for confirmation. | 155 // function returns false the user needs to be prompted for confirmation. |
105 bool SilentlyHandleRegisterHandlerRequest(const ProtocolHandler& handler); | 156 bool SilentlyHandleRegisterHandlerRequest(const ProtocolHandler& handler); |
106 | 157 |
107 // Called when the user accepts the registration of a given protocol handler. | 158 // Called when the user accepts the registration of a given protocol handler. |
108 void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler); | 159 void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler); |
109 | 160 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 friend class base::DeleteHelper<ProtocolHandlerRegistry>; | 256 friend class base::DeleteHelper<ProtocolHandlerRegistry>; |
206 friend struct content::BrowserThread::DeleteOnThread< | 257 friend struct content::BrowserThread::DeleteOnThread< |
207 content::BrowserThread::IO>; | 258 content::BrowserThread::IO>; |
208 | 259 |
209 // for access to InstallDefaultsForChromeOS | 260 // for access to InstallDefaultsForChromeOS |
210 friend class ProtocolHandlerRegistryFactory; | 261 friend class ProtocolHandlerRegistryFactory; |
211 | 262 |
212 friend class ProtocolHandlerRegistryTest; | 263 friend class ProtocolHandlerRegistryTest; |
213 friend class RegisterProtocolHandlerBrowserTest; | 264 friend class RegisterProtocolHandlerBrowserTest; |
214 | 265 |
215 // Forward declaration of the internal implementation classes. | |
216 class Core; | |
217 class URLInterceptor; | |
218 | |
219 // Puts the given handler at the top of the list of handlers for its | 266 // Puts the given handler at the top of the list of handlers for its |
220 // protocol. | 267 // protocol. |
221 void PromoteHandler(const ProtocolHandler& handler); | 268 void PromoteHandler(const ProtocolHandler& handler); |
222 | 269 |
223 // Saves a user's registered protocol handlers. | 270 // Saves a user's registered protocol handlers. |
224 void Save(); | 271 void Save(); |
225 | 272 |
226 // Returns a pointer to the list of handlers registered for the given scheme, | 273 // Returns a pointer to the list of handlers registered for the given scheme, |
227 // or NULL if there are none. | 274 // or NULL if there are none. |
228 const ProtocolHandlerList* GetHandlerList(const std::string& scheme) const; | 275 const ProtocolHandlerList* GetHandlerList(const std::string& scheme) const; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 | 327 |
281 // Whether or not we are loading. | 328 // Whether or not we are loading. |
282 bool is_loading_; | 329 bool is_loading_; |
283 | 330 |
284 // When the table gets loaded this flag will be set and any further calls to | 331 // When the table gets loaded this flag will be set and any further calls to |
285 // AddPredefinedHandler will be rejected. | 332 // AddPredefinedHandler will be rejected. |
286 bool is_loaded_; | 333 bool is_loaded_; |
287 | 334 |
288 // Copy of registry data for use on the IO thread. Changes to the registry | 335 // Copy of registry data for use on the IO thread. Changes to the registry |
289 // are posted to the IO thread where updates are applied to this object. | 336 // are posted to the IO thread where updates are applied to this object. |
290 scoped_refptr<Core> core_; | 337 scoped_refptr<IOThreadDelegate> io_thread_delegate_; |
291 | 338 |
292 DefaultClientObserverList default_client_observers_; | 339 DefaultClientObserverList default_client_observers_; |
293 | 340 |
294 DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry); | 341 DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry); |
295 }; | 342 }; |
296 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ | 343 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ |
OLD | NEW |