16e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
56e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#ifndef MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
66e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#define MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <map>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/basictypes.h"
11effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "base/gtest_prod_util.h"
120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/memory/scoped_ptr.h"
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/memory/weak_ptr.h"
146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/application_manager/application_loader.h"
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/application_manager/application_manager_export.h"
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "mojo/public/interfaces/application/service_provider.mojom.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "url/gurl.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace mojo {
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager {
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class MOJO_APPLICATION_MANAGER_EXPORT Delegate {
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci   public:
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    virtual ~Delegate();
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // Send when the Applicaiton holding the handle on the other end of the
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    // Shell pipe goes away.
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    virtual void OnApplicationError(const GURL& url) = 0;
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  };
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // API for testing.
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  class MOJO_APPLICATION_MANAGER_EXPORT TestAPI {
335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu   public:
346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    explicit TestAPI(ApplicationManager* manager);
355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    ~TestAPI();
365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // Returns true if the shared instance has been created.
38effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    static bool HasCreatedInstance();
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Returns true if there is a ShellImpl for this URL.
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    bool HasFactoryForURL(const GURL& url) const;
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
425c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu   private:
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    ApplicationManager* manager_;
445c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
455c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    DISALLOW_COPY_AND_ASSIGN(TestAPI);
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
48c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Interface class for debugging only.
49c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  class Interceptor {
50c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch   public:
51c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    virtual ~Interceptor() {}
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // Called when ApplicationManager::Connect is called.
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    virtual ServiceProviderPtr OnConnectToClient(
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        const GURL& url,
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        ServiceProviderPtr service_provider) = 0;
56c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  };
57c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ApplicationManager();
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ~ApplicationManager();
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Returns a shared instance, creating it if necessary.
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  static ApplicationManager* GetInstance();
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void SetDelegate(Delegate* delegate) { delegate_ = delegate; }
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Loads a service if necessary and establishes a new client connection.
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void ConnectToApplication(const GURL& application_url,
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                            const GURL& requestor_url,
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                            ServiceProviderPtr service_provider);
7046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
7146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  template <typename Interface>
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  inline void ConnectToService(const GURL& application_url,
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                               InterfacePtr<Interface>* ptr) {
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    ScopedMessagePipeHandle service_handle =
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        ConnectToServiceByName(application_url, Interface::Name_);
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    ptr->Bind(service_handle.Pass());
7746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ScopedMessagePipeHandle ConnectToServiceByName(
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const GURL& application_url,
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const std::string& interface_name);
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void set_delegate(Delegate* delegate) { delegate_ = delegate; }
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Sets the default Loader to be used if not overridden by SetLoaderForURL()
860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // or SetLoaderForScheme().
876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void set_default_loader(scoped_ptr<ApplicationLoader> loader) {
880529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    default_loader_ = loader.Pass();
890529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Sets a Loader to be used for a specific url.
916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, const GURL& url);
92effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Sets a Loader to be used for a specific url scheme.
936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader,
940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                          const std::string& scheme);
951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // These strings will be passed to the Initialize() method when an
961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Application is instantiated.
971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void SetArgsForURL(const std::vector<std::string>& args, const GURL& url);
981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
99c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Allows to interpose a debugger to service connections.
100c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void SetInterceptor(Interceptor* interceptor);
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Destroys all Shell-ends of connections established with Applications.
1036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Applications connected by this ApplicationManager will observe pipe errors
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // and have a chance to shutdown.
1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void TerminateShellConnections();
1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  struct ContentHandlerConnection;
1095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  class LoadCallbacksImpl;
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  class ShellImpl;
1115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  typedef std::map<std::string, ApplicationLoader*> SchemeToLoaderMap;
1136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  typedef std::map<GURL, ApplicationLoader*> URLToLoaderMap;
114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  typedef std::map<GURL, ShellImpl*> URLToShellImplMap;
1155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  typedef std::map<GURL, ContentHandlerConnection*> URLToContentHandlerMap;
1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  typedef std::map<GURL, std::vector<std::string> > URLToArgsMap;
1175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void ConnectToClient(ShellImpl* shell_impl,
1195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                       const GURL& url,
1205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                       const GURL& requestor_url,
1215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                       ServiceProviderPtr service_provider);
1225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void RegisterLoadedApplication(const GURL& service_url,
1245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                 const GURL& requestor_url,
1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                 ServiceProviderPtr service_provider,
1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                 ScopedMessagePipeHandle* shell_handle);
1275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void LoadWithContentHandler(const GURL& content_url,
1295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                              const GURL& requestor_url,
1305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                              const GURL& content_handler_url,
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                              URLResponsePtr url_response,
1325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                              ServiceProviderPtr service_provider);
133effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
134effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Returns the Loader to use for a url (using default if not overridden.)
135effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The preference is to use a loader that's been specified for an url first,
136effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // then one that's been specified for a scheme, then the default.
1376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ApplicationLoader* GetLoaderForURL(const GURL& url);
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
139116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Removes a ShellImpl when it encounters an error.
140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void OnShellImplError(ShellImpl* shell_impl);
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Delegate* delegate_;
143effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Loader management.
144effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  URLToLoaderMap url_to_loader_;
145effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SchemeToLoaderMap scheme_to_loader_;
1466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_ptr<ApplicationLoader> default_loader_;
147c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  Interceptor* interceptor_;
148effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  URLToShellImplMap url_to_shell_impl_;
1505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  URLToContentHandlerMap url_to_content_handler_;
1511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  URLToArgsMap url_to_args_;
1525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
154116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ApplicationManager);
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace mojo
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif  // MOJO_APPLICATION_MANAGER_APPLICATION_MANAGER_H_
161