OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_ | |
6 #define MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/gtest_prod_util.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "mojo/application_manager/application_loader.h" | |
16 #include "mojo/application_manager/application_manager_export.h" | |
17 #include "mojo/application_manager/shell_impl.h" | |
18 #include "mojo/public/interfaces/application/service_provider.mojom.h" | |
19 #include "url/gurl.h" | |
20 | |
21 namespace mojo { | |
22 | |
23 class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager { | |
24 public: | |
25 class MOJO_APPLICATION_MANAGER_EXPORT Delegate { | |
26 public: | |
27 virtual ~Delegate(); | |
28 // Send when the Application holding the handle on the other end of the | |
29 // Shell pipe goes away. | |
30 virtual void OnApplicationError(const GURL& url); | |
31 virtual GURL ResolveURL(const GURL& url); | |
32 }; | |
33 | |
34 // API for testing. | |
35 class MOJO_APPLICATION_MANAGER_EXPORT TestAPI { | |
36 public: | |
37 explicit TestAPI(ApplicationManager* manager); | |
38 ~TestAPI(); | |
39 | |
40 // Returns true if the shared instance has been created. | |
41 static bool HasCreatedInstance(); | |
42 // Returns true if there is a ShellImpl for this URL. | |
43 bool HasFactoryForURL(const GURL& url) const; | |
44 | |
45 private: | |
46 ApplicationManager* manager_; | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(TestAPI); | |
49 }; | |
50 | |
51 explicit ApplicationManager(Delegate* delegate); | |
52 ~ApplicationManager(); | |
53 | |
54 // Loads a service if necessary and establishes a new client connection. | |
55 void ConnectToApplication(const GURL& application_url, | |
56 const GURL& requestor_url, | |
57 InterfaceRequest<ServiceProvider> services, | |
58 ServiceProviderPtr exposed_services); | |
59 | |
60 template <typename Interface> | |
61 inline void ConnectToService(const GURL& application_url, | |
62 InterfacePtr<Interface>* ptr) { | |
63 ScopedMessagePipeHandle service_handle = | |
64 ConnectToServiceByName(application_url, Interface::Name_); | |
65 ptr->Bind(service_handle.Pass()); | |
66 } | |
67 | |
68 ScopedMessagePipeHandle ConnectToServiceByName( | |
69 const GURL& application_url, | |
70 const std::string& interface_name); | |
71 | |
72 void RegisterExternalApplication(const GURL& application_url, | |
73 ScopedMessagePipeHandle shell); | |
74 | |
75 // Sets the default Loader to be used if not overridden by SetLoaderForURL() | |
76 // or SetLoaderForScheme(). | |
77 void set_default_loader(scoped_ptr<ApplicationLoader> loader) { | |
78 default_loader_ = loader.Pass(); | |
79 } | |
80 // Sets a Loader to be used for a specific url. | |
81 void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url); | |
82 // Sets a Loader to be used for a specific url scheme. | |
83 void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader, | |
84 const std::string& scheme); | |
85 // These strings will be passed to the Initialize() method when an | |
86 // Application is instantiated. | |
87 void SetArgsForURL(const std::vector<std::string>& args, const GURL& url); | |
88 | |
89 // Destroys all Shell-ends of connections established with Applications. | |
90 // Applications connected by this ApplicationManager will observe pipe errors | |
91 // and have a chance to shutdown. | |
92 void TerminateShellConnections(); | |
93 | |
94 // Removes a ShellImpl when it encounters an error. | |
95 void OnShellImplError(ShellImpl* shell_impl); | |
96 | |
97 private: | |
98 enum IncludeDefaultLoader { | |
99 INCLUDE_DEFAULT_LOADER, | |
100 DONT_INCLUDE_DEFAULT_LOADER, | |
101 }; | |
102 | |
103 class ContentHandlerConnection; | |
104 | |
105 typedef std::map<std::string, ApplicationLoader*> SchemeToLoaderMap; | |
106 typedef std::map<GURL, ApplicationLoader*> URLToLoaderMap; | |
107 typedef std::map<GURL, ShellImpl*> URLToShellImplMap; | |
108 typedef std::map<GURL, ContentHandlerConnection*> URLToContentHandlerMap; | |
109 typedef std::map<GURL, std::vector<std::string> > URLToArgsMap; | |
110 | |
111 void ConnectToApplicationImpl(const GURL& requested_url, | |
112 const GURL& resolved_url, | |
113 const GURL& requestor_url, | |
114 InterfaceRequest<ServiceProvider> services, | |
115 ServiceProviderPtr exposed_services, | |
116 ApplicationLoader* loader); | |
117 | |
118 void ConnectToClient(ShellImpl* shell_impl, | |
119 const GURL& url, | |
120 const GURL& requestor_url, | |
121 InterfaceRequest<ServiceProvider> services, | |
122 ServiceProviderPtr exposed_services); | |
123 | |
124 void LoadWithContentHandler(const GURL& content_handler_url, | |
125 ScopedMessagePipeHandle shell_handle, | |
126 URLResponsePtr url_response); | |
127 | |
128 // Return the appropriate loader for |url|. This can return NULL if there is | |
129 // no default loader configured. | |
130 ApplicationLoader* GetLoaderForURL(const GURL& url, | |
131 IncludeDefaultLoader fallback); | |
132 | |
133 // Removes a ContentHandler when it encounters an error. | |
134 void OnContentHandlerError(ContentHandlerConnection* content_handler); | |
135 | |
136 // Returns the arguments for the given url. | |
137 Array<String> GetArgsForURL(const GURL& url); | |
138 | |
139 Delegate* delegate_; | |
140 // Loader management. | |
141 URLToLoaderMap url_to_loader_; | |
142 SchemeToLoaderMap scheme_to_loader_; | |
143 scoped_ptr<ApplicationLoader> default_loader_; | |
144 | |
145 URLToShellImplMap url_to_shell_impl_; | |
146 URLToContentHandlerMap url_to_content_handler_; | |
147 URLToArgsMap url_to_args_; | |
148 | |
149 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_; | |
150 | |
151 DISALLOW_COPY_AND_ASSIGN(ApplicationManager); | |
152 }; | |
153 | |
154 } // namespace mojo | |
155 | |
156 #endif // MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_ | |
OLD | NEW |