1 // Copyright 2015 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 "components/html_viewer/global_state.h"
10 #include "base/command_line.h"
11 #include "base/i18n/icu_util.h"
12 #include "base/logging.h"
13 #include "components/html_viewer/blink_platform_impl.h"
14 #include "components/html_viewer/media_factory.h"
15 #include "components/scheduler/renderer/renderer_scheduler.h"
16 #include "gin/v8_initializer.h"
17 #include "mojo/application/public/cpp/application_impl.h"
18 #include "mojo/logging/init_logging.h"
19 #include "third_party/WebKit/public/web/WebKit.h"
20 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/base/ui_base_paths.h"
23 #include "ui/mojo/init/ui_init.h"
24 #include "v8/include/v8.h"
26 #if defined(OS_LINUX) && !defined(OS_ANDROID)
27 #include "components/font_service/public/cpp/font_loader.h"
30 namespace html_viewer
{
34 // Disables support for (unprefixed) Encrypted Media Extensions.
35 const char kDisableEncryptedMedia
[] = "disable-encrypted-media";
37 // Prevents creation of any output surface.
38 const char kIsHeadless
[] = "is-headless";
40 // Specifies the flags passed to JS engine.
41 const char kJavaScriptFlags
[] = "js-flags";
43 size_t kDesiredMaxMemory
= 20 * 1024 * 1024;
45 // Paths resources are loaded from.
46 const char kResourceResourcesPak
[] = "html_viewer.pak";
47 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
48 const char kResourceNativesBlob
[] = "natives_blob.bin";
49 const char kResourceSnapshotBlob
[] = "snapshot_blob.bin";
52 std::set
<std::string
> GetResourcePaths() {
53 std::set
<std::string
> paths
;
54 paths
.insert(kResourceResourcesPak
);
55 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
56 paths
.insert(kResourceNativesBlob
);
57 paths
.insert(kResourceSnapshotBlob
);
64 GlobalState::GlobalState(mojo::ApplicationImpl
* app
)
66 resource_loader_(app
->shell(), GetResourcePaths()),
68 base::CommandLine::ForCurrentProcess()->HasSwitch(kIsHeadless
)),
70 device_pixel_ratio_(1.f
),
71 discardable_memory_allocator_(kDesiredMaxMemory
),
72 compositor_thread_("compositor thread") {
74 InitIfNecessary(gfx::Size(1024, 1024), 1.f
);
77 GlobalState::~GlobalState() {
78 if (blink_platform_
) {
79 renderer_scheduler_
->Shutdown();
84 void GlobalState::InitIfNecessary(const gfx::Size
& screen_size_in_pixels
,
85 float device_pixel_ratio
) {
89 DCHECK_NE(0.f
, device_pixel_ratio
);
92 device_pixel_ratio_
= device_pixel_ratio
;
93 screen_size_in_pixels_
= screen_size_in_pixels
;
95 if (!resource_loader_
.BlockUntilLoaded()) {
96 // Assume on error we're being shut down.
101 #if defined(OS_LINUX) && !defined(OS_ANDROID)
102 SkFontConfigInterface::SetGlobal(new font_service::FontLoader(app_
));
106 new ui::mojo::UIInit(screen_size_in_pixels
, device_pixel_ratio
));
107 base::DiscardableMemoryAllocator::SetInstance(&discardable_memory_allocator_
);
109 renderer_scheduler_
= scheduler::RendererScheduler::Create();
110 blink_platform_
.reset(new BlinkPlatformImpl(app_
, renderer_scheduler_
.get()));
111 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
112 gin::V8Initializer::LoadV8SnapshotFromFD(
113 resource_loader_
.ReleaseFile(kResourceSnapshotBlob
).TakePlatformFile(),
115 gin::V8Initializer::LoadV8NativesFromFD(
116 resource_loader_
.ReleaseFile(kResourceNativesBlob
).TakePlatformFile(), 0u,
119 blink::initialize(blink_platform_
.get());
120 base::i18n::InitializeICUWithFileDescriptor(
121 resource_loader_
.GetICUFile().TakePlatformFile(),
122 base::MemoryMappedFile::Region::kWholeFile
);
124 ui::RegisterPathProvider();
126 mojo::logging::InitLogging();
128 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
130 if (command_line
->HasSwitch(kDisableEncryptedMedia
))
131 blink::WebRuntimeFeatures::enableEncryptedMedia(false);
134 base::File pak_file
= resource_loader_
.ReleaseFile(kResourceResourcesPak
);
135 base::File pak_file_2
= pak_file
.Duplicate();
136 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
137 pak_file
.Pass(), base::MemoryMappedFile::Region::kWholeFile
);
138 // TODO(sky): why is this always using 100?
139 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
140 pak_file_2
.Pass(), ui::SCALE_FACTOR_100P
);
143 compositor_thread_
.Start();
145 media_factory_
.reset(
146 new MediaFactory(compositor_thread_
.task_runner(), app_
->shell()));
148 if (command_line
->HasSwitch(kJavaScriptFlags
)) {
149 std::string
flags(command_line
->GetSwitchValueASCII(kJavaScriptFlags
));
150 v8::V8::SetFlagsFromString(flags
.c_str(), static_cast<int>(flags
.size()));
154 } // namespace html_viewer