Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ | |
| 6 #define CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 #include <map> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/ref_counted.h" | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "net/url_request/url_request.h" | |
| 17 #include "net/url_request/url_request_job.h" | |
| 18 | |
| 19 | |
| 20 // ProtocolHandler ------------------------------------------------------------- | |
| 21 | |
| 22 // A single tuple of (protocol, url, title) that indicates how URLs of the | |
| 23 // given protocol should be rewritten to be handled. | |
| 24 | |
| 25 class ProtocolHandler { | |
| 26 public: | |
| 27 static ProtocolHandler* CreateProtocolHandler(const std::string& protocol, | |
| 28 const GURL& url, | |
| 29 const string16& title); | |
| 30 static ProtocolHandler* CreateProtocolHandler(const DictionaryValue* value); | |
| 31 GURL TranslateUrl(const GURL& url); | |
| 32 Value* Encode(); | |
|
tony
2011/02/14 23:40:07
Nit: Maybe make it clear that the caller should de
koz (OOO until 15th September)
2011/02/15 03:37:27
Done.
| |
| 33 std::string protocol() const { return protocol_; } | |
| 34 GURL url() const { return url_;} | |
| 35 string16 title() const { return title_; } | |
| 36 bool operator==(const ProtocolHandler &other) const; | |
| 37 | |
| 38 private: | |
| 39 ProtocolHandler(const std::string& protocol, | |
| 40 const GURL& url, | |
| 41 const string16& title); | |
| 42 const std::string protocol_; | |
| 43 const GURL url_; | |
| 44 const string16 title_; | |
| 45 }; | |
| 46 | |
| 47 typedef std::map<std::string, ProtocolHandler*> ProtocolHandlerMap; | |
| 48 | |
| 49 // ProtocolHandlerRegistry ----------------------------------------------------- | |
| 50 | |
| 51 // This is where handlers for protocols registered with | |
| 52 // navigator.registerProtocolHandler() are registered. Each Profile owns an | |
| 53 // instance of this class, which is initialized on browser start through | |
| 54 // Profile::InitRegisteredProtocolHandlers(), and they should be the only | |
| 55 // instances of this class. | |
| 56 | |
| 57 class ProtocolHandlerRegistry : public base::RefCounted<ProtocolHandlerRegistry> { | |
| 58 public: | |
| 59 explicit ProtocolHandlerRegistry(Profile* profile); | |
| 60 | |
| 61 // Called when the user accepts the registration of a given protocol handler. | |
| 62 void OnAcceptRegisterProtocolHandler(ProtocolHandler* handler); | |
| 63 | |
| 64 // Called when the user denies the registration of a given protocol handler. | |
| 65 void OnDenyRegisterProtocolHandler(ProtocolHandler* handler); | |
| 66 | |
| 67 // Loads a user's registered protocol handlers. | |
| 68 void Load(); | |
| 69 | |
| 70 // Saves a user's registered protocol handlers. | |
| 71 void Save(); | |
| 72 | |
| 73 // Returns the handler for this protocol. | |
| 74 ProtocolHandler* GetHandlerFor(const std::string& scheme) const; | |
| 75 | |
| 76 bool IsAlreadyRegistered(const ProtocolHandler* handler) const; | |
| 77 | |
| 78 // URLRequestFactory for use with URLRequest::RegisterProtocolFactory(). | |
| 79 // Redirects any URLRequests for which there is a matching protocol handler. | |
| 80 static net::URLRequestJob* Factory(net::URLRequest* request, | |
| 81 const std::string& scheme); | |
| 82 | |
| 83 // Registers the preferences that we store registered protocol handlers in. | |
| 84 static void RegisterPrefs(PrefService* prefService); | |
| 85 | |
| 86 private: | |
| 87 // Returns a JSON dictionary of protocols to protocol handlers. The caller is | |
| 88 // responsible for deleting this Value. | |
| 89 Value* Encode(); | |
| 90 | |
| 91 // Registers a new protocol handler. | |
| 92 void RegisterProtocolHandler(ProtocolHandler* handler); | |
| 93 | |
| 94 // Creates a URL request job for the given request if there is a matching | |
| 95 // protocol handler, returns NULL otherwise. | |
| 96 net::URLRequestJob* CreateJob(net::URLRequest* request, | |
| 97 const std::string& scheme) const; | |
| 98 | |
| 99 // Registers a new protocol handler from a JSON dictionary. | |
| 100 void RegisterHandlerFromValue(const DictionaryValue* value); | |
| 101 | |
| 102 // Map from protocols (strings) to protocol handlers. | |
| 103 ProtocolHandlerMap protocolHandlers_; | |
| 104 | |
| 105 // The Profile that owns this ProtocolHandlerRegistry. | |
| 106 Profile* profile_; | |
| 107 | |
| 108 friend class base::RefCounted<ProtocolHandlerRegistry>; | |
| 109 ~ProtocolHandlerRegistry() {} | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry); | |
| 112 }; | |
| 113 #endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ | |
| 114 | |
| OLD | NEW |