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 "third_party/WebKit/public/web/WebKit.h"
19 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/base/ui_base_paths.h"
22 #include "ui/mojo/init/ui_init.h"
23 #include "v8/include/v8.h"
25 #if defined(OS_LINUX) && !defined(OS_ANDROID)
26 #include "components/font_service/public/cpp/font_loader.h"
29 namespace html_viewer
{
33 // Disables support for (unprefixed) Encrypted Media Extensions.
34 const char kDisableEncryptedMedia
[] = "disable-encrypted-media";
36 // Prevents creation of any output surface.
37 const char kIsHeadless
[] = "is-headless";
39 // Specifies the flags passed to JS engine.
40 const char kJavaScriptFlags
[] = "js-flags";
42 size_t kDesiredMaxMemory
= 20 * 1024 * 1024;
44 // Paths resources are loaded from.
45 const char kResourceResourcesPak
[] = "html_viewer.pak";
46 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
47 const char kResourceNativesBlob
[] = "natives_blob.bin";
48 const char kResourceSnapshotBlob
[] = "snapshot_blob.bin";
51 std::set
<std::string
> GetResourcePaths() {
52 std::set
<std::string
> paths
;
53 paths
.insert(kResourceResourcesPak
);
54 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
55 paths
.insert(kResourceNativesBlob
);
56 paths
.insert(kResourceSnapshotBlob
);
63 GlobalState::GlobalState(mojo::ApplicationImpl
* app
)
65 resource_loader_(app
->shell(), GetResourcePaths()),
67 base::CommandLine::ForCurrentProcess()->HasSwitch(kIsHeadless
)),
69 device_pixel_ratio_(1.f
),
70 discardable_memory_allocator_(kDesiredMaxMemory
),
71 compositor_thread_("compositor thread") {
73 InitIfNecessary(gfx::Size(1024, 1024), 1.f
);
76 GlobalState::~GlobalState() {
77 if (blink_platform_
) {
78 renderer_scheduler_
->Shutdown();
83 void GlobalState::InitIfNecessary(const gfx::Size
& screen_size_in_pixels
,
84 float device_pixel_ratio
) {
88 DCHECK_NE(0.f
, device_pixel_ratio
);
91 device_pixel_ratio_
= device_pixel_ratio
;
92 screen_size_in_pixels_
= screen_size_in_pixels
;
94 if (!resource_loader_
.BlockUntilLoaded()) {
95 // Assume on error we're being shut down.
100 #if defined(OS_LINUX) && !defined(OS_ANDROID)
101 SkFontConfigInterface::SetGlobal(new font_service::FontLoader(app_
));
105 new ui::mojo::UIInit(screen_size_in_pixels
, device_pixel_ratio
));
106 base::DiscardableMemoryAllocator::SetInstance(&discardable_memory_allocator_
);
108 renderer_scheduler_
= scheduler::RendererScheduler::Create();
109 blink_platform_
.reset(new BlinkPlatformImpl(app_
, renderer_scheduler_
.get()));
110 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
111 gin::V8Initializer::LoadV8SnapshotFromFD(
112 resource_loader_
.ReleaseFile(kResourceSnapshotBlob
).TakePlatformFile(),
114 gin::V8Initializer::LoadV8NativesFromFD(
115 resource_loader_
.ReleaseFile(kResourceNativesBlob
).TakePlatformFile(), 0u,
118 blink::initialize(blink_platform_
.get());
119 base::i18n::InitializeICUWithFileDescriptor(
120 resource_loader_
.GetICUFile().TakePlatformFile(),
121 base::MemoryMappedFile::Region::kWholeFile
);
123 ui::RegisterPathProvider();
125 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
127 logging::LoggingSettings settings
;
128 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
129 logging::InitLogging(settings
);
130 // Display process ID, thread ID and timestamp in logs.
131 logging::SetLogItems(true, true, true, false);
133 if (command_line
->HasSwitch(kDisableEncryptedMedia
))
134 blink::WebRuntimeFeatures::enableEncryptedMedia(false);
137 base::File pak_file
= resource_loader_
.ReleaseFile(kResourceResourcesPak
);
138 base::File pak_file_2
= pak_file
.Duplicate();
139 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
140 pak_file
.Pass(), base::MemoryMappedFile::Region::kWholeFile
);
141 // TODO(sky): why is this always using 100?
142 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
143 pak_file_2
.Pass(), ui::SCALE_FACTOR_100P
);
146 compositor_thread_
.Start();
148 media_factory_
.reset(
149 new MediaFactory(compositor_thread_
.task_runner(), app_
->shell()));
151 if (command_line
->HasSwitch(kJavaScriptFlags
)) {
152 std::string
flags(command_line
->GetSwitchValueASCII(kJavaScriptFlags
));
153 v8::V8::SetFlagsFromString(flags
.c_str(), static_cast<int>(flags
.size()));
157 } // namespace html_viewer