OLD | NEW |
| (Empty) |
1 // Copyright 2016 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. | |
4 | |
5 #ifndef MOJO_SERVICES_PACKAGE_MANAGER_PACKAGE_MANAGER_H_ | |
6 #define MOJO_SERVICES_PACKAGE_MANAGER_PACKAGE_MANAGER_H_ | |
7 | |
8 #include "base/files/file_path.h" | |
9 #include "base/memory/weak_ptr.h" | |
10 #include "base/path_service.h" | |
11 #include "base/values.h" | |
12 #include "mojo/public/cpp/bindings/binding_set.h" | |
13 #include "mojo/services/package_manager/public/interfaces/catalog.mojom.h" | |
14 #include "mojo/services/package_manager/public/interfaces/resolver.mojom.h" | |
15 #include "mojo/shell/public/cpp/interface_factory.h" | |
16 #include "mojo/shell/public/cpp/shell_client.h" | |
17 #include "mojo/shell/public/interfaces/shell_resolver.mojom.h" | |
18 #include "url/gurl.h" | |
19 | |
20 namespace package_manager { | |
21 // A set of names of interfaces that may be exposed to an application. | |
22 using AllowedInterfaces = std::set<std::string>; | |
23 // A map of allowed applications to allowed interface sets. See shell.mojom for | |
24 // more details. | |
25 using CapabilityFilter = std::map<std::string, AllowedInterfaces>; | |
26 | |
27 // Static information about an application package known to the PackageManager. | |
28 struct ApplicationInfo { | |
29 ApplicationInfo(); | |
30 ApplicationInfo(const ApplicationInfo& other); | |
31 ~ApplicationInfo(); | |
32 | |
33 std::string name; | |
34 std::string qualifier; | |
35 std::string display_name; | |
36 CapabilityFilter base_filter; | |
37 }; | |
38 | |
39 // Implemented by an object that provides storage for the application catalog | |
40 // (e.g. in Chrome, preferences). The PackageManagerImpl is the canonical owner | |
41 // of the contents of the store, so no one else must modify its contents. | |
42 class ApplicationCatalogStore { | |
43 public: | |
44 // Value is a string. | |
45 static const char kNameKey[]; | |
46 // Value is a string. | |
47 static const char kQualifierKey[]; | |
48 // Value is a string. | |
49 static const char kDisplayNameKey[]; | |
50 // Value is a dictionary that maps from the filter to a list of string | |
51 // interfaces. | |
52 static const char kCapabilitiesKey[]; | |
53 | |
54 virtual ~ApplicationCatalogStore() {} | |
55 | |
56 // Called during initialization to construct the PackageManagerImpl's catalog. | |
57 // Returns a serialized list of the apps. Each entry in the returned list | |
58 // corresponds to an app (as a dictionary). Each dictionary has a name, | |
59 // display name and capabilities. The return value is owned by the caller. | |
60 virtual const base::ListValue* GetStore() = 0; | |
61 | |
62 // Write the catalog to the store. Called when the PackageManagerImpl learns | |
63 // of a newly encountered application. | |
64 virtual void UpdateStore(scoped_ptr<base::ListValue> store) = 0; | |
65 }; | |
66 | |
67 class PackageManager | |
68 : public mojo::ShellClient, | |
69 public mojo::InterfaceFactory<mojom::Resolver>, | |
70 public mojo::InterfaceFactory<mojo::shell::mojom::ShellResolver>, | |
71 public mojo::InterfaceFactory<mojom::Catalog>, | |
72 public mojom::Resolver, | |
73 public mojo::shell::mojom::ShellResolver, | |
74 public mojom::Catalog { | |
75 public: | |
76 // If |register_schemes| is true, mojo: and exe: schemes are registered as | |
77 // "standard". | |
78 PackageManager(base::TaskRunner* blocking_pool, | |
79 scoped_ptr<ApplicationCatalogStore> catalog); | |
80 ~PackageManager() override; | |
81 | |
82 private: | |
83 using MojoNameAliasMap = | |
84 std::map<std::string, std::pair<std::string, std::string>>; | |
85 | |
86 // mojo::ShellClient: | |
87 bool AcceptConnection(mojo::Connection* connection) override; | |
88 | |
89 // mojo::InterfaceFactory<mojom::Resolver>: | |
90 void Create(mojo::Connection* connection, | |
91 mojom::ResolverRequest request) override; | |
92 | |
93 // mojo::InterfaceFactory<mojo::shell::mojom::ShellResolver>: | |
94 void Create(mojo::Connection* connection, | |
95 mojo::shell::mojom::ShellResolverRequest request) override; | |
96 | |
97 // mojo::InterfaceFactory<mojom::Catalog>: | |
98 void Create(mojo::Connection* connection, | |
99 mojom::CatalogRequest request) override; | |
100 | |
101 // mojom::Resolver: | |
102 void ResolveResponse( | |
103 mojo::URLResponsePtr response, | |
104 const ResolveResponseCallback& callback) override; | |
105 void ResolveInterfaces(mojo::Array<mojo::String> interfaces, | |
106 const ResolveInterfacesCallback& callback) override; | |
107 void ResolveMIMEType(const mojo::String& mime_type, | |
108 const ResolveMIMETypeCallback& callback) override; | |
109 void ResolveProtocolScheme( | |
110 const mojo::String& scheme, | |
111 const ResolveProtocolSchemeCallback& callback) override; | |
112 | |
113 // mojo::shell::mojom::ShellResolver: | |
114 void ResolveMojoName(const mojo::String& mojo_name, | |
115 const ResolveMojoNameCallback& callback) override; | |
116 | |
117 // mojom::Catalog: | |
118 void GetEntries(mojo::Array<mojo::String> names, | |
119 const GetEntriesCallback& callback) override; | |
120 | |
121 // Completes resolving a Mojo name from the Shell after the resolved name has | |
122 // been added to the catalog and the manifest read. | |
123 void CompleteResolveMojoName(const std::string& resolved_name, | |
124 const std::string& qualifier, | |
125 const ResolveMojoNameCallback& callback); | |
126 | |
127 bool IsNameInCatalog(const std::string& name) const; | |
128 | |
129 // Called from ResolveMojoName(). | |
130 // Attempts to load a manifest for |name|, reads it and adds its metadata to | |
131 // the catalog. | |
132 void AddNameToCatalog(const std::string& name, | |
133 const ResolveMojoNameCallback& callback); | |
134 | |
135 // Populate/serialize the catalog from/to the supplied store. | |
136 void DeserializeCatalog(); | |
137 void SerializeCatalog(); | |
138 | |
139 // Construct a catalog entry from |dictionary|. | |
140 const ApplicationInfo& DeserializeApplication( | |
141 const base::DictionaryValue* dictionary); | |
142 | |
143 GURL GetManifestURL(const std::string& name); | |
144 | |
145 // Called once the manifest has been read. |pm| may be null at this point, | |
146 // but |callback| must be run. | |
147 static void OnReadManifest(base::WeakPtr<PackageManager> pm, | |
148 const std::string& name, | |
149 const ResolveMojoNameCallback& callback, | |
150 scoped_ptr<base::Value> manifest); | |
151 | |
152 // Called once the manifest is read and |this| hasn't been deleted. | |
153 void OnReadManifestImpl(const std::string& name, | |
154 const ResolveMojoNameCallback& callback, | |
155 scoped_ptr<base::Value> manifest); | |
156 | |
157 base::TaskRunner* blocking_pool_; | |
158 GURL system_package_dir_; | |
159 | |
160 mojo::BindingSet<mojom::Resolver> resolver_bindings_; | |
161 mojo::BindingSet<mojo::shell::mojom::ShellResolver> shell_resolver_bindings_; | |
162 mojo::BindingSet<mojom::Catalog> catalog_bindings_; | |
163 | |
164 scoped_ptr<ApplicationCatalogStore> catalog_store_; | |
165 std::map<std::string, ApplicationInfo> catalog_; | |
166 | |
167 // Used when an app handles multiple names. Maps from app (as name) to name of | |
168 // app that is responsible for handling it. The value is a pair of the name of | |
169 // the handler along with a qualifier. | |
170 MojoNameAliasMap mojo_name_aliases_; | |
171 | |
172 std::map<std::string, std::string> qualifiers_; | |
173 | |
174 base::WeakPtrFactory<PackageManager> weak_factory_; | |
175 | |
176 DISALLOW_COPY_AND_ASSIGN(PackageManager); | |
177 }; | |
178 | |
179 } // namespace package_manager | |
180 | |
181 #endif // MOJO_SERVICES_PACKAGE_MANAGER_PACKAGE_MANAGER_H_ | |
OLD | NEW |