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