Chromium Code Reviews| Index: chrome/browser/custom_handlers/protocol_handler_registry.h |
| diff --git a/chrome/browser/custom_handlers/protocol_handler_registry.h b/chrome/browser/custom_handlers/protocol_handler_registry.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7e10c82edf0a939f46f39e60f62c3e8d9683a05f |
| --- /dev/null |
| +++ b/chrome/browser/custom_handlers/protocol_handler_registry.h |
| @@ -0,0 +1,127 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ |
| +#define CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ |
| +#pragma once |
| + |
| +#include <string> |
| +#include <map> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/ref_counted.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_job.h" |
| + |
| + |
| +// ProtocolHandler ------------------------------------------------------------- |
| + |
| +// A single tuple of (protocol, url, title) that indicates how URLs of the |
| +// given protocol should be rewritten to be handled. |
| + |
| +class ProtocolHandler { |
|
jam
2011/02/15 18:48:39
this class should just go into its own file
koz (OOO until 15th September)
2011/02/16 03:37:48
Done.
|
| + public: |
| + static ProtocolHandler* CreateProtocolHandler(const std::string& protocol, |
| + const GURL& url, |
| + const string16& title); |
| + static ProtocolHandler* CreateProtocolHandler(const DictionaryValue* value); |
| + |
| + // Interpolates the given URL into the URL template of this handler. |
| + GURL TranslateUrl(const GURL& url); |
| + |
| + // Encodes this protocol handler as a Value. The caller is responsible for |
| + // deleting the returned value. |
| + Value* Encode(); |
| + |
| + std::string protocol() const { return protocol_; } |
| + GURL url() const { return url_;} |
| + string16 title() const { return title_; } |
| + |
| + bool operator==(const ProtocolHandler &other) const; |
| + |
| + private: |
| + ProtocolHandler(const std::string& protocol, |
| + const GURL& url, |
| + const string16& title); |
| + const std::string protocol_; |
| + const GURL url_; |
| + const string16 title_; |
| +}; |
| + |
| +typedef std::map<std::string, ProtocolHandler*> ProtocolHandlerMap; |
|
jam
2011/02/15 18:48:39
it's preferable that this typedef is inside a clas
koz (OOO until 15th September)
2011/02/16 03:37:48
Done.
|
| + |
| +// ProtocolHandlerRegistry ----------------------------------------------------- |
| + |
| +// This is where handlers for protocols registered with |
| +// navigator.registerProtocolHandler() are registered. Each Profile owns an |
| +// instance of this class, which is initialized on browser start through |
| +// Profile::InitRegisteredProtocolHandlers(), and they should be the only |
| +// instances of this class. |
| + |
| +class ProtocolHandlerRegistry |
| + : public base::RefCounted<ProtocolHandlerRegistry> { |
| + public: |
| + explicit ProtocolHandlerRegistry(Profile* profile); |
| + |
| + // Called when the user accepts the registration of a given protocol handler. |
| + void OnAcceptRegisterProtocolHandler(ProtocolHandler* handler); |
| + |
| + // Called when the user denies the registration of a given protocol handler. |
| + void OnDenyRegisterProtocolHandler(ProtocolHandler* handler); |
| + |
| + // Loads a user's registered protocol handlers. |
| + void Load(); |
| + |
| + // Saves a user's registered protocol handlers. |
| + void Save(); |
| + |
| + // Returns the handler for this protocol. |
| + ProtocolHandler* GetHandlerFor(const std::string& scheme) const; |
| + |
| + // Returns true if we allow websites to register handlers for the given |
| + // scheme. |
| + bool CanSchemeBeOverridden(const std::string& scheme) const; |
| + |
| + // Returns true if an identical protocol handler has already been registered. |
| + bool IsAlreadyRegistered(const ProtocolHandler* handler) const; |
| + |
| + // URLRequestFactory for use with URLRequest::RegisterProtocolFactory(). |
| + // Redirects any URLRequests for which there is a matching protocol handler. |
| + static net::URLRequestJob* Factory(net::URLRequest* request, |
| + const std::string& scheme); |
| + |
| + // Registers the preferences that we store registered protocol handlers in. |
| + static void RegisterPrefs(PrefService* prefService); |
| + |
| + private: |
| + // Returns a JSON dictionary of protocols to protocol handlers. The caller is |
| + // responsible for deleting this Value. |
| + Value* Encode(); |
| + |
| + // Registers a new protocol handler. |
| + void RegisterProtocolHandler(ProtocolHandler* handler); |
| + |
| + // Creates a URL request job for the given request if there is a matching |
| + // protocol handler, returns NULL otherwise. |
| + net::URLRequestJob* CreateJob(net::URLRequest* request, |
| + const std::string& scheme) const; |
| + |
| + // Registers a new protocol handler from a JSON dictionary. |
| + void RegisterHandlerFromValue(const DictionaryValue* value); |
| + |
| + // Map from protocols (strings) to protocol handlers. |
| + ProtocolHandlerMap protocolHandlers_; |
| + |
| + // The Profile that owns this ProtocolHandlerRegistry. |
| + Profile* profile_; |
| + |
| + friend class base::RefCounted<ProtocolHandlerRegistry>; |
| + ~ProtocolHandlerRegistry() {} |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry); |
| +}; |
| +#endif // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_ |
| + |