summaryrefslogtreecommitdiffstats
path: root/mojo/services/html_viewer/html_viewer.cc
blob: 4db44ad3714ae35c56049a7c11b65d0a054a8b09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h"
#include "mojo/application/application_runner_chromium.h"
#include "mojo/public/c/system/main.h"
#include "mojo/public/cpp/application/application_connection.h"
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/application_impl.h"
#include "mojo/public/cpp/application/interface_factory_impl.h"
#include "mojo/services/html_viewer/html_document_view.h"
#include "mojo/services/html_viewer/mojo_blink_platform_impl.h"
#include "mojo/services/html_viewer/webmediaplayer_factory.h"
#include "mojo/services/public/interfaces/content_handler/content_handler.mojom.h"
#include "third_party/WebKit/public/web/WebKit.h"

#if !defined(COMPONENT_BUILD)
#include "base/i18n/icu_util.h"
#include "base/path_service.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
#endif

namespace mojo {

// Switches for html_viewer to be used with "--args-for". For example:
// --args-for='mojo:html_viewer --enable-mojo-media-renderer'

// Enable mojo::MediaRenderer in media pipeline instead of using the internal
// media::Renderer implementation.
const char kEnableMojoMediaRenderer[] = "enable-mojo-media-renderer";

class HTMLViewer;

class ContentHandlerImpl : public InterfaceImpl<ContentHandler> {
 public:
  ContentHandlerImpl(scoped_refptr<base::MessageLoopProxy> compositor_thread,
                     WebMediaPlayerFactory* web_media_player_factory)
      : compositor_thread_(compositor_thread),
        web_media_player_factory_(web_media_player_factory) {}
  ~ContentHandlerImpl() override {}

 private:
  // Overridden from ContentHandler:
  void StartApplication(ShellPtr shell, URLResponsePtr response) override {
    new HTMLDocumentView(response.Pass(),
                         shell.Pass(),
                         compositor_thread_,
                         web_media_player_factory_);
  }

  scoped_refptr<base::MessageLoopProxy> compositor_thread_;
  WebMediaPlayerFactory* web_media_player_factory_;

  DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl);
};

class HTMLViewer : public ApplicationDelegate,
                   public InterfaceFactory<ContentHandler> {
 public:
  HTMLViewer() : compositor_thread_("compositor thread") {}

  ~HTMLViewer() override { blink::shutdown(); }

 private:
  // Overridden from ApplicationDelegate:
  void Initialize(ApplicationImpl* app) override {
    blink_platform_.reset(new MojoBlinkPlatformImpl(app));
    blink::initialize(blink_platform_.get());
#if !defined(COMPONENT_BUILD)
    base::i18n::InitializeICU();

    ui::RegisterPathProvider();

    base::FilePath ui_test_pak_path;
    CHECK(PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path));
    ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path);
#endif

    base::CommandLine::StringVector command_line_args;
#if defined(OS_WIN)
    for (const auto& arg : app->args())
      command_line_args.push_back(base::UTF8ToUTF16(arg));
#elif defined(OS_POSIX)
    command_line_args = app->args();
#endif

    base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
    command_line->InitFromArgv(command_line_args);

    logging::LoggingSettings settings;
    settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
    logging::InitLogging(settings);

    bool enable_mojo_media_renderer =
        command_line->HasSwitch(kEnableMojoMediaRenderer);

    compositor_thread_.Start();
    web_media_player_factory_.reset(new WebMediaPlayerFactory(
        compositor_thread_.message_loop_proxy(), enable_mojo_media_renderer));
  }

  bool ConfigureIncomingConnection(ApplicationConnection* connection) override {
    connection->AddService(this);
    return true;
  }

  // Overridden from InterfaceFactory<ContentHandler>
  void Create(ApplicationConnection* connection,
              InterfaceRequest<ContentHandler> request) override {
    BindToRequest(
        new ContentHandlerImpl(compositor_thread_.message_loop_proxy(),
                               web_media_player_factory_.get()),
        &request);
  }

  scoped_ptr<MojoBlinkPlatformImpl> blink_platform_;
  base::Thread compositor_thread_;
  scoped_ptr<WebMediaPlayerFactory> web_media_player_factory_;

  DISALLOW_COPY_AND_ASSIGN(HTMLViewer);
};

}  // namespace mojo

MojoResult MojoMain(MojoHandle shell_handle) {
  mojo::ApplicationRunnerChromium runner(new mojo::HTMLViewer);
  return runner.Run(shell_handle);
}