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 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" 17 #include "url/gurl.h" 18 19 namespace mojo { 20 21 class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager { 22 public: 23 class MOJO_APPLICATION_MANAGER_EXPORT Delegate { 24 public: 25 virtual ~Delegate(); 26 // Send when the Applicaiton holding the handle on the other end of the 27 // Shell pipe goes away. 28 virtual void OnApplicationError(const GURL& url) = 0; 29 }; 30 31 // API for testing. 32 class MOJO_APPLICATION_MANAGER_EXPORT TestAPI { 33 public: 34 explicit TestAPI(ApplicationManager* manager); 35 ~TestAPI(); 36 37 // Returns true if the shared instance has been created. 38 static bool HasCreatedInstance(); 39 // Returns true if there is a ShellImpl for this URL. 40 bool HasFactoryForURL(const GURL& url) const; 41 42 private: 43 ApplicationManager* manager_; 44 45 DISALLOW_COPY_AND_ASSIGN(TestAPI); 46 }; 47 48 // Interface class for debugging only. 49 class Interceptor { 50 public: 51 virtual ~Interceptor() {} 52 // Called when ApplicationManager::Connect is called. 53 virtual ServiceProviderPtr OnConnectToClient( 54 const GURL& url, 55 ServiceProviderPtr service_provider) = 0; 56 }; 57 58 ApplicationManager(); 59 ~ApplicationManager(); 60 61 // Returns a shared instance, creating it if necessary. 62 static ApplicationManager* GetInstance(); 63 64 void SetDelegate(Delegate* delegate) { delegate_ = delegate; } 65 66 // Loads a service if necessary and establishes a new client connection. 67 void ConnectToApplication(const GURL& application_url, 68 const GURL& requestor_url, 69 ServiceProviderPtr service_provider); 70 71 template <typename Interface> 72 inline void ConnectToService(const GURL& application_url, 73 InterfacePtr<Interface>* ptr) { 74 ScopedMessagePipeHandle service_handle = 75 ConnectToServiceByName(application_url, Interface::Name_); 76 ptr->Bind(service_handle.Pass()); 77 } 78 79 ScopedMessagePipeHandle ConnectToServiceByName( 80 const GURL& application_url, 81 const std::string& interface_name); 82 83 void set_delegate(Delegate* delegate) { delegate_ = delegate; } 84 85 // Sets the default Loader to be used if not overridden by SetLoaderForURL() 86 // or SetLoaderForScheme(). 87 void set_default_loader(scoped_ptr<ApplicationLoader> loader) { 88 default_loader_ = loader.Pass(); 89 } 90 // Sets a Loader to be used for a specific url. 91 void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url); 92 // Sets a Loader to be used for a specific url scheme. 93 void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader, 94 const std::string& scheme); 95 // These strings will be passed to the Initialize() method when an 96 // Application is instantiated. 97 void SetArgsForURL(const std::vector<std::string>& args, const GURL& url); 98 99 // Allows to interpose a debugger to service connections. 100 void SetInterceptor(Interceptor* interceptor); 101 102 // Destroys all Shell-ends of connections established with Applications. 103 // Applications connected by this ApplicationManager will observe pipe errors 104 // and have a chance to shutdown. 105 void TerminateShellConnections(); 106 107 private: 108 struct ContentHandlerConnection; 109 class LoadCallbacksImpl; 110 class ShellImpl; 111 112 typedef std::map<std::string, ApplicationLoader*> SchemeToLoaderMap; 113 typedef std::map<GURL, ApplicationLoader*> URLToLoaderMap; 114 typedef std::map<GURL, ShellImpl*> URLToShellImplMap; 115 typedef std::map<GURL, ContentHandlerConnection*> URLToContentHandlerMap; 116 typedef std::map<GURL, std::vector<std::string> > URLToArgsMap; 117 118 void ConnectToClient(ShellImpl* shell_impl, 119 const GURL& url, 120 const GURL& requestor_url, 121 ServiceProviderPtr service_provider); 122 123 void RegisterLoadedApplication(const GURL& service_url, 124 const GURL& requestor_url, 125 ServiceProviderPtr service_provider, 126 ScopedMessagePipeHandle* shell_handle); 127 128 void LoadWithContentHandler(const GURL& content_url, 129 const GURL& requestor_url, 130 const GURL& content_handler_url, 131 URLResponsePtr url_response, 132 ServiceProviderPtr service_provider); 133 134 // Returns the Loader to use for a url (using default if not overridden.) 135 // The preference is to use a loader that's been specified for an url first, 136 // then one that's been specified for a scheme, then the default. 137 ApplicationLoader* GetLoaderForURL(const GURL& url); 138 139 // Removes a ShellImpl when it encounters an error. 140 void OnShellImplError(ShellImpl* shell_impl); 141 142 Delegate* delegate_; 143 // Loader management. 144 URLToLoaderMap url_to_loader_; 145 SchemeToLoaderMap scheme_to_loader_; 146 scoped_ptr<ApplicationLoader> default_loader_; 147 Interceptor* interceptor_; 148 149 URLToShellImplMap url_to_shell_impl_; 150 URLToContentHandlerMap url_to_content_handler_; 151 URLToArgsMap url_to_args_; 152 153 base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_; 154 155 DISALLOW_COPY_AND_ASSIGN(ApplicationManager); 156 }; 157 158 } // namespace mojo 159 160 #endif // MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_ 161