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 : app_refcount_(setup
->app()->app_lifetime_helper()->CreateAppRefCount()),
50 binding_(this, request
.Pass()),
51 initial_response_(response
.Pass()),
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());
74 network_service_
->CreateURLLoader(GetProxy(&loader
));
75 mojo::URLRequestPtr
request(mojo::URLRequest::New());
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();
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
{
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_
;
107 mojo::StrongBinding
<mojo::Application
> binding_
;
109 mojo::NetworkServicePtr network_service_
;
110 URLResponsePtr initial_response_
;
113 DISALLOW_COPY_AND_ASSIGN(HTMLViewerApplication
);
116 class ContentHandlerImpl
: public mojo::ContentHandler
{
118 ContentHandlerImpl(Setup
* setup
,
119 mojo::InterfaceRequest
<ContentHandler
> request
)
121 binding_(this, request
.Pass()) {}
122 ~ContentHandlerImpl() override
{}
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_
);
133 mojo::StrongBinding
<mojo::ContentHandler
> binding_
;
135 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl
);
138 class HTMLViewer
: public mojo::ApplicationDelegate
,
139 public mojo::InterfaceFactory
<ContentHandler
> {
142 ~HTMLViewer() override
{}
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);
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
);