Chromium Code Reviews| 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 #pragma once | 7 #pragma once | 
| 8 | 8 | 
| 9 #include <map> | 9 #include <map> | 
| 10 #include <string> | 10 #include <string> | 
| 11 #include <vector> | 11 #include <vector> | 
| 12 | 12 | 
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" | 
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" | 
| 15 #include "base/sequenced_task_runner_helpers.h" | 15 #include "base/sequenced_task_runner_helpers.h" | 
| 16 #include "base/values.h" | 16 #include "base/values.h" | 
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" | 
| 18 #include "chrome/browser/shell_integration.h" | 18 #include "chrome/browser/shell_integration.h" | 
| 19 #include "chrome/common/custom_handlers/protocol_handler.h" | 19 #include "chrome/common/custom_handlers/protocol_handler.h" | 
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" | 
| 21 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" | 
| 22 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" | 
| 23 #include "net/url_request/url_request_job.h" | 23 #include "net/url_request/url_request_job.h" | 
| 24 #include "net/url_request/url_request_job_factory.h" | |
| 24 | 25 | 
| 25 // This is where handlers for protocols registered with | 26 // This is where handlers for protocols registered with | 
| 26 // navigator.registerProtocolHandler() are registered. Each Profile owns an | 27 // navigator.registerProtocolHandler() are registered. Each Profile owns an | 
| 27 // instance of this class, which is initialized on browser start through | 28 // instance of this class, which is initialized on browser start through | 
| 28 // Profile::InitRegisteredProtocolHandlers(), and they should be the only | 29 // Profile::InitRegisteredProtocolHandlers(), and they should be the only | 
| 29 // instances of this class. | 30 // instances of this class. | 
| 31 class ProtocolHandlerRegistry : public ProfileKeyedService { | |
| 30 | 32 | 
| 31 class ProtocolHandlerRegistry | |
| 32 : public base::RefCountedThreadSafe< | |
| 33 ProtocolHandlerRegistry, content::BrowserThread::DeleteOnIOThread> { | |
| 34 public: | 33 public: | 
| 34 // Provides notification of when the OS level user agent settings | |
| 35 // are changed. | |
| 35 class DefaultClientObserver | 36 class DefaultClientObserver | 
| 36 : public ShellIntegration::DefaultWebClientObserver { | 37 : public ShellIntegration::DefaultWebClientObserver { | 
| 37 public: | 38 public: | 
| 38 explicit DefaultClientObserver(ProtocolHandlerRegistry* registry); | 39 explicit DefaultClientObserver(ProtocolHandlerRegistry* registry); | 
| 39 virtual ~DefaultClientObserver(); | 40 virtual ~DefaultClientObserver(); | 
| 40 | 41 | 
| 41 // Get response from the worker regarding whether Chrome is the default | 42 // Get response from the worker regarding whether Chrome is the default | 
| 42 // handler for the protocol. | 43 // handler for the protocol. | 
| 43 virtual void SetDefaultWebClientUIState( | 44 virtual void SetDefaultWebClientUIState( | 
| 44 ShellIntegration::DefaultWebClientUIState state) OVERRIDE; | 45 ShellIntegration::DefaultWebClientUIState state) OVERRIDE; | 
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 ProtocolHandlerRegistry* registry); | 80 ProtocolHandlerRegistry* registry); | 
| 80 }; | 81 }; | 
| 81 | 82 | 
| 82 typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap; | 83 typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap; | 
| 83 typedef std::vector<ProtocolHandler> ProtocolHandlerList; | 84 typedef std::vector<ProtocolHandler> ProtocolHandlerList; | 
| 84 typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap; | 85 typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap; | 
| 85 typedef std::vector<DefaultClientObserver*> DefaultClientObserverList; | 86 typedef std::vector<DefaultClientObserver*> DefaultClientObserverList; | 
| 86 | 87 | 
| 87 ProtocolHandlerRegistry(Profile* profile, Delegate* delegate); | 88 ProtocolHandlerRegistry(Profile* profile, Delegate* delegate); | 
| 88 | 89 | 
| 90 // Returns a net::URLRequestJobFactory::Interceptor suitable | |
| 91 // for use on the IO thread, but is initialized on the UI thread. | |
| 92 // Callers assume responsibility for deleting this object. | |
| 93 net::URLRequestJobFactory::Interceptor* CreateIOURLInterceptor(); | |
| 94 | |
| 89 // Called when a site tries to register as a protocol handler. If the request | 95 // Called when a site tries to register as a protocol handler. If the request | 
| 90 // can be handled silently by the registry - either to ignore the request | 96 // can be handled silently by the registry - either to ignore the request | 
| 91 // or to update an existing handler - the request will succeed. If this | 97 // or to update an existing handler - the request will succeed. If this | 
| 92 // function returns false the user needs to be prompted for confirmation. | 98 // function returns false the user needs to be prompted for confirmation. | 
| 93 bool SilentlyHandleRegisterHandlerRequest(const ProtocolHandler& handler); | 99 bool SilentlyHandleRegisterHandlerRequest(const ProtocolHandler& handler); | 
| 94 | 100 | 
| 95 // Called when the user accepts the registration of a given protocol handler. | 101 // Called when the user accepts the registration of a given protocol handler. | 
| 96 void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler); | 102 void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler); | 
| 97 | 103 | 
| 98 // Called when the user denies the registration of a given protocol handler. | 104 // Called when the user denies the registration of a given protocol handler. | 
| (...skipping 11 matching lines...) Expand all Loading... | |
| 110 // Returns a list of protocol handlers that can be replaced by the given | 116 // Returns a list of protocol handlers that can be replaced by the given | 
| 111 // handler. | 117 // handler. | 
| 112 ProtocolHandlerList GetReplacedHandlers(const ProtocolHandler& handler) const; | 118 ProtocolHandlerList GetReplacedHandlers(const ProtocolHandler& handler) const; | 
| 113 | 119 | 
| 114 // Clears the default for the provided protocol. | 120 // Clears the default for the provided protocol. | 
| 115 void ClearDefault(const std::string& scheme); | 121 void ClearDefault(const std::string& scheme); | 
| 116 | 122 | 
| 117 // Returns true if this handler is the default handler for its protocol. | 123 // Returns true if this handler is the default handler for its protocol. | 
| 118 bool IsDefault(const ProtocolHandler& handler) const; | 124 bool IsDefault(const ProtocolHandler& handler) const; | 
| 119 | 125 | 
| 120 // Loads a user's registered protocol handlers. | 126 // Initializes default protocol settings and loads them from prefs. | 
| 121 void Load(); | 127 void InitProtocolSettings(); | 
| 122 | 128 | 
| 123 // Returns the offset in the list of handlers for a protocol of the default | 129 // Returns the offset in the list of handlers for a protocol of the default | 
| 124 // handler for that protocol. | 130 // handler for that protocol. | 
| 125 int GetHandlerIndex(const std::string& scheme) const; | 131 int GetHandlerIndex(const std::string& scheme) const; | 
| 126 | 132 | 
| 127 // Get the list of protocol handlers for the given scheme. | 133 // Get the list of protocol handlers for the given scheme. | 
| 128 ProtocolHandlerList GetHandlersFor(const std::string& scheme) const; | 134 ProtocolHandlerList GetHandlersFor(const std::string& scheme) const; | 
| 129 | 135 | 
| 130 // Get the list of ignored protocol handlers. | 136 // Get the list of ignored protocol handlers. | 
| 131 ProtocolHandlerList GetIgnoredHandlers(); | 137 ProtocolHandlerList GetIgnoredHandlers(); | 
| (...skipping 17 matching lines...) Expand all Loading... | |
| 149 | 155 | 
| 150 // Returns true if an equivalent protocol handler is being ignored. | 156 // Returns true if an equivalent protocol handler is being ignored. | 
| 151 bool HasIgnoredEquivalent(const ProtocolHandler& handler) const; | 157 bool HasIgnoredEquivalent(const ProtocolHandler& handler) const; | 
| 152 | 158 | 
| 153 // Causes the given protocol handler to not be ignored anymore. | 159 // Causes the given protocol handler to not be ignored anymore. | 
| 154 void RemoveIgnoredHandler(const ProtocolHandler& handler); | 160 void RemoveIgnoredHandler(const ProtocolHandler& handler); | 
| 155 | 161 | 
| 156 // Returns true if the protocol has a default protocol handler. | 162 // Returns true if the protocol has a default protocol handler. | 
| 157 bool IsHandledProtocol(const std::string& scheme) const; | 163 bool IsHandledProtocol(const std::string& scheme) const; | 
| 158 | 164 | 
| 159 // Returns true if the protocol has a default protocol handler. | |
| 160 // Should be called only from the IO thread. | |
| 161 bool IsHandledProtocolIO(const std::string& scheme) const; | |
| 162 | |
| 163 // Removes the given protocol handler from the registry. | 165 // Removes the given protocol handler from the registry. | 
| 164 void RemoveHandler(const ProtocolHandler& handler); | 166 void RemoveHandler(const ProtocolHandler& handler); | 
| 165 | 167 | 
| 166 // Remove the default handler for the given protocol. | 168 // Remove the default handler for the given protocol. | 
| 167 void RemoveDefaultHandler(const std::string& scheme); | 169 void RemoveDefaultHandler(const std::string& scheme); | 
| 168 | 170 | 
| 169 // Returns the default handler for this protocol, or an empty handler if none | 171 // Returns the default handler for this protocol, or an empty handler if none | 
| 170 // exists. | 172 // exists. | 
| 171 const ProtocolHandler& GetHandlerFor(const std::string& scheme) const; | 173 const ProtocolHandler& GetHandlerFor(const std::string& scheme) const; | 
| 172 | 174 | 
| 173 // Creates a URL request job for the given request if there is a matching | |
| 174 // protocol handler, returns NULL otherwise. | |
| 175 net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const; | |
| 176 | |
| 177 // Puts this registry in the enabled state - registered protocol handlers | 175 // Puts this registry in the enabled state - registered protocol handlers | 
| 178 // will handle requests. | 176 // will handle requests. | 
| 179 void Enable(); | 177 void Enable(); | 
| 180 | 178 | 
| 181 // Puts this registry in the disabled state - registered protocol handlers | 179 // Puts this registry in the disabled state - registered protocol handlers | 
| 182 // will not handle requests. | 180 // will not handle requests. | 
| 183 void Disable(); | 181 void Disable(); | 
| 184 | 182 | 
| 185 // This is called by the UI thread when the system is shutting down. This | 183 // This is called by the UI thread when the system is shutting down. This | 
| 186 // does finalization which must be done on the UI thread. | 184 // does finalization which must be done on the UI thread. | 
| 187 void Finalize(); | 185 virtual void Shutdown() OVERRIDE; | 
| 188 | 186 | 
| 189 // Registers the preferences that we store registered protocol handlers in. | 187 // Registers the preferences that we store registered protocol handlers in. | 
| 190 static void RegisterPrefs(PrefService* prefService); | 188 static void RegisterPrefs(PrefService* prefService); | 
| 191 | 189 | 
| 192 bool enabled() const { return enabled_; } | 190 bool enabled() const { return enabled_; } | 
| 193 | 191 | 
| 194 // Add a predefined protocol handler. This has to be called before the first | 192 // Add a predefined protocol handler. This has to be called before the first | 
| 195 // load command was issued, otherwise the command will be ignored. | 193 // load command was issued, otherwise the command will be ignored. | 
| 196 void AddPredefinedHandler(const ProtocolHandler& handler); | 194 void AddPredefinedHandler(const ProtocolHandler& handler); | 
| 197 | 195 | 
| 198 private: | 196 private: | 
| 199 friend class base::DeleteHelper<ProtocolHandlerRegistry>; | 197 friend class base::DeleteHelper<ProtocolHandlerRegistry>; | 
| 200 friend struct content::BrowserThread::DeleteOnThread< | 198 friend struct content::BrowserThread::DeleteOnThread< | 
| 201 content::BrowserThread::IO>; | 199 content::BrowserThread::IO>; | 
| 202 friend class ProtocolHandlerRegistryTest; | 200 friend class ProtocolHandlerRegistryTest; | 
| 203 friend class RegisterProtocolHandlerBrowserTest; | 201 friend class RegisterProtocolHandlerBrowserTest; | 
| 204 | 202 | 
| 205 ~ProtocolHandlerRegistry(); | 203 // Forward declaration of our internal implementation classes. | 
| 204 class Core; | |
| 
 
