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/command_line.h"
6 #include "base/logging.h"
7 #include "base/macros.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/threading/thread.h"
11 #include "mojo/application/application_runner_chromium.h"
12 #include "mojo/public/c/system/main.h"
13 #include "mojo/public/cpp/application/application_connection.h"
14 #include "mojo/public/cpp/application/application_delegate.h"
15 #include "mojo/public/cpp/application/application_impl.h"
16 #include "mojo/public/cpp/application/connect.h"
17 #include "mojo/public/cpp/application/interface_factory_impl.h"
18 #include "mojo/services/content_handler/public/interfaces/content_handler.mojom.h"
19 #include "mojo/services/html_viewer/html_document.h"
20 #include "mojo/services/html_viewer/mojo_blink_platform_impl.h"
21 #include "mojo/services/html_viewer/webmediaplayer_factory.h"
22 #include "mojo/services/network/public/interfaces/network_service.mojom.h"
23 #include "third_party/WebKit/public/web/WebKit.h"
25 #if !defined(COMPONENT_BUILD)
26 #include "base/i18n/icu_util.h"
27 #include "base/path_service.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/base/ui_base_paths.h"
34 // Switches for html_viewer to be used with "--args-for". For example:
35 // --args-for='mojo:html_viewer --enable-mojo-media-renderer'
37 // Enable MediaRenderer in media pipeline instead of using the internal
38 // media::Renderer implementation.
39 const char kEnableMojoMediaRenderer
[] = "enable-mojo-media-renderer";
43 class HTMLViewerApplication
: public Application
{
45 HTMLViewerApplication(ShellPtr shell
,
46 URLResponsePtr response
,
47 scoped_refptr
<base::MessageLoopProxy
> compositor_thread
,
48 WebMediaPlayerFactory
* web_media_player_factory
)
49 : url_(response
->url
),
51 initial_response_(response
.Pass()),
52 compositor_thread_(compositor_thread
),
53 web_media_player_factory_(web_media_player_factory
) {
54 shell_
.set_client(this);
55 ServiceProviderPtr service_provider
;
56 shell_
->ConnectToApplication("mojo:network_service",
57 GetProxy(&service_provider
));
58 ConnectToService(service_provider
.get(), &network_service_
);
61 void Initialize(Array
<String
> args
) override
{}
63 void AcceptConnection(const String
& requestor_url
,
64 ServiceProviderPtr provider
) override
{
65 if (initial_response_
) {
66 OnResponseReceived(URLLoaderPtr(), provider
.Pass(),
67 initial_response_
.Pass());
70 network_service_
->CreateURLLoader(GetProxy(&loader
));
71 URLRequestPtr
request(URLRequest::New());
73 request
->auto_follow_redirects
= true;
75 // |loader| will be pass to the OnResponseReceived method through a
76 // callback. Because order of evaluation is undefined, a reference to the
77 // raw pointer is needed.
78 URLLoader
* raw_loader
= loader
.get();
81 base::Bind(&HTMLViewerApplication::OnResponseReceived
,
82 base::Unretained(this), base::Passed(&loader
),
83 base::Passed(&provider
)));
88 void OnResponseReceived(URLLoaderPtr loader
,
89 ServiceProviderPtr provider
,
90 URLResponsePtr response
) {
91 new HTMLDocument(provider
.Pass(), response
.Pass(), shell_
.get(),
92 compositor_thread_
, web_media_player_factory_
);
97 NetworkServicePtr network_service_
;
98 URLResponsePtr initial_response_
;
99 scoped_refptr
<base::MessageLoopProxy
> compositor_thread_
;
100 WebMediaPlayerFactory
* web_media_player_factory_
;
103 class ContentHandlerImpl
: public InterfaceImpl
<ContentHandler
> {
105 ContentHandlerImpl(scoped_refptr
<base::MessageLoopProxy
> compositor_thread
,
106 WebMediaPlayerFactory
* web_media_player_factory
)
107 : compositor_thread_(compositor_thread
),
108 web_media_player_factory_(web_media_player_factory
) {}
109 ~ContentHandlerImpl() override
{}
112 // Overridden from ContentHandler:
113 void StartApplication(ShellPtr shell
, URLResponsePtr response
) override
{
114 new HTMLViewerApplication(shell
.Pass(), response
.Pass(), compositor_thread_
,
115 web_media_player_factory_
);
118 scoped_refptr
<base::MessageLoopProxy
> compositor_thread_
;
119 WebMediaPlayerFactory
* web_media_player_factory_
;
121 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl
);
124 class HTMLViewer
: public ApplicationDelegate
,
125 public InterfaceFactory
<ContentHandler
> {
127 HTMLViewer() : compositor_thread_("compositor thread") {}
129 ~HTMLViewer() override
{ blink::shutdown(); }
132 // Overridden from ApplicationDelegate:
133 void Initialize(ApplicationImpl
* app
) override
{
134 blink_platform_
.reset(new MojoBlinkPlatformImpl(app
));
135 blink::initialize(blink_platform_
.get());
136 #if !defined(COMPONENT_BUILD)
137 base::i18n::InitializeICU();
139 ui::RegisterPathProvider();
141 base::FilePath ui_test_pak_path
;
142 CHECK(PathService::Get(ui::UI_TEST_PAK
, &ui_test_pak_path
));
143 ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path
);
146 base::CommandLine::StringVector command_line_args
;
148 for (const auto& arg
: app
->args())
149 command_line_args
.push_back(base::UTF8ToUTF16(arg
));
150 #elif defined(OS_POSIX)
151 command_line_args
= app
->args();
154 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
155 command_line
->InitFromArgv(command_line_args
);
157 logging::LoggingSettings settings
;
158 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
159 logging::InitLogging(settings
);
161 bool enable_mojo_media_renderer
=
162 command_line
->HasSwitch(kEnableMojoMediaRenderer
);
164 compositor_thread_
.Start();
165 web_media_player_factory_
.reset(new WebMediaPlayerFactory(
166 compositor_thread_
.message_loop_proxy(), enable_mojo_media_renderer
));
169 bool ConfigureIncomingConnection(ApplicationConnection
* connection
) override
{
170 connection
->AddService(this);
174 // Overridden from InterfaceFactory<ContentHandler>
175 void Create(ApplicationConnection
* connection
,
176 InterfaceRequest
<ContentHandler
> request
) override
{
178 new ContentHandlerImpl(compositor_thread_
.message_loop_proxy(),
179 web_media_player_factory_
.get()),
183 scoped_ptr
<MojoBlinkPlatformImpl
> blink_platform_
;
184 base::Thread compositor_thread_
;
185 scoped_ptr
<WebMediaPlayerFactory
> web_media_player_factory_
;
187 DISALLOW_COPY_AND_ASSIGN(HTMLViewer
);
192 MojoResult
MojoMain(MojoHandle shell_handle
) {
193 mojo::ApplicationRunnerChromium
runner(new mojo::HTMLViewer
);
194 return runner
.Run(shell_handle
);