Update mojo sdk to rev 59145288bae55b0fce4276b017df6a1117bcf00f
[chromium-blink-merge.git] / mojo / services / html_viewer / html_viewer.cc
blob38508e6b877862e0612d8c74d2a554b89a13f77c
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"
30 #endif
32 namespace mojo {
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";
41 class HTMLViewer;
43 class HTMLViewerApplication : public Application {
44 public:
45 HTMLViewerApplication(ShellPtr shell,
46 URLResponsePtr response,
47 scoped_refptr<base::MessageLoopProxy> compositor_thread,
48 WebMediaPlayerFactory* web_media_player_factory)
49 : url_(response->url),
50 shell_(shell.Pass()),
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());
68 } else {
69 URLLoaderPtr loader;
70 network_service_->CreateURLLoader(GetProxy(&loader));
71 URLRequestPtr request(URLRequest::New());
72 request->url = url_;
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();
79 raw_loader->Start(
80 request.Pass(),
81 base::Bind(&HTMLViewerApplication::OnResponseReceived,
82 base::Unretained(this), base::Passed(&loader),
83 base::Passed(&provider)));
87 private:
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_);
95 String url_;
96 ShellPtr shell_;
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> {
104 public:
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 {}
111 private:
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> {
126 public:
127 HTMLViewer() : compositor_thread_("compositor thread") {}
129 ~HTMLViewer() override { blink::shutdown(); }
131 private:
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);
144 #endif
146 base::CommandLine::StringVector command_line_args;
147 #if defined(OS_WIN)
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();
152 #endif
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);
171 return true;
174 // Overridden from InterfaceFactory<ContentHandler>
175 void Create(ApplicationConnection* connection,
176 InterfaceRequest<ContentHandler> request) override {
177 BindToRequest(
178 new ContentHandlerImpl(compositor_thread_.message_loop_proxy(),
179 web_media_player_factory_.get()),
180 &request);
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);
190 } // namespace mojo
192 MojoResult MojoMain(MojoHandle shell_handle) {
193 mojo::ApplicationRunnerChromium runner(new mojo::HTMLViewer);
194 return runner.Run(shell_handle);