Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / components / html_viewer / html_viewer.cc
blob0e49ef46a8290e3ae9b9872442cc1880e7bfcec1
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 : url_(response->url),
49 binding_(this, request.Pass()),
50 initial_response_(response.Pass()),
51 setup_(setup) {}
53 void Initialize(ShellPtr shell, const String& url) override {
54 ServiceProviderPtr service_provider;
55 shell_ = shell.Pass();
56 mojo::URLRequestPtr request(mojo::URLRequest::New());
57 request->url = mojo::String::From("mojo:network_service");
58 shell_->ConnectToApplication(request.Pass(),
59 GetProxy(&service_provider), nullptr);
60 ConnectToService(service_provider.get(), &network_service_);
63 void AcceptConnection(const String& requestor_url,
64 InterfaceRequest<ServiceProvider> services,
65 ServiceProviderPtr exposed_services,
66 const String& url) override {
67 if (initial_response_) {
68 OnResponseReceived(URLLoaderPtr(), services.Pass(),
69 initial_response_.Pass());
70 } else {
71 URLLoaderPtr loader;
72 network_service_->CreateURLLoader(GetProxy(&loader));
73 mojo::URLRequestPtr request(mojo::URLRequest::New());
74 request->url = url_;
75 request->auto_follow_redirects = true;
77 // |loader| will be pass to the OnResponseReceived method through a
78 // callback. Because order of evaluation is undefined, a reference to the
79 // raw pointer is needed.
80 mojo::URLLoader* raw_loader = loader.get();
81 raw_loader->Start(
82 request.Pass(),
83 base::Bind(&HTMLViewerApplication::OnResponseReceived,
84 base::Unretained(this), base::Passed(&loader),
85 base::Passed(&services)));
89 void RequestQuit() override {}
91 private:
92 void OnResponseReceived(URLLoaderPtr loader,
93 InterfaceRequest<ServiceProvider> services,
94 URLResponsePtr response) {
95 // HTMLDocument is destroyed when the hosting view is destroyed.
96 // TODO(sky): when headless, this leaks.
97 new HTMLDocument(services.Pass(), response.Pass(), shell_.get(), setup_);
100 String url_;
101 mojo::StrongBinding<mojo::Application> binding_;
102 ShellPtr shell_;
103 mojo::NetworkServicePtr network_service_;
104 URLResponsePtr initial_response_;
105 Setup* setup_;
107 DISALLOW_COPY_AND_ASSIGN(HTMLViewerApplication);
110 class ContentHandlerImpl : public mojo::InterfaceImpl<ContentHandler> {
111 public:
112 explicit ContentHandlerImpl(Setup* setup) : setup_(setup) {}
113 ~ContentHandlerImpl() override {}
115 private:
116 // Overridden from ContentHandler:
117 void StartApplication(InterfaceRequest<mojo::Application> request,
118 URLResponsePtr response) override {
119 // HTMLViewerApplication is owned by the binding.
120 new HTMLViewerApplication(request.Pass(), response.Pass(), setup_);
123 Setup* setup_;
125 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl);
128 class HTMLViewer : public mojo::ApplicationDelegate,
129 public mojo::InterfaceFactory<ContentHandler> {
130 public:
131 HTMLViewer() {}
132 ~HTMLViewer() override { blink::shutdown(); }
134 private:
135 // Overridden from ApplicationDelegate:
136 void Initialize(mojo::ApplicationImpl* app) override {
137 setup_.reset(new Setup(app));
140 bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
141 // If we're not being connected to from the view manager assume we're being
142 // run in tests, or a headless environment, in which case we'll never get a
143 // ui and there is no point in waiting for it.
144 if (connection->GetRemoteApplicationURL() != "mojo://view_manager/" &&
145 !setup_->did_init()) {
146 setup_->InitHeadless();
148 connection->AddService(this);
149 return true;
152 // Overridden from InterfaceFactory<ContentHandler>
153 void Create(ApplicationConnection* connection,
154 mojo::InterfaceRequest<ContentHandler> request) override {
155 BindToRequest(new ContentHandlerImpl(setup_.get()), &request);
158 scoped_ptr<Setup> setup_;
160 DISALLOW_COPY_AND_ASSIGN(HTMLViewer);
163 } // namespace html_viewer
165 MojoResult MojoMain(MojoHandle shell_handle) {
166 mojo::ApplicationRunner runner(new html_viewer::HTMLViewer);
167 return runner.Run(shell_handle);