1 // Copyright 2015 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_SHELL_APPLICATION_INSTANCE_H_
6 #define MOJO_SHELL_APPLICATION_INSTANCE_H_
10 #include "base/callback.h"
11 #include "mojo/application/public/interfaces/application.mojom.h"
12 #include "mojo/application/public/interfaces/shell.mojom.h"
13 #include "mojo/public/cpp/bindings/binding.h"
14 #include "mojo/shell/identity.h"
19 // TODO(beng): upstream this into mojo repo, array.h so it can be shared with
20 // application_impl.cc.
21 // A |TypeConverter| that will create an |std::set<E>| containing a copy of
22 // the contents of an |Array<T>|, using |TypeConverter<E, T>| to copy each
23 // element. If the input array is null, the output set will be empty.
24 template <typename E
, typename T
>
25 struct TypeConverter
<std::set
<E
>, Array
<T
>> {
26 static std::set
<E
> Convert(const Array
<T
>& input
) {
28 if (!input
.is_null()) {
29 for (size_t i
= 0; i
< input
.size(); ++i
)
30 result
.insert(TypeConverter
<E
, T
>::Convert(input
[i
]));
36 template <typename T
, typename E
>
37 struct TypeConverter
<Array
<T
>, std::set
<E
>> {
38 static Array
<T
> Convert(const std::set
<E
>& input
) {
41 result
.push_back(TypeConverter
<T
, E
>::Convert(i
));
48 class ApplicationManager
;
50 // Encapsulates a connection to an instance of an application, tracked by the
51 // shell's ApplicationManager.
52 class ApplicationInstance
: public Shell
{
54 using AllowedInterfaces
= std::set
<std::string
>;
55 using CapabilityFilter
= std::map
<std::string
, AllowedInterfaces
>;
57 ApplicationInstance(ApplicationPtr application
,
58 ApplicationManager
* manager
,
59 const Identity
& resolved_identity
,
60 const CapabilityFilter
& filter
,
61 const base::Closure
& on_application_end
);
63 ~ApplicationInstance() override
;
65 void InitializeApplication();
67 void ConnectToClient(ApplicationInstance
* originator
,
68 const GURL
& requested_url
,
69 const GURL
& requestor_url
,
70 InterfaceRequest
<ServiceProvider
> services
,
71 ServiceProviderPtr exposed_services
,
72 CapabilityFilterPtr filter
);
74 // Returns the set of interfaces this application instance is allowed to see
75 // from an instance with |identity|.
76 AllowedInterfaces
GetAllowedInterfaces(const Identity
& identity
) const;
78 Application
* application() { return application_
.get(); }
79 const Identity
& identity() const { return identity_
; }
80 base::Closure
on_application_end() const { return on_application_end_
; }
83 // Shell implementation:
84 void ConnectToApplication(URLRequestPtr app_request
,
85 InterfaceRequest
<ServiceProvider
> services
,
86 ServiceProviderPtr exposed_services
,
87 CapabilityFilterPtr filter
) override
;
88 void QuitApplication() override
;
90 void CallAcceptConnection(ApplicationInstance
* originator
,
92 InterfaceRequest
<ServiceProvider
> services
,
93 ServiceProviderPtr exposed_services
,
94 const GURL
& requested_url
);
96 void OnConnectionError();
98 void OnQuitRequestedResult(bool can_quit
);
100 struct QueuedClientRequest
{
101 QueuedClientRequest();
102 ~QueuedClientRequest();
103 ApplicationInstance
* originator
;
106 InterfaceRequest
<ServiceProvider
> services
;
107 ServiceProviderPtr exposed_services
;
108 CapabilityFilterPtr filter
;
111 ApplicationManager
* const manager_
;
112 const Identity identity_
;
113 const CapabilityFilter filter_
;
114 const bool allow_any_application_
;
115 base::Closure on_application_end_
;
116 ApplicationPtr application_
;
117 Binding
<Shell
> binding_
;
118 bool queue_requests_
;
119 std::vector
<QueuedClientRequest
*> queued_client_requests_
;
121 DISALLOW_COPY_AND_ASSIGN(ApplicationInstance
);
127 #endif // MOJO_SHELL_APPLICATION_INSTANCE_H_