Revert of Adds back some debugging code (https://codereview.chromium.org/257023002/)
[chromium-blink-merge.git] / mojo / service_manager / service_manager.h
blob40cb8cdb1fea225c3e8f66e2d0ba23b93f16087a
1 // Copyright 2013 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.
5 #ifndef MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_
6 #define MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "mojo/public/interfaces/shell/shell.mojom.h"
15 #include "mojo/service_manager/service_loader.h"
16 #include "mojo/service_manager/service_manager_export.h"
17 #include "url/gurl.h"
19 namespace content {
20 class MojoTest;
23 namespace mojo {
25 class MOJO_SERVICE_MANAGER_EXPORT ServiceManager {
26 public:
27 // API for testing.
28 class MOJO_SERVICE_MANAGER_EXPORT TestAPI {
29 private:
30 friend class ServiceManagerTest;
31 friend class content::MojoTest;
33 explicit TestAPI(ServiceManager* manager) : manager_(manager) {}
34 // Returns true if the shared instance has been created.
35 static bool HasCreatedInstance();
36 // Returns true if there is a ServiceFactory for this URL.
37 bool HasFactoryForURL(const GURL& url) const;
39 ServiceManager* manager_;
42 // Interface class for debugging only.
43 class Interceptor {
44 public:
45 virtual ~Interceptor() {}
46 // Called when ServiceManager::Connect is called.
47 virtual ScopedMessagePipeHandle OnConnectToClient(
48 const GURL& url, ScopedMessagePipeHandle handle) = 0;
51 ServiceManager();
52 ~ServiceManager();
54 // Returns a shared instance, creating it if necessary.
55 static ServiceManager* GetInstance();
57 // Loads a service if necessary and establishes a new client connection.
58 void Connect(const GURL& url, ScopedMessagePipeHandle client_handle);
60 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
61 // or SetLoaderForScheme().
62 void set_default_loader(scoped_ptr<ServiceLoader> loader) {
63 default_loader_ = loader.Pass();
65 // Sets a Loader to be used for a specific url.
66 void SetLoaderForURL(scoped_ptr<ServiceLoader> loader, const GURL& url);
67 // Sets a Loader to be used for a specific url scheme.
68 void SetLoaderForScheme(scoped_ptr<ServiceLoader> loader,
69 const std::string& scheme);
70 // Allows to interpose a debugger to service connections.
71 void SetInterceptor(Interceptor* interceptor);
73 private:
74 class ServiceFactory;
75 typedef std::map<std::string, ServiceLoader*> SchemeToLoaderMap;
76 typedef std::map<GURL, ServiceLoader*> URLToLoaderMap;
77 typedef std::map<GURL, ServiceFactory*> URLToServiceFactoryMap;
79 // Returns the Loader to use for a url (using default if not overridden.)
80 // The preference is to use a loader that's been specified for an url first,
81 // then one that's been specified for a scheme, then the default.
82 ServiceLoader* GetLoaderForURL(const GURL& url);
84 // Removes a ServiceFactory when it no longer has any connections.
85 void OnServiceFactoryError(ServiceFactory* service_factory);
87 // Loader management.
88 URLToLoaderMap url_to_loader_;
89 SchemeToLoaderMap scheme_to_loader_;
90 scoped_ptr<ServiceLoader> default_loader_;
91 Interceptor* interceptor_;
93 URLToServiceFactoryMap url_to_service_factory_;
94 DISALLOW_COPY_AND_ASSIGN(ServiceManager);
97 } // namespace mojo
99 #endif // MOJO_SERVICE_MANAGER_SERVICE_MANAGER_H_