protocol_handler_registry.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/sequenced_task_runner_helpers.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/shell_integration.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/custom_handlers/protocol_handler.h"
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_job.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_job_factory.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace user_prefs {
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PrefRegistrySyncable;
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is where handlers for protocols registered with
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// navigator.registerProtocolHandler() are registered. Each Profile owns an
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// instance of this class, which is initialized on browser start through
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Profile::InitRegisteredProtocolHandlers(), and they should be the only
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// instances of this class.
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class ProtocolHandlerRegistry : public KeyedService {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Provides notification of when the OS level user agent settings
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // are changed.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class DefaultClientObserver
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : public ShellIntegration::DefaultWebClientObserver {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    explicit DefaultClientObserver(ProtocolHandlerRegistry* registry);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~DefaultClientObserver();
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Get response from the worker regarding whether Chrome is the default
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // handler for the protocol.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void SetDefaultWebClientUIState(
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ShellIntegration::DefaultWebClientUIState state) OVERRIDE;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual bool IsInteractiveSetDefaultPermitted() OVERRIDE;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Give the observer a handle to the worker, so we can find out the protocol
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // when we're called and also tell the worker if we get deleted.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void SetWorker(ShellIntegration::DefaultProtocolClientWorker* worker);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   protected:
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ShellIntegration::DefaultProtocolClientWorker* worker_;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual bool IsOwnedByWorker() OVERRIDE;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This is a raw pointer, not reference counted, intentionally. In general
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // subclasses of DefaultWebClientObserver are not able to be refcounted
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // e.g. the browser options page
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProtocolHandlerRegistry* registry_;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(DefaultClientObserver);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |Delegate| provides an interface for interacting asynchronously
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with the underlying OS for the purposes of registering Chrome
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // as the default handler for specific protocols.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Delegate {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~Delegate();
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void RegisterExternalHandler(const std::string& protocol);
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void DeregisterExternalHandler(const std::string& protocol);
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual bool IsExternalHandlerRegistered(const std::string& protocol);
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker(
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ShellIntegration::DefaultWebClientObserver* observer,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        const std::string& protocol);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual DefaultClientObserver* CreateShellObserver(
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ProtocolHandlerRegistry* registry);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void RegisterWithOSAsDefaultClient(
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        const std::string& protocol,
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ProtocolHandlerRegistry* registry);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Forward declaration of the internal implementation class.
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class IOThreadDelegate;
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // JobInterceptorFactory intercepts URLRequestJob creation for URLRequests the
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ProtocolHandlerRegistry is registered to handle.  When no handler is
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // registered, the URLRequest is passed along to the chained
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // URLRequestJobFactory (set with |JobInterceptorFactory::Chain|).
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // JobInterceptorFactory's are created via
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |ProtocolHandlerRegistry::CreateJobInterceptorFactory|.
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class JobInterceptorFactory : public net::URLRequestJobFactory {
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   public:
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // |io_thread_delegate| is used to perform actual job creation work.
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    explicit JobInterceptorFactory(IOThreadDelegate* io_thread_delegate);
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    virtual ~JobInterceptorFactory();
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // |job_factory| is set as the URLRequestJobFactory where requests are
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // forwarded if JobInterceptorFactory decides to pass on them.
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    void Chain(scoped_ptr<net::URLRequestJobFactory> job_factory);
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // URLRequestJobFactory implementation.
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const std::string& scheme,
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        net::URLRequest* request,
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        net::NetworkDelegate* network_delegate) const OVERRIDE;
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE;
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    virtual bool IsHandledURL(const GURL& url) const OVERRIDE;
117b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE;
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   private:
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // When JobInterceptorFactory decides to pass on particular requests,
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // they're forwarded to the chained URLRequestJobFactory, |job_factory_|.
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<URLRequestJobFactory> job_factory_;
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // |io_thread_delegate_| performs the actual job creation decisions by
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // mirroring the ProtocolHandlerRegistry on the IO thread.
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_refptr<IOThreadDelegate> io_thread_delegate_;
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(JobInterceptorFactory);
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::map<std::string, ProtocolHandler> ProtocolHandlerMap;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::vector<ProtocolHandler> ProtocolHandlerList;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::map<std::string, ProtocolHandlerList> ProtocolHandlerMultiMap;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::vector<DefaultClientObserver*> DefaultClientObserverList;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a new instance. Assumes ownership of |delegate|.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerRegistry(Profile* profile, Delegate* delegate);
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ProtocolHandlerRegistry();
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns a net::URLRequestJobFactory suitable for use on the IO thread, but
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is initialized on the UI thread.
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<JobInterceptorFactory> CreateJobInterceptorFactory();
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when a site tries to register as a protocol handler. If the request
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // can be handled silently by the registry - either to ignore the request
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or to update an existing handler - the request will succeed. If this
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // function returns false the user needs to be prompted for confirmation.
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SilentlyHandleRegisterHandlerRequest(const ProtocolHandler& handler);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the user accepts the registration of a given protocol handler.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnAcceptRegisterProtocolHandler(const ProtocolHandler& handler);
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the user denies the registration of a given protocol handler.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnDenyRegisterProtocolHandler(const ProtocolHandler& handler);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the user indicates that they don't want to be asked about the
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // given protocol handler again.
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnIgnoreRegisterProtocolHandler(const ProtocolHandler& handler);
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes all handlers that have the same origin and protocol as the given
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // one and installs the given handler. Returns true if any protocol handlers
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // were replaced.
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AttemptReplace(const ProtocolHandler& handler);
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a list of protocol handlers that can be replaced by the given
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // handler.
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerList GetReplacedHandlers(const ProtocolHandler& handler) const;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Clears the default for the provided protocol.
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ClearDefault(const std::string& scheme);
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this handler is the default handler for its protocol.
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsDefault(const ProtocolHandler& handler) const;
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes default protocol settings and loads them from prefs.
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This method must be called to complete initialization of the
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // registry after creation, and prior to use.
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InitProtocolSettings();
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the offset in the list of handlers for a protocol of the default
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // handler for that protocol.
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int GetHandlerIndex(const std::string& scheme) const;
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the list of protocol handlers for the given scheme.
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerList GetHandlersFor(const std::string& scheme) const;
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the list of ignored protocol handlers.
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerList GetIgnoredHandlers();
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Yields a list of the protocols that have handlers registered in this
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // registry.
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetRegisteredProtocols(std::vector<std::string>* output) const;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if we allow websites to register handlers for the given
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // scheme.
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CanSchemeBeOverridden(const std::string& scheme) const;
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if an identical protocol handler has already been registered.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsRegistered(const ProtocolHandler& handler) const;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if an identical protocol handler is being ignored.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsIgnored(const ProtocolHandler& handler) const;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if an equivalent protocol handler has already been registered.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasRegisteredEquivalent(const ProtocolHandler& handler) const;
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if an equivalent protocol handler is being ignored.
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasIgnoredEquivalent(const ProtocolHandler& handler) const;
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Causes the given protocol handler to not be ignored anymore.
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveIgnoredHandler(const ProtocolHandler& handler);
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the protocol has a default protocol handler.
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsHandledProtocol(const std::string& scheme) const;
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the given protocol handler from the registry.
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveHandler(const ProtocolHandler& handler);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Remove the default handler for the given protocol.
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveDefaultHandler(const std::string& scheme);
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the default handler for this protocol, or an empty handler if none
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // exists.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ProtocolHandler& GetHandlerFor(const std::string& scheme) const;
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Puts this registry in the enabled state - registered protocol handlers
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will handle requests.
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Enable();
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Puts this registry in the disabled state - registered protocol handlers
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will not handle requests.
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Disable();
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is called by the UI thread when the system is shutting down. This
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // does finalization which must be done on the UI thread.
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Shutdown() OVERRIDE;
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Registers the preferences that we store registered protocol handlers in.
2387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool enabled() const { return enabled_; }
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add a predefined protocol handler. This has to be called before the first
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // load command was issued, otherwise the command will be ignored.
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddPredefinedHandler(const ProtocolHandler& handler);
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class base::DeleteHelper<ProtocolHandlerRegistry>;
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend struct content::BrowserThread::DeleteOnThread<
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::BrowserThread::IO>;
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for access to InstallDefaultsForChromeOS
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class ProtocolHandlerRegistryFactory;
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class ProtocolHandlerRegistryTest;
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class RegisterProtocolHandlerBrowserTest;
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Puts the given handler at the top of the list of handlers for its
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // protocol.
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void PromoteHandler(const ProtocolHandler& handler);
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Saves a user's registered protocol handlers.
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Save();
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a pointer to the list of handlers registered for the given scheme,
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or NULL if there are none.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ProtocolHandlerList* GetHandlerList(const std::string& scheme) const;
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Install default protocol handlers for chromeos which must be done
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // prior to calling InitProtocolSettings.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InstallDefaultsForChromeOS();
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Makes this ProtocolHandler the default handler for its protocol.
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetDefault(const ProtocolHandler& handler);
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Insert the given ProtocolHandler into the registry.
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InsertHandler(const ProtocolHandler& handler);
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a JSON list of protocol handlers. The caller is responsible for
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // deleting this Value.
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::Value* EncodeRegisteredHandlers();
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a JSON list of ignored protocol handlers. The caller is
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // responsible for deleting this Value.
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::Value* EncodeIgnoredHandlers();
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sends a notification of the given type to the NotificationService.
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void NotifyChanged();
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Registers a new protocol handler.
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RegisterProtocolHandler(const ProtocolHandler& handler);
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the DictionaryValues stored under the given pref name that are valid
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ProtocolHandler values.
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<const base::DictionaryValue*> GetHandlersFromPref(
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const char* pref_name) const;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ignores future requests to register the given protocol handler.
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void IgnoreProtocolHandler(const ProtocolHandler& handler);
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Map from protocols (strings) to protocol handlers.
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerMultiMap protocol_handlers_;
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Protocol handlers that the user has told us to ignore.
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerList ignored_protocol_handlers_;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Protocol handlers that are the defaults for a given protocol.
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerMap default_handlers_;
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The Profile that owns this ProtocolHandlerRegistry.
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile_;
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The Delegate that registers / deregisters external handlers on our behalf.
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<Delegate> delegate_;
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If false then registered protocol handlers will not be used to handle
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // requests.
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool enabled_;
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether or not we are loading.
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_loading_;
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // When the table gets loaded this flag will be set and any further calls to
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AddPredefinedHandler will be rejected.
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_loaded_;
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Copy of registry data for use on the IO thread. Changes to the registry
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // are posted to the IO thread where updates are applied to this object.
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<IOThreadDelegate> io_thread_delegate_;
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DefaultClientObserverList default_client_observers_;
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerRegistry);
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_CUSTOM_HANDLERS_PROTOCOL_HANDLER_REGISTRY_H_
335