1 // Copyright 2014 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_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
6 #define MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "mojo/application_manager/application_loader.h"
15 #include "mojo/application_manager/application_manager_export.h"
16 #include "mojo/public/interfaces/application/service_provider.mojom.h"
21 class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager
{
24 class MOJO_APPLICATION_MANAGER_EXPORT TestAPI
{
26 explicit TestAPI(ApplicationManager
* manager
);
29 // Returns true if the shared instance has been created.
30 static bool HasCreatedInstance();
31 // Returns true if there is a ShellImpl for this URL.
32 bool HasFactoryForURL(const GURL
& url
) const;
35 ApplicationManager
* manager_
;
37 DISALLOW_COPY_AND_ASSIGN(TestAPI
);
40 // Interface class for debugging only.
43 virtual ~Interceptor() {}
44 // Called when ApplicationManager::Connect is called.
45 virtual ServiceProviderPtr
OnConnectToClient(
47 ServiceProviderPtr service_provider
) = 0;
51 ~ApplicationManager();
53 // Returns a shared instance, creating it if necessary.
54 static ApplicationManager
* GetInstance();
56 // Loads a service if necessary and establishes a new client connection.
57 void ConnectToApplication(const GURL
& application_url
,
58 const GURL
& requestor_url
,
59 ServiceProviderPtr service_provider
);
61 template <typename Interface
>
62 inline void ConnectToService(const GURL
& application_url
,
63 InterfacePtr
<Interface
>* ptr
) {
64 ScopedMessagePipeHandle service_handle
=
65 ConnectToServiceByName(application_url
, Interface::Name_
);
66 ptr
->Bind(service_handle
.Pass());
69 ScopedMessagePipeHandle
ConnectToServiceByName(
70 const GURL
& application_url
,
71 const std::string
& interface_name
);
73 // Sets the default Loader to be used if not overridden by SetLoaderForURL()
74 // or SetLoaderForScheme().
75 void set_default_loader(scoped_ptr
<ApplicationLoader
> loader
) {
76 default_loader_
= loader
.Pass();
78 // Sets a Loader to be used for a specific url.
79 void SetLoaderForURL(scoped_ptr
<ApplicationLoader
> loader
, const GURL
& url
);
80 // Sets a Loader to be used for a specific url scheme.
81 void SetLoaderForScheme(scoped_ptr
<ApplicationLoader
> loader
,
82 const std::string
& scheme
);
83 // Allows to interpose a debugger to service connections.
84 void SetInterceptor(Interceptor
* interceptor
);
86 // Destroys all Shell-ends of connections established with Applications.
87 // Applications connected by this ApplicationManager will observe pipe errors
88 // and have a chance to shutdown.
89 void TerminateShellConnections();
92 struct ContentHandlerConnection
;
93 class LoadCallbacksImpl
;
96 typedef std::map
<std::string
, ApplicationLoader
*> SchemeToLoaderMap
;
97 typedef std::map
<GURL
, ApplicationLoader
*> URLToLoaderMap
;
98 typedef std::map
<GURL
, ShellImpl
*> URLToShellImplMap
;
99 typedef std::map
<GURL
, ContentHandlerConnection
*> URLToContentHandlerMap
;
101 void ConnectToClient(ShellImpl
* shell_impl
,
103 const GURL
& requestor_url
,
104 ServiceProviderPtr service_provider
);
106 void RegisterLoadedApplication(const GURL
& service_url
,
107 const GURL
& requestor_url
,
108 ServiceProviderPtr service_provider
,
109 ScopedMessagePipeHandle
* shell_handle
);
111 void LoadWithContentHandler(const GURL
& content_url
,
112 const GURL
& requestor_url
,
113 const GURL
& content_handler_url
,
114 URLResponsePtr content
,
115 ServiceProviderPtr service_provider
);
117 // Returns the Loader to use for a url (using default if not overridden.)
118 // The preference is to use a loader that's been specified for an url first,
119 // then one that's been specified for a scheme, then the default.
120 ApplicationLoader
* GetLoaderForURL(const GURL
& url
);
122 // Removes a ShellImpl when it encounters an error.
123 void OnShellImplError(ShellImpl
* shell_impl
);
125 // Loader management.
126 URLToLoaderMap url_to_loader_
;
127 SchemeToLoaderMap scheme_to_loader_
;
128 scoped_ptr
<ApplicationLoader
> default_loader_
;
129 Interceptor
* interceptor_
;
131 URLToShellImplMap url_to_shell_impl_
;
132 URLToContentHandlerMap url_to_content_handler_
;
134 base::WeakPtrFactory
<ApplicationManager
> weak_ptr_factory_
;
136 DISALLOW_COPY_AND_ASSIGN(ApplicationManager
);
141 #endif // MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_