• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/memory/ref_counted.h"
14 #include "base/values.h"
15 #include "chrome/browser/custom_handlers/protocol_handler.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "net/url_request/url_request.h"
18 #include "net/url_request/url_request_job.h"
19 
20 
21 
22 // This is where handlers for protocols registered with
23 // navigator.registerProtocolHandler() are registered. Each Profile owns an
24 // instance of this class, which is initialized on browser start through
25 // Profile::InitRegisteredProtocolHandlers(), and they should be the only
26 // instances of this class.
27 
28 class ProtocolHandlerRegistry
29     : public base::RefCountedThreadSafe<ProtocolHandlerRegistry> {
30  public:
31   explicit ProtocolHandlerRegistry(Profile* profile);
32 
33   // Called when the user accepts the registration of a given protocol handler.
34   void OnAcceptRegisterProtocolHandler(ProtocolHandler* handler);
35 
36   // Called when the user denies the registration of a given protocol handler.
37   void OnDenyRegisterProtocolHandler(ProtocolHandler* handler);
38 
39   // Loads a user's registered protocol handlers.
40   void Load();
41 
42   // Saves a user's registered protocol handlers.
43   void Save();
44 
45   // Returns the handler for this protocol.
46   ProtocolHandler* GetHandlerFor(const std::string& scheme) const;
47 
48   // Returns true if we allow websites to register handlers for the given
49   // scheme.
50   bool CanSchemeBeOverridden(const std::string& scheme) const;
51 
52   // Returns true if an identical protocol handler has already been registered.
53   bool IsAlreadyRegistered(const ProtocolHandler* handler) const;
54 
55   // URLRequestFactory for use with URLRequest::RegisterProtocolFactory().
56   // Redirects any URLRequests for which there is a matching protocol handler.
57   static net::URLRequestJob* Factory(net::URLRequest* request,
58                                      const std::string& scheme);
59 
60   // Registers the preferences that we store registered protocol handlers in.
61   static void RegisterPrefs(PrefService* prefService);
62 
63   // Creates a URL request job for the given request if there is a matching
64   // protocol handler, returns NULL otherwise.
65   net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const;
66 
67  private:
68   typedef std::map<std::string, ProtocolHandler*> ProtocolHandlerMap;
69 
70   friend class base::RefCountedThreadSafe<ProtocolHandlerRegistry>;
71   ~ProtocolHandlerRegistry();
72 
73   // Returns a JSON dictionary of protocols to protocol handlers. The caller is
74   // responsible for deleting this Value.
75   Value* Encode();
76 
77   // Registers a new protocol handler.
78   void RegisterProtocolHandler(ProtocolHandler* handler);
79 
80   // Registers a new protocol handler from a JSON dictionary.
81   void RegisterHandlerFromValue(const DictionaryValue* value);
82 
83   // Map from protocols (strings) to protocol handlers.
84   ProtocolHandlerMap protocolHandlers_;
85 
86   // The Profile that owns this ProtocolHandlerRegistry.
87   Profile* profile_;
88 
89   DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry);
90 };
91 #endif  // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_
92 
93