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.
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
;
29 using mojo::BindToRequest
;
30 using mojo::ContentHandler
;
31 using mojo::InterfaceRequest
;
32 using mojo::ServiceProvider
;
33 using mojo::ServiceProviderPtr
;
36 using mojo::URLLoaderPtr
;
37 using mojo::URLResponsePtr
;
39 namespace html_viewer
{
43 class HTMLViewerApplication
: public mojo::Application
{
45 HTMLViewerApplication(InterfaceRequest
<Application
> request
,
46 URLResponsePtr response
,
48 : url_(response
->url
),
49 binding_(this, request
.Pass()),
50 initial_response_(response
.Pass()),
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());
72 network_service_
->CreateURLLoader(GetProxy(&loader
));
73 mojo::URLRequestPtr
request(mojo::URLRequest::New());
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();
83 base::Bind(&HTMLViewerApplication::OnResponseReceived
,
84 base::Unretained(this), base::Passed(&loader
),
85 base::Passed(&services
)));
89 void RequestQuit() override
{}
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_
);
101 mojo::StrongBinding
<mojo::Application
> binding_
;
103 mojo::NetworkServicePtr network_service_
;
104 URLResponsePtr initial_response_
;
107 DISALLOW_COPY_AND_ASSIGN(HTMLViewerApplication
);
110 class ContentHandlerImpl
: public mojo::InterfaceImpl
<ContentHandler
> {
112 explicit ContentHandlerImpl(Setup
* setup
) : setup_(setup
) {}
113 ~ContentHandlerImpl() override
{}
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_
);
125 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl
);
128 class HTMLViewer
: public mojo::ApplicationDelegate
,
129 public mojo::InterfaceFactory
<ContentHandler
> {
132 ~HTMLViewer() override
{ blink::shutdown(); }
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);
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
);