OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/custom_handlers/protocol_handler.h" | 16 #include "chrome/browser/custom_handlers/protocol_handler.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "content/common/notification_service.h" |
18 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
19 #include "net/url_request/url_request_job.h" | 20 #include "net/url_request/url_request_job.h" |
20 | 21 |
21 // This is where handlers for protocols registered with | 22 // This is where handlers for protocols registered with |
22 // navigator.registerProtocolHandler() are registered. Each Profile owns an | 23 // navigator.registerProtocolHandler() are registered. Each Profile owns an |
23 // instance of this class, which is initialized on browser start through | 24 // instance of this class, which is initialized on browser start through |
24 // Profile::InitRegisteredProtocolHandlers(), and they should be the only | 25 // Profile::InitRegisteredProtocolHandlers(), and they should be the only |
25 // instances of this class. | 26 // instances of this class. |
26 | 27 |
27 class ProtocolHandlerRegistry | 28 class ProtocolHandlerRegistry |
28 : public base::RefCountedThreadSafe<ProtocolHandlerRegistry> { | 29 : public base::RefCountedThreadSafe<ProtocolHandlerRegistry> { |
29 public: | 30 public: |
30 // TODO(koz): Refactor this to eliminate the unnecessary virtuals. All that | 31 // TODO(koz): Refactor this to eliminate the unnecessary virtuals. All that |
31 // should be needed is a way to ensure that the list of websafe protocols is | 32 // should be needed is a way to ensure that the list of websafe protocols is |
32 // updated. | 33 // updated. |
33 class Delegate { | 34 class Delegate { |
34 public: | 35 public: |
35 virtual ~Delegate(); | 36 virtual ~Delegate(); |
36 virtual void RegisterExternalHandler(const std::string& protocol); | 37 virtual void RegisterExternalHandler(const std::string& protocol); |
37 virtual void DeregisterExternalHandler(const std::string& protocol); | 38 virtual void DeregisterExternalHandler(const std::string& protocol); |
38 virtual bool IsExternalHandlerRegistered(const std::string& protocol); | 39 virtual bool IsExternalHandlerRegistered(const std::string& protocol); |
39 }; | 40 }; |
40 | 41 |
| 42 typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap; |
| 43 typedef std::vector<ProtocolHandler> ProtocolHandlerList; |
| 44 typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap; |
| 45 |
41 ProtocolHandlerRegistry(Profile* profile, Delegate* delegate); | 46 ProtocolHandlerRegistry(Profile* profile, Delegate* delegate); |
42 ~ProtocolHandlerRegistry(); | 47 ~ProtocolHandlerRegistry(); |
43 | 48 |
44 // Called when the user accepts the registration of a given protocol handler. | 49 // Called when the user accepts the registration of a given protocol handler. |
45 void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler); | 50 void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler); |
46 | 51 |
47 // Called when the user denies the registration of a given protocol handler. | 52 // Called when the user denies the registration of a given protocol handler. |
48 void OnDenyRegisterProtocolHandler(const ProtocolHandler& handler); | 53 void OnDenyRegisterProtocolHandler(const ProtocolHandler& handler); |
49 | 54 |
50 // Called when the user indicates that they don't want to be asked about the | 55 // Called when the user indicates that they don't want to be asked about the |
51 // given protocol handler again. | 56 // given protocol handler again. |
52 void OnIgnoreRegisterProtocolHandler(const ProtocolHandler& handler); | 57 void OnIgnoreRegisterProtocolHandler(const ProtocolHandler& handler); |
53 | 58 |
54 // Makes this ProtocolHandler the default handler for its protocol. | 59 // Makes this ProtocolHandler the default handler for its protocol. |
55 void SetDefault(const ProtocolHandler& handler); | 60 void SetDefault(const ProtocolHandler& handler); |
56 | 61 |
57 // Clears the default for the provided protocol. | 62 // Clears the default for the provided protocol. |
58 void ClearDefault(const std::string& scheme); | 63 void ClearDefault(const std::string& scheme); |
59 | 64 |
60 // Returns true if this handler is the default handler for its protocol. | 65 // Returns true if this handler is the default handler for its protocol. |
61 bool IsDefault(const ProtocolHandler& handler) const; | 66 bool IsDefault(const ProtocolHandler& handler) const; |
62 | 67 |
63 // Returns true if the given protocol has a default handler associated with | 68 // Returns true if the given protocol has a default handler associated with |
64 // it. | 69 // it. |
65 bool HasDefault(const std::string& scheme) const; | 70 bool HasDefault(const std::string& scheme) const; |
66 | 71 |
67 // Returns true if there is a handler registered for the given protocol. | |
68 bool HasHandler(const std::string& scheme); | |
69 | |
70 // Loads a user's registered protocol handlers. | 72 // Loads a user's registered protocol handlers. |
71 void Load(); | 73 void Load(); |
72 | 74 |
73 // Saves a user's registered protocol handlers. | 75 // Saves a user's registered protocol handlers. |
74 void Save(); | 76 void Save(); |
75 | 77 |
76 // Returns the default handler for this protocol, or an empty handler if none | 78 // Returns the default handler for this protocol, or an empty handler if none |
77 // exists. | 79 // exists. |
78 const ProtocolHandler& GetHandlerFor(const std::string& scheme) const; | 80 const ProtocolHandler& GetHandlerFor(const std::string& scheme) const; |
79 | 81 |
| 82 // Returns the offset in the list of handlers for a protocol of the default |
| 83 // handler for that protocol. |
| 84 int GetHandlerIndex(const std::string& scheme) const; |
| 85 |
| 86 // Get the list of protocol handlers for the given scheme. |
| 87 const ProtocolHandlerList* GetHandlersFor(const std::string& scheme) const; |
| 88 |
80 // Yields a list of the protocols handled by this registry. | 89 // Yields a list of the protocols handled by this registry. |
81 void GetHandledProtocols(std::vector<std::string>* output) const; | 90 void GetHandledProtocols(std::vector<std::string>* output) const; |
82 | 91 |
83 // Returns true if we allow websites to register handlers for the given | 92 // Returns true if we allow websites to register handlers for the given |
84 // scheme. | 93 // scheme. |
85 bool CanSchemeBeOverridden(const std::string& scheme) const; | 94 bool CanSchemeBeOverridden(const std::string& scheme) const; |
86 | 95 |
87 // Returns true if an identical protocol handler has already been registered. | 96 // Returns true if an identical protocol handler has already been registered. |
88 bool IsRegistered(const ProtocolHandler& handler); | 97 bool IsRegistered(const ProtocolHandler& handler) const; |
89 | 98 |
90 // Returns true if the protocol handler is being ignored. | 99 // Returns true if the protocol handler is being ignored. |
91 bool IsIgnored(const ProtocolHandler& handler) const; | 100 bool IsIgnored(const ProtocolHandler& handler) const; |
92 | 101 |
93 // Returns true if the protocol has a registered protocol handler. | 102 // Returns true if the protocol has a registered protocol handler. |
94 bool IsHandledProtocol(const std::string& scheme) const; | 103 bool IsHandledProtocol(const std::string& scheme) const; |
95 | 104 |
96 // Removes the given protocol handler from the registry. | 105 // Removes the given protocol handler from the registry. |
97 void RemoveHandler(const ProtocolHandler& handler); | 106 void RemoveHandler(const ProtocolHandler& handler); |
98 | 107 |
(...skipping 13 matching lines...) Expand all Loading... |
112 | 121 |
113 // Puts this registry in the disabled state - registered protocol handlers | 122 // Puts this registry in the disabled state - registered protocol handlers |
114 // will not handle requests. | 123 // will not handle requests. |
115 void Disable(); | 124 void Disable(); |
116 | 125 |
117 bool enabled() const { return enabled_; } | 126 bool enabled() const { return enabled_; } |
118 | 127 |
119 private: | 128 private: |
120 friend class base::RefCountedThreadSafe<ProtocolHandlerRegistry>; | 129 friend class base::RefCountedThreadSafe<ProtocolHandlerRegistry>; |
121 | 130 |
122 typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap; | 131 // Insert the given ProtocolHandler into the registry. |
123 typedef std::vector<ProtocolHandler> ProtocolHandlerList; | 132 void InsertHandler(const ProtocolHandler& handler); |
124 typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap; | |
125 | 133 |
126 // Returns a JSON list of protocol handlers. The caller is responsible for | 134 // Returns a JSON list of protocol handlers. The caller is responsible for |
127 // deleting this Value. | 135 // deleting this Value. |
128 Value* EncodeRegisteredHandlers(); | 136 Value* EncodeRegisteredHandlers(); |
129 | 137 |
130 // Returns a JSON list of ignored protocol handlers. The caller is | 138 // Returns a JSON list of ignored protocol handlers. The caller is |
131 // responsible for deleting this Value. | 139 // responsible for deleting this Value. |
132 Value* EncodeIgnoredHandlers(); | 140 Value* EncodeIgnoredHandlers(); |
133 | 141 |
| 142 // Sends a notification of the given type to the NotificationService. |
| 143 void NotifyChanged(); |
| 144 |
134 // Registers a new protocol handler. | 145 // Registers a new protocol handler. |
135 void RegisterProtocolHandler(const ProtocolHandler& handler); | 146 void RegisterProtocolHandler(const ProtocolHandler& handler); |
136 | 147 |
137 // Get the DictionaryValues stored under the given pref name that are valid | 148 // Get the DictionaryValues stored under the given pref name that are valid |
138 // ProtocolHandler values. | 149 // ProtocolHandler values. |
139 std::vector<const DictionaryValue*> GetHandlersFromPref( | 150 std::vector<const DictionaryValue*> GetHandlersFromPref( |
140 const char* pref_name) const; | 151 const char* pref_name) const; |
141 | 152 |
142 // Ignores future requests to register the given protocol handler. | 153 // Ignores future requests to register the given protocol handler. |
143 void IgnoreProtocolHandler(const ProtocolHandler& handler); | 154 void IgnoreProtocolHandler(const ProtocolHandler& handler); |
144 | 155 |
145 // Registers a new protocol handler from a JSON dictionary. | 156 // Registers a new protocol handler from a JSON dictionary. |
146 void RegisterHandlerFromValue(const DictionaryValue* value); | 157 void RegisterHandlerFromValue(const DictionaryValue* value); |
147 | 158 |
148 // Register | 159 // Register |
149 void IgnoreHandlerFromValue(const DictionaryValue* value); | 160 void IgnoreHandlerFromValue(const DictionaryValue* value); |
150 | 161 |
151 ProtocolHandlerList& GetHandlerListFor(const std::string& scheme); | |
152 | |
153 // Map from protocols (strings) to protocol handlers. | 162 // Map from protocols (strings) to protocol handlers. |
154 ProtocolHandlerMultiMap protocol_handlers_; | 163 ProtocolHandlerMultiMap protocol_handlers_; |
155 | 164 |
156 // Protocol handlers that the user has told us to ignore. | 165 // Protocol handlers that the user has told us to ignore. |
157 ProtocolHandlerList ignored_protocol_handlers_; | 166 ProtocolHandlerList ignored_protocol_handlers_; |
158 | 167 |
159 // Protocol handlers that are the defaults for a given protocol. | 168 // Protocol handlers that are the defaults for a given protocol. |
160 ProtocolHandlerMap default_handlers_; | 169 ProtocolHandlerMap default_handlers_; |
161 | 170 |
162 // The Profile that owns this ProtocolHandlerRegistry. | 171 // The Profile that owns this ProtocolHandlerRegistry. |
163 Profile* profile_; | 172 Profile* profile_; |
164 | 173 |
165 // The Delegate that registers / deregisters external handlers on our behalf. | 174 // The Delegate that registers / deregisters external handlers on our behalf. |
166 scoped_ptr<Delegate> delegate_; | 175 scoped_ptr<Delegate> delegate_; |
167 | 176 |
168 // If false then registered protocol handlers will not be used to handle | 177 // If false then registered protocol handlers will not be used to handle |
169 // requests. | 178 // requests. |
170 bool enabled_; | 179 bool enabled_; |
171 | 180 |
172 // Whether or not we are loading. | 181 // Whether or not we are loading. |
173 bool is_loading_; | 182 bool is_loading_; |
174 | 183 |
175 DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry); | 184 DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry); |
176 }; | 185 }; |
177 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ | 186 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ |
OLD | NEW |