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

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: Respond to Elliot's comments. Created 8 years, 6 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
willchan no longer on Chromium 2012/06/22 19:01:56 -whitespace
Steve McKay 2012/06/27 21:49:28 Done.
35 // Provides notification of when the OS level user agent settings
36 // are changed.
35 class DefaultClientObserver 37 class DefaultClientObserver
36 : public ShellIntegration::DefaultWebClientObserver { 38 : public ShellIntegration::DefaultWebClientObserver {
37 public: 39 public:
38 explicit DefaultClientObserver(ProtocolHandlerRegistry* registry); 40 explicit DefaultClientObserver(ProtocolHandlerRegistry* registry);
39 virtual ~DefaultClientObserver(); 41 virtual ~DefaultClientObserver();
40 42
41 // Get response from the worker regarding whether Chrome is the default 43 // Get response from the worker regarding whether Chrome is the default
42 // handler for the protocol. 44 // handler for the protocol.
43 virtual void SetDefaultWebClientUIState( 45 virtual void SetDefaultWebClientUIState(
44 ShellIntegration::DefaultWebClientUIState state) OVERRIDE; 46 ShellIntegration::DefaultWebClientUIState state) OVERRIDE;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 ProtocolHandlerRegistry* registry); 81 ProtocolHandlerRegistry* registry);
80 }; 82 };
81 83
82 typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap; 84 typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap;
83 typedef std::vector<ProtocolHandler> ProtocolHandlerList; 85 typedef std::vector<ProtocolHandler> ProtocolHandlerList;
84 typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap; 86 typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap;
85 typedef std::vector<DefaultClientObserver*> DefaultClientObserverList; 87 typedef std::vector<DefaultClientObserver*> DefaultClientObserverList;
86 88
87 ProtocolHandlerRegistry(Profile* profile, Delegate* delegate); 89 ProtocolHandlerRegistry(Profile* profile, Delegate* delegate);
88 90
91 // Returns a net::URLRequestJobFactory::Interceptor suitable
92 // for use on the IO thread, but is initialized on the UI thread.
93 // Callers assume responsibility for deleting this object.
94 net::URLRequestJobFactory::Interceptor* CreateIOURLInterceptor();
95
89 // Called when a site tries to register as a protocol handler. If the request 96 // 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 97 // 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 98 // or to update an existing handler - the request will succeed. If this
92 // function returns false the user needs to be prompted for confirmation. 99 // function returns false the user needs to be prompted for confirmation.
93 bool SilentlyHandleRegisterHandlerRequest(const ProtocolHandler& handler); 100 bool SilentlyHandleRegisterHandlerRequest(const ProtocolHandler& handler);
94 101
95 // Called when the user accepts the registration of a given protocol handler. 102 // Called when the user accepts the registration of a given protocol handler.
96 void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler); 103 void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler);
97 104
98 // Called when the user denies the registration of a given protocol handler. 105 // Called when the user denies the registration of a given protocol handler.
(...skipping 11 matching lines...) Expand all
110 // Returns a list of protocol handlers that can be replaced by the given 117 // Returns a list of protocol handlers that can be replaced by the given
111 // handler. 118 // handler.
112 ProtocolHandlerList GetReplacedHandlers(const ProtocolHandler& handler) const; 119 ProtocolHandlerList GetReplacedHandlers(const ProtocolHandler& handler) const;
113 120
114 // Clears the default for the provided protocol. 121 // Clears the default for the provided protocol.
115 void ClearDefault(const std::string& scheme); 122 void ClearDefault(const std::string& scheme);
116 123
117 // Returns true if this handler is the default handler for its protocol. 124 // Returns true if this handler is the default handler for its protocol.
118 bool IsDefault(const ProtocolHandler& handler) const; 125 bool IsDefault(const ProtocolHandler& handler) const;
119 126
120 // Loads a user's registered protocol handlers. 127 // Initializes default protocol settings and loads them from prefs.
121 void Load(); 128 void InitProtocolSettings();
122 129
123 // Returns the offset in the list of handlers for a protocol of the default 130 // Returns the offset in the list of handlers for a protocol of the default
124 // handler for that protocol. 131 // handler for that protocol.
125 int GetHandlerIndex(const std::string& scheme) const; 132 int GetHandlerIndex(const std::string& scheme) const;
126 133
127 // Get the list of protocol handlers for the given scheme. 134 // Get the list of protocol handlers for the given scheme.
128 ProtocolHandlerList GetHandlersFor(const std::string& scheme) const; 135 ProtocolHandlerList GetHandlersFor(const std::string& scheme) const;
129 136
130 // Get the list of ignored protocol handlers. 137 // Get the list of ignored protocol handlers.
131 ProtocolHandlerList GetIgnoredHandlers(); 138 ProtocolHandlerList GetIgnoredHandlers();
(...skipping 17 matching lines...) Expand all
149 156
150 // Returns true if an equivalent protocol handler is being ignored. 157 // Returns true if an equivalent protocol handler is being ignored.
151 bool HasIgnoredEquivalent(const ProtocolHandler& handler) const; 158 bool HasIgnoredEquivalent(const ProtocolHandler& handler) const;
152 159
153 // Causes the given protocol handler to not be ignored anymore. 160 // Causes the given protocol handler to not be ignored anymore.
154 void RemoveIgnoredHandler(const ProtocolHandler& handler); 161 void RemoveIgnoredHandler(const ProtocolHandler& handler);
155 162
156 // Returns true if the protocol has a default protocol handler. 163 // Returns true if the protocol has a default protocol handler.
157 bool IsHandledProtocol(const std::string& scheme) const; 164 bool IsHandledProtocol(const std::string& scheme) const;
158 165
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. 166 // Removes the given protocol handler from the registry.
164 void RemoveHandler(const ProtocolHandler& handler); 167 void RemoveHandler(const ProtocolHandler& handler);
165 168
166 // Remove the default handler for the given protocol. 169 // Remove the default handler for the given protocol.
167 void RemoveDefaultHandler(const std::string& scheme); 170 void RemoveDefaultHandler(const std::string& scheme);
168 171
169 // Returns the default handler for this protocol, or an empty handler if none 172 // Returns the default handler for this protocol, or an empty handler if none
170 // exists. 173 // exists.
171 const ProtocolHandler& GetHandlerFor(const std::string& scheme) const; 174 const ProtocolHandler& GetHandlerFor(const std::string& scheme) const;
172 175
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 176 // Puts this registry in the enabled state - registered protocol handlers
178 // will handle requests. 177 // will handle requests.
179 void Enable(); 178 void Enable();
180 179
181 // Puts this registry in the disabled state - registered protocol handlers 180 // Puts this registry in the disabled state - registered protocol handlers
182 // will not handle requests. 181 // will not handle requests.
183 void Disable(); 182 void Disable();
184 183
185 // This is called by the UI thread when the system is shutting down. This 184 // 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. 185 // does finalization which must be done on the UI thread.
187 void Finalize(); 186 virtual void Shutdown() OVERRIDE;
188 187
189 // Registers the preferences that we store registered protocol handlers in. 188 // Registers the preferences that we store registered protocol handlers in.
190 static void RegisterPrefs(PrefService* prefService); 189 static void RegisterPrefs(PrefService* prefService);
191 190
192 bool enabled() const { return enabled_; } 191 bool enabled() const { return enabled_; }
193 192
194 // Add a predefined protocol handler. This has to be called before the first 193 // Add a predefined protocol handler. This has to be called before the first
195 // load command was issued, otherwise the command will be ignored. 194 // load command was issued, otherwise the command will be ignored.
196 void AddPredefinedHandler(const ProtocolHandler& handler); 195 void AddPredefinedHandler(const ProtocolHandler& handler);
197 196
198 private: 197 private:
199 friend class base::DeleteHelper<ProtocolHandlerRegistry>; 198 friend class base::DeleteHelper<ProtocolHandlerRegistry>;
200 friend struct content::BrowserThread::DeleteOnThread< 199 friend struct content::BrowserThread::DeleteOnThread<
201 content::BrowserThread::IO>; 200 content::BrowserThread::IO>;
202 friend class ProtocolHandlerRegistryTest; 201 friend class ProtocolHandlerRegistryTest;
203 friend class RegisterProtocolHandlerBrowserTest; 202 friend class RegisterProtocolHandlerBrowserTest;
204 203
205 ~ProtocolHandlerRegistry(); 204 // Forward declaration of our internal implementation classes.
205 class Core;
206 class IOURLInterceptor;
207
208 virtual ~ProtocolHandlerRegistry();
206 209
207 // Puts the given handler at the top of the list of handlers for its 210 // Puts the given handler at the top of the list of handlers for its
208 // protocol. 211 // protocol.
209 void PromoteHandler(const ProtocolHandler& handler); 212 void PromoteHandler(const ProtocolHandler& handler);
210 213
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. 214 // Saves a user's registered protocol handlers.
228 void Save(); 215 void Save();
229 216
230 // Returns a pointer to the list of handlers registered for the given scheme, 217 // Returns a pointer to the list of handlers registered for the given scheme,
231 // or NULL if there are none. 218 // or NULL if there are none.
232 const ProtocolHandlerList* GetHandlerList(const std::string& scheme) const; 219 const ProtocolHandlerList* GetHandlerList(const std::string& scheme) const;
233 220
234 // Makes this ProtocolHandler the default handler for its protocol. 221 // Makes this ProtocolHandler the default handler for its protocol.
235 void SetDefault(const ProtocolHandler& handler); 222 void SetDefault(const ProtocolHandler& handler);
236 223
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 // The Profile that owns this ProtocolHandlerRegistry. 258 // The Profile that owns this ProtocolHandlerRegistry.
272 Profile* profile_; 259 Profile* profile_;
273 260
274 // The Delegate that registers / deregisters external handlers on our behalf. 261 // The Delegate that registers / deregisters external handlers on our behalf.
275 scoped_ptr<Delegate> delegate_; 262 scoped_ptr<Delegate> delegate_;
276 263
277 // If false then registered protocol handlers will not be used to handle 264 // If false then registered protocol handlers will not be used to handle
278 // requests. 265 // requests.
279 bool enabled_; 266 bool enabled_;
280 267
281 // Copy of enabled_ that is only accessed on the IO thread.
282 bool enabled_io_;
283
284 // Whether or not we are loading. 268 // Whether or not we are loading.
285 bool is_loading_; 269 bool is_loading_;
286 270
287 // When the table gets loaded this flag will be set and any further calls to 271 // When the table gets loaded this flag will be set and any further calls to
288 // AddPredefinedHandler will be rejected. 272 // AddPredefinedHandler will be rejected.
289 bool is_loaded_; 273 bool is_loaded_;
290 274
275 scoped_refptr<Core> core_;
276
291 DefaultClientObserverList default_client_observers_; 277 DefaultClientObserverList default_client_observers_;
292 278
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); 279 DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry);
297 }; 280 };
298 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ 281 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698