benwells
2012/06/22 23:31:27
Maybe call this something like IORegistry, CoreIO,
 
Steve McKay
2012/06/27 21:56:45
I had originally called this IOPartner, but rename
 
benwells
2012/06/28 08:15:46
I agree Core is superior to IOPartner. I'm fine wi
 
 | |
| 205 class IOURLInterceptor; | |
| 206 | |
| 207 virtual ~ProtocolHandlerRegistry(); | |
| 206 | 208 | 
| 207 // Puts the given handler at the top of the list of handlers for its | 209 // Puts the given handler at the top of the list of handlers for its | 
| 208 // protocol. | 210 // protocol. | 
| 209 void PromoteHandler(const ProtocolHandler& handler); | 211 void PromoteHandler(const ProtocolHandler& handler); | 
| 210 | 212 | 
| 211 // Clears the default for the provided protocol. | |
| 212 // Should be called only from the IO thread. | |
| 213 void ClearDefaultIO(const std::string& scheme); | |
| 214 | |
| 215 // Makes this ProtocolHandler the default handler for its protocol. | |
| 216 // Should be called only from the IO thread. | |
| 217 void SetDefaultIO(const ProtocolHandler& handler); | |
| 218 | |
| 219 // Indicate that the registry has been enabled in the IO thread's copy of the | |
| 220 // data. | |
| 221 void EnableIO() { enabled_io_ = true; } | |
| 222 | |
| 223 // Indicate that the registry has been disabled in the IO thread's copy of | |
| 224 // the data. | |
| 225 void DisableIO() { enabled_io_ = false; } | |
| 226 | |
| 227 // Saves a user's registered protocol handlers. | 213 // Saves a user's registered protocol handlers. | 
| 228 void Save(); | 214 void Save(); | 
| 229 | 215 | 
| 230 // Returns a pointer to the list of handlers registered for the given scheme, | 216 // Returns a pointer to the list of handlers registered for the given scheme, | 
| 231 // or NULL if there are none. | 217 // or NULL if there are none. | 
| 232 const ProtocolHandlerList* GetHandlerList(const std::string& scheme) const; | 218 const ProtocolHandlerList* GetHandlerList(const std::string& scheme) const; | 
| 233 | 219 | 
| 234 // Makes this ProtocolHandler the default handler for its protocol. | 220 // Makes this ProtocolHandler the default handler for its protocol. | 
| 235 void SetDefault(const ProtocolHandler& handler); | 221 void SetDefault(const ProtocolHandler& handler); | 
| 236 | 222 | 
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 // The Profile that owns this ProtocolHandlerRegistry. | 257 // The Profile that owns this ProtocolHandlerRegistry. | 
| 272 Profile* profile_; | 258 Profile* profile_; | 
| 273 | 259 | 
| 274 // The Delegate that registers / deregisters external handlers on our behalf. | 260 // The Delegate that registers / deregisters external handlers on our behalf. | 
| 275 scoped_ptr<Delegate> delegate_; | 261 scoped_ptr<Delegate> delegate_; | 
| 276 | 262 | 
| 277 // If false then registered protocol handlers will not be used to handle | 263 // If false then registered protocol handlers will not be used to handle | 
| 278 // requests. | 264 // requests. | 
| 279 bool enabled_; | 265 bool enabled_; | 
| 280 | 266 | 
| 281 // Copy of enabled_ that is only accessed on the IO thread. | |
| 282 bool enabled_io_; | |
| 283 | |
| 284 // Whether or not we are loading. | 267 // Whether or not we are loading. | 
| 285 bool is_loading_; | 268 bool is_loading_; | 
| 286 | 269 | 
| 287 // When the table gets loaded this flag will be set and any further calls to | 270 // When the table gets loaded this flag will be set and any further calls to | 
| 288 // AddPredefinedHandler will be rejected. | 271 // AddPredefinedHandler will be rejected. | 
| 289 bool is_loaded_; | 272 bool is_loaded_; | 
| 290 | 273 | 
| 274 scoped_refptr<Core> core_; | |
| 275 | |
| 291 DefaultClientObserverList default_client_observers_; | 276 DefaultClientObserverList default_client_observers_; | 
| 292 | 277 | 
| 293 // Copy of default_handlers_ that is only accessed on the IO thread. | |
| 294 ProtocolHandlerMap default_handlers_io_; | |
| 295 | |
| 296 DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry); | 278 DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry); | 
| 297 }; | 279 }; | 
| 298 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ | 280 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ | 
| OLD | NEW |