mojo: Fix Blink's shutdown sequence in AxProviderImplTest
[chromium-blink-merge.git] / components / html_viewer / html_viewer.cc
blobfad2bc60c602c8b70512111b4d1183260ddcd76b
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 #include "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "components/html_viewer/html_document.h"
13 #include "components/html_viewer/setup.h"
14 #include "mojo/application/public/cpp/application_connection.h"
15 #include "mojo/application/public/cpp/application_delegate.h"
16 #include "mojo/application/public/cpp/application_impl.h"
17 #include "mojo/application/public/cpp/application_runner.h"
18 #include "mojo/application/public/cpp/connect.h"
19 #include "mojo/application/public/cpp/interface_factory_impl.h"
20 #include "mojo/application/public/interfaces/content_handler.mojom.h"
21 #include "mojo/common/common_type_converters.h"
22 #include "mojo/services/network/public/interfaces/network_service.mojom.h"
23 #include "third_party/WebKit/public/web/WebKit.h"
24 #include "third_party/mojo/src/mojo/public/c/system/main.h"
25 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
27 using mojo::ApplicationConnection;
28 using mojo::Array;
29 using mojo::BindToRequest;
30 using mojo::ContentHandler;
31 using mojo::InterfaceRequest;
32 using mojo::ServiceProvider;
33 using mojo::ServiceProviderPtr;
34 using mojo::ShellPtr;
35 using mojo::String;
36 using mojo::URLLoaderPtr;
37 using mojo::URLResponsePtr;
39 namespace html_viewer {
41 class HTMLViewer;
43 class HTMLViewerApplication : public mojo::Application {
44 public:
45 HTMLViewerApplication(InterfaceRequest<Application> request,
46 URLResponsePtr response,
47 Setup* setup)
48 : app_refcount_(setup->app()->app_lifetime_helper()->CreateAppRefCount()),
49 url_(response->url),
50 binding_(this, request.Pass()),
51 initial_response_(response.Pass()),
52 setup_(setup) {
55 ~HTMLViewerApplication() override {
58 void Initialize(ShellPtr shell, const String& url) override {
59 shell_ = shell.Pass();
60 mojo::URLRequestPtr request(mojo::URLRequest::New());
61 request->url = mojo::String::From("mojo:network_service");
62 setup_->app()->ConnectToService(request.Pass(), &network_service_);
65 void AcceptConnection(const String& requestor_url,
66 InterfaceRequest<ServiceProvider> services,
67 ServiceProviderPtr exposed_services,
68 const String& url) override {
69 if (initial_response_) {
70 OnResponseReceived(URLLoaderPtr(), services.Pass(),
71 initial_response_.Pass());
72 } else {
73 URLLoaderPtr loader;
74 network_service_->CreateURLLoader(GetProxy(&loader));
75 mojo::URLRequestPtr request(mojo::URLRequest::New());
76 request->url = url_;
77 request->auto_follow_redirects = true;
79 // |loader| will be pass to the OnResponseReceived method through a
80 // callback. Because order of evaluation is undefined, a reference to the
81 // raw pointer is needed.
82 mojo::URLLoader* raw_loader = loader.get();
83 raw_loader->Start(
84 request.Pass(),
85 base::Bind(&HTMLViewerApplication::OnResponseReceived,
86 base::Unretained(this), base::Passed(&loader),
87 base::Passed(&services)));
91 void OnQuitRequested(const mojo::Callback<void(bool)>& callback) override {
92 callback.Run(true);
93 delete this;
96 private:
97 void OnResponseReceived(URLLoaderPtr loader,
98 InterfaceRequest<ServiceProvider> services,
99 URLResponsePtr response) {
100 // HTMLDocument is destroyed when the hosting view is destroyed.
101 // TODO(sky): when headless, this leaks.
102 new HTMLDocument(services.Pass(), response.Pass(), shell_.Pass(), setup_);
105 scoped_ptr<mojo::AppRefCount> app_refcount_;
106 String url_;
107 mojo::StrongBinding<mojo::Application> binding_;
108 ShellPtr shell_;
109 mojo::NetworkServicePtr network_service_;
110 URLResponsePtr initial_response_;
111 Setup* setup_;
113 DISALLOW_COPY_AND_ASSIGN(HTMLViewerApplication);
116 class ContentHandlerImpl : public mojo::ContentHandler {
117 public:
118 ContentHandlerImpl(Setup* setup,
119 mojo::InterfaceRequest<ContentHandler> request)
120 : setup_(setup),
121 binding_(this, request.Pass()) {}
122 ~ContentHandlerImpl() override {}
124 private:
125 // Overridden from ContentHandler:
126 void StartApplication(InterfaceRequest<mojo::Application> request,
127 URLResponsePtr response) override {
128 // HTMLViewerApplication is owned by the binding.
129 new HTMLViewerApplication(request.Pass(), response.Pass(), setup_);
132 Setup* setup_;
133 mojo::StrongBinding<mojo::ContentHandler> binding_;
135 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl);
138 class HTMLViewer : public mojo::ApplicationDelegate,
139 public mojo::InterfaceFactory<ContentHandler> {
140 public:
141 HTMLViewer() {}
142 ~HTMLViewer() override {}
144 private:
145 // Overridden from ApplicationDelegate:
146 void Initialize(mojo::ApplicationImpl* app) override {
147 setup_.reset(new Setup(app));
150 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
151 // If we're not being connected to from the view manager assume we're being
152 // run in tests, or a headless environment, in which case we'll never get a
153 // ui and there is no point in waiting for it.
154 if (connection->GetRemoteApplicationURL() != "mojo://view_manager/" &&
155 !setup_->did_init()) {
156 setup_->InitHeadless();
158 connection->AddService(this);
159 return true;
162 // Overridden from InterfaceFactory<ContentHandler>
163 void Create(ApplicationConnection* connection,
164 mojo::InterfaceRequest<ContentHandler> request) override {
165 new ContentHandlerImpl(setup_.get(), request.Pass());
168 scoped_ptr<Setup> setup_;
170 DISALLOW_COPY_AND_ASSIGN(HTMLViewer);
173 } // namespace html_viewer
175 MojoResult MojoMain(MojoHandle shell_handle) {
176 mojo::ApplicationRunner runner(new html_viewer::HTMLViewer);
177 return runner.Run(shell_handle);