Delete historical data usage from store when user clears data usage.
[chromium-blink-merge.git] / mojo / application / public / cpp / application_impl.h
blob01088f8cceae30dca24a985b2fc4a52505f07be7
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.
5 #ifndef MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_
6 #define MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_
8 #include <vector>
10 #include "base/memory/scoped_vector.h"
11 #include "base/memory/weak_ptr.h"
12 #include "mojo/application/public/cpp/app_lifetime_helper.h"
13 #include "mojo/application/public/cpp/application_connection.h"
14 #include "mojo/application/public/cpp/application_delegate.h"
15 #include "mojo/application/public/cpp/lib/service_registry.h"
16 #include "mojo/application/public/interfaces/application.mojom.h"
17 #include "mojo/application/public/interfaces/shell.mojom.h"
18 #include "mojo/public/cpp/bindings/binding.h"
19 #include "mojo/public/cpp/bindings/callback.h"
20 #include "mojo/public/cpp/system/core.h"
22 namespace mojo {
24 // TODO(beng): This comment is hilariously out of date.
25 // Utility class for communicating with the Shell, and providing Services
26 // to clients.
28 // To use define a class that implements your specific server api, e.g. FooImpl
29 // to implement a service named Foo.
30 // That class must subclass an InterfaceImpl specialization.
32 // If there is context that is to be shared amongst all instances, define a
33 // constructor with that class as its only argument, otherwise define an empty
34 // constructor.
36 // class FooImpl : public InterfaceImpl<Foo> {
37 // public:
38 // FooImpl(ApplicationContext* app_context) {}
39 // };
41 // or
43 // class BarImpl : public InterfaceImpl<Bar> {
44 // public:
45 // // contexts will remain valid for the lifetime of BarImpl.
46 // BarImpl(ApplicationContext* app_context, BarContext* service_context)
47 // : app_context_(app_context), servicecontext_(context) {}
49 // Create an ApplicationImpl instance that collects any service implementations.
51 // ApplicationImpl app(service_provider_handle);
52 // app.AddService<FooImpl>();
54 // BarContext context;
55 // app.AddService<BarImpl>(&context);
58 class ApplicationImpl : public Application {
59 public:
60 class TestApi {
61 public:
62 explicit TestApi(ApplicationImpl* application)
63 : application_(application) {}
65 void UnbindConnections(InterfaceRequest<Application>* application_request,
66 ShellPtr* shell) {
67 application_->UnbindConnections(application_request, shell);
70 private:
71 ApplicationImpl* application_;
74 // Does not take ownership of |delegate|, which must remain valid for the
75 // lifetime of ApplicationImpl.
76 ApplicationImpl(ApplicationDelegate* delegate,
77 InterfaceRequest<Application> request);
78 // Constructs an ApplicationImpl with a custom termination closure. This
79 // closure is invoked on Quit() instead of the default behavior of quitting
80 // the current base::MessageLoop.
81 ApplicationImpl(ApplicationDelegate* delegate,
82 InterfaceRequest<Application> request,
83 const Closure& termination_closure);
84 ~ApplicationImpl() override;
86 // The Mojo shell. This will return a valid pointer after Initialize() has
87 // been invoked. It will remain valid until UnbindConnections() is invoked or
88 // the ApplicationImpl is destroyed.
89 Shell* shell() const { return shell_.get(); }
91 const std::string& url() const { return url_; }
93 AppLifetimeHelper* app_lifetime_helper() { return &app_lifetime_helper_; }
95 // Requests a new connection to an application. Returns a pointer to the
96 // connection if the connection is permitted by this application's delegate,
97 // or nullptr otherwise. Caller takes ownership.
98 scoped_ptr<ApplicationConnection> ConnectToApplication(URLRequestPtr request);
99 scoped_ptr<ApplicationConnection> ConnectToApplicationWithCapabilityFilter(
100 URLRequestPtr request,
101 CapabilityFilterPtr filter);
103 // Connect to application identified by |request->url| and connect to the
104 // service implementation of the interface identified by |Interface|.
105 template <typename Interface>
106 void ConnectToService(mojo::URLRequestPtr request,
107 InterfacePtr<Interface>* ptr) {
108 scoped_ptr<ApplicationConnection> connection =
109 ConnectToApplication(request.Pass());
110 if (!connection.get())
111 return;
112 connection->ConnectToService(ptr);
115 // Initiate shutdown of this application. This may involve a round trip to the
116 // Shell to ensure there are no inbound service requests.
117 void Quit();
119 private:
120 // Application implementation.
121 void Initialize(ShellPtr shell, const mojo::String& url) override;
122 void AcceptConnection(const String& requestor_url,
123 InterfaceRequest<ServiceProvider> services,
124 ServiceProviderPtr exposed_services,
125 Array<String> allowed_interfaces,
126 const String& url) override;
127 void OnQuitRequested(const Callback<void(bool)>& callback) override;
129 void OnConnectionError();
131 // Called from Quit() when there is no Shell connection, or asynchronously
132 // from Quit() once the Shell has OK'ed shutdown.
133 void QuitNow();
135 // Unbinds the Shell and Application connections. Can be used to re-bind the
136 // handles to another implementation of ApplicationImpl, for instance when
137 // running apptests.
138 void UnbindConnections(InterfaceRequest<Application>* application_request,
139 ShellPtr* shell);
141 // We track the lifetime of incoming connection registries as it more
142 // convenient for the client.
143 ScopedVector<ApplicationConnection> incoming_connections_;
144 ApplicationDelegate* delegate_;
145 Binding<Application> binding_;
146 ShellPtr shell_;
147 std::string url_;
148 Closure termination_closure_;
149 AppLifetimeHelper app_lifetime_helper_;
150 bool quit_requested_;
151 base::WeakPtrFactory<ApplicationImpl> weak_factory_;
153 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl);
156 } // namespace mojo
158 #endif // MOJO_APPLICATION_PUBLIC_CPP_APPLICATION_IMPL_H_