Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(387)

Side by Side Diff: chrome/browser/custom_handlers/protocol_handler_registry.h

Issue 10546083: Convert ProtocolHandlerRegistry to be a ProfileKeyedService. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Merge in MessageLoop conflict from upstream. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
45 46
46 // Give the observer a handle to the worker, so we can find out the protocol 47 // Give the observer a handle to the worker, so we can find out the protocol
47 // when we're called and also tell the worker if we get deleted. 48 // when we're called and also tell the worker if we get deleted.
48 void SetWorker(ShellIntegration::DefaultProtocolClientWorker* worker); 49 void SetWorker(ShellIntegration::DefaultProtocolClientWorker* worker);
49 50
50 protected: 51 protected:
51 ShellIntegration::DefaultProtocolClientWorker* worker_; 52 ShellIntegration::DefaultProtocolClientWorker* worker_;
52 53
53 private: 54 private:
54 virtual bool IsOwnedByWorker() OVERRIDE { return true; } 55 virtual bool IsOwnedByWorker() OVERRIDE { return true; }
55 // This is a raw pointer, not reference counted, intentionally. In general 56 // This is a raw pointer, not reference counted, intentionally. In general
56 // subclasses of DefaultWebClientObserver are not able to be refcounted 57 // subclasses of DefaultWebClientObserver are not able to be refcounted
57 // e.g. the browser options page 58 // e.g. the browser options page
58 ProtocolHandlerRegistry* registry_; 59 ProtocolHandlerRegistry* registry_;
59 60
60 DISALLOW_COPY_AND_ASSIGN(DefaultClientObserver); 61 DISALLOW_COPY_AND_ASSIGN(DefaultClientObserver);
61 }; 62 };
62 63
64 // The tersely named |Delegate| class provides an interface for interacting
65 // asynchronously with the underlying OS for the purposes of registering
66 // Chrome as the default handler for specific protocols.
67 //
63 // TODO(koz): Refactor this to eliminate the unnecessary virtuals. All that 68 // TODO(koz): Refactor this to eliminate the unnecessary virtuals. All that
64 // should be needed is a way to ensure that the list of websafe protocols is 69 // should be needed is a way to ensure that the list of websafe protocols is
65 // updated. 70 // updated.
66 class Delegate { 71 class Delegate {
67 public: 72 public:
68 virtual ~Delegate(); 73 virtual ~Delegate();
69 virtual void RegisterExternalHandler(const std::string& protocol); 74 virtual void RegisterExternalHandler(const std::string& protocol);
70 virtual void DeregisterExternalHandler(const std::string& protocol); 75 virtual void DeregisterExternalHandler(const std::string& protocol);
71 virtual bool IsExternalHandlerRegistered(const std::string& protocol); 76 virtual bool IsExternalHandlerRegistered(const std::string& protocol);
72 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( 77 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker(
73 ShellIntegration::DefaultWebClientObserver* observer, 78 ShellIntegration::DefaultWebClientObserver* observer,
74 const std::string& protocol); 79 const std::string& protocol);
75 virtual DefaultClientObserver* CreateShellObserver( 80 virtual DefaultClientObserver* CreateShellObserver(
76 ProtocolHandlerRegistry* registry); 81 ProtocolHandlerRegistry* registry);
77 virtual void RegisterWithOSAsDefaultClient( 82 virtual void RegisterWithOSAsDefaultClient(
78 const std::string& protocol, 83 const std::string& protocol,
79 ProtocolHandlerRegistry* registry); 84 ProtocolHandlerRegistry* registry);
80 }; 85 };
81 86
82 typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap; 87 typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap;
83 typedef std::vector<ProtocolHandler> ProtocolHandlerList; 88 typedef std::vector<ProtocolHandler> ProtocolHandlerList;
84 typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap; 89 typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap;
85 typedef std::vector<DefaultClientObserver*> DefaultClientObserverList; 90 typedef std::vector<DefaultClientObserver*> DefaultClientObserverList;
86 91
87 ProtocolHandlerRegistry(Profile* profile, Delegate* delegate); 92 ProtocolHandlerRegistry(Profile* profile, Delegate* delegate);
93 virtual ~ProtocolHandlerRegistry();
94
95 // Returns a net::URLRequestJobFactory::Interceptor suitable
96 // for use on the IO thread, but is initialized on the UI thread.
97 // Callers assume responsibility for deleting this object.
98 net::URLRequestJobFactory::Interceptor* CreateURLInterceptor();
88 99
89 // Called when a site tries to register as a protocol handler. If the request 100 // 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 101 // 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 102 // or to update an existing handler - the request will succeed. If this
92 // function returns false the user needs to be prompted for confirmation. 103 // function returns false the user needs to be prompted for confirmation.
93 bool SilentlyHandleRegisterHandlerRequest(const ProtocolHandler& handler); 104 bool SilentlyHandleRegisterHandlerRequest(const ProtocolHandler& handler);
94 105
95 // Called when the user accepts the registration of a given protocol handler. 106 // Called when the user accepts the registration of a given protocol handler.
96 void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler); 107 void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler);
97 108
(...skipping 12 matching lines...) Expand all
110 // Returns a list of protocol handlers that can be replaced by the given 121 // Returns a list of protocol handlers that can be replaced by the given
111 // handler. 122 // handler.
112 ProtocolHandlerList GetReplacedHandlers(const ProtocolHandler& handler) const; 123 ProtocolHandlerList GetReplacedHandlers(const ProtocolHandler& handler) const;
113 124
114 // Clears the default for the provided protocol. 125 // Clears the default for the provided protocol.
115 void ClearDefault(const std::string& scheme); 126 void ClearDefault(const std::string& scheme);
116 127
117 // Returns true if this handler is the default handler for its protocol. 128 // Returns true if this handler is the default handler for its protocol.
118 bool IsDefault(const ProtocolHandler& handler) const; 129 bool IsDefault(const ProtocolHandler& handler) const;
119 130
120 // Loads a user's registered protocol handlers. 131 // Initializes default protocol settings and loads them from prefs.
121 void Load(); 132 // This method must be called to complete initialization of the
133 // registry after creation, and prior to use.
134 void InitProtocolSettings();
122 135
123 // Returns the offset in the list of handlers for a protocol of the default 136 // Returns the offset in the list of handlers for a protocol of the default
124 // handler for that protocol. 137 // handler for that protocol.
125 int GetHandlerIndex(const std::string& scheme) const; 138 int GetHandlerIndex(const std::string& scheme) const;
126 139
127 // Get the list of protocol handlers for the given scheme. 140 // Get the list of protocol handlers for the given scheme.
128 ProtocolHandlerList GetHandlersFor(const std::string& scheme) const; 141 ProtocolHandlerList GetHandlersFor(const std::string& scheme) const;
129 142
130 // Get the list of ignored protocol handlers. 143 // Get the list of ignored protocol handlers.
131 ProtocolHandlerList GetIgnoredHandlers(); 144 ProtocolHandlerList GetIgnoredHandlers();
(...skipping 17 matching lines...) Expand all
149 162
150 // Returns true if an equivalent protocol handler is being ignored. 163 // Returns true if an equivalent protocol handler is being ignored.
151 bool HasIgnoredEquivalent(const ProtocolHandler& handler) const; 164 bool HasIgnoredEquivalent(const ProtocolHandler& handler) const;
152 165
153 // Causes the given protocol handler to not be ignored anymore. 166 // Causes the given protocol handler to not be ignored anymore.
154 void RemoveIgnoredHandler(const ProtocolHandler& handler); 167 void RemoveIgnoredHandler(const ProtocolHandler& handler);
155 168
156 // Returns true if the protocol has a default protocol handler. 169 // Returns true if the protocol has a default protocol handler.
157 bool IsHandledProtocol(const std::string& scheme) const; 170 bool IsHandledProtocol(const std::string& scheme) const;
158 171
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. 172 // Removes the given protocol handler from the registry.
164 void RemoveHandler(const ProtocolHandler& handler); 173 void RemoveHandler(const ProtocolHandler& handler);
165 174
166 // Remove the default handler for the given protocol. 175 // Remove the default handler for the given protocol.
167 void RemoveDefaultHandler(const std::string& scheme); 176 void RemoveDefaultHandler(const std::string& scheme);
168 177
169 // Returns the default handler for this protocol, or an empty handler if none 178 // Returns the default handler for this protocol, or an empty handler if none
170 // exists. 179 // exists.
171 const ProtocolHandler& GetHandlerFor(const std::string& scheme) const; 180 const ProtocolHandler& GetHandlerFor(const std::string& scheme) const;
172 181
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 182 // Puts this registry in the enabled state - registered protocol handlers
178 // will handle requests. 183 // will handle requests.
179 void Enable(); 184 void Enable();
180 185
181 // Puts this registry in the disabled state - registered protocol handlers 186 // Puts this registry in the disabled state - registered protocol handlers
182 // will not handle requests. 187 // will not handle requests.
183 void Disable(); 188 void Disable();
184 189
185 // This is called by the UI thread when the system is shutting down. This 190 // 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. 191 // does finalization which must be done on the UI thread.
187 void Finalize(); 192 virtual void Shutdown() OVERRIDE;
188 193
189 // Registers the preferences that we store registered protocol handlers in. 194 // Registers the preferences that we store registered protocol handlers in.
190 static void RegisterPrefs(PrefService* prefService); 195 static void RegisterPrefs(PrefService* prefService);
191 196
192 bool enabled() const { return enabled_; } 197 bool enabled() const { return enabled_; }
193 198
194 // Add a predefined protocol handler. This has to be called before the first 199 // Add a predefined protocol handler. This has to be called before the first
195 // load command was issued, otherwise the command will be ignored. 200 // load command was issued, otherwise the command will be ignored.
196 void AddPredefinedHandler(const ProtocolHandler& handler); 201 void AddPredefinedHandler(const ProtocolHandler& handler);
197 202
198 private: 203 private:
199 friend class base::DeleteHelper<ProtocolHandlerRegistry>; 204 friend class base::DeleteHelper<ProtocolHandlerRegistry>;
200 friend struct content::BrowserThread::DeleteOnThread< 205 friend struct content::BrowserThread::DeleteOnThread<
201 content::BrowserThread::IO>; 206 content::BrowserThread::IO>;
202 friend class ProtocolHandlerRegistryTest; 207 friend class ProtocolHandlerRegistryTest;
203 friend class RegisterProtocolHandlerBrowserTest; 208 friend class RegisterProtocolHandlerBrowserTest;
204 209
205 ~ProtocolHandlerRegistry(); 210 // Forward declaration of our internal implementation classes.
211 class Core;
212 class URLInterceptor;
206 213
207 // Puts the given handler at the top of the list of handlers for its 214 // Puts the given handler at the top of the list of handlers for its
208 // protocol. 215 // protocol.
209 void PromoteHandler(const ProtocolHandler& handler); 216 void PromoteHandler(const ProtocolHandler& handler);
210 217
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. 218 // Saves a user's registered protocol handlers.
228 void Save(); 219 void Save();
229 220
230 // Returns a pointer to the list of handlers registered for the given scheme, 221 // Returns a pointer to the list of handlers registered for the given scheme,
231 // or NULL if there are none. 222 // or NULL if there are none.
232 const ProtocolHandlerList* GetHandlerList(const std::string& scheme) const; 223 const ProtocolHandlerList* GetHandlerList(const std::string& scheme) const;
233 224
234 // Makes this ProtocolHandler the default handler for its protocol. 225 // Makes this ProtocolHandler the default handler for its protocol.
235 void SetDefault(const ProtocolHandler& handler); 226 void SetDefault(const ProtocolHandler& handler);
236 227
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 // The Profile that owns this ProtocolHandlerRegistry. 262 // The Profile that owns this ProtocolHandlerRegistry.
272 Profile* profile_; 263 Profile* profile_;
273 264
274 // The Delegate that registers / deregisters external handlers on our behalf. 265 // The Delegate that registers / deregisters external handlers on our behalf.
275 scoped_ptr<Delegate> delegate_; 266 scoped_ptr<Delegate> delegate_;
276 267
277 // If false then registered protocol handlers will not be used to handle 268 // If false then registered protocol handlers will not be used to handle
278 // requests. 269 // requests.
279 bool enabled_; 270 bool enabled_;
280 271
281 // Copy of enabled_ that is only accessed on the IO thread.
282 bool enabled_io_;
283
284 // Whether or not we are loading. 272 // Whether or not we are loading.
285 bool is_loading_; 273 bool is_loading_;
286 274
287 // When the table gets loaded this flag will be set and any further calls to 275 // When the table gets loaded this flag will be set and any further calls to
288 // AddPredefinedHandler will be rejected. 276 // AddPredefinedHandler will be rejected.
289 bool is_loaded_; 277 bool is_loaded_;
290 278
279 scoped_refptr<Core> core_;
280
291 DefaultClientObserverList default_client_observers_; 281 DefaultClientObserverList default_client_observers_;
292 282
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); 283 DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry);
297 }; 284 };
298 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ 285 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698