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 // Specifies the flags passed to JS engine.
38 const char kJavaScriptFlags
[] = "js-flags";
40 size_t kDesiredMaxMemory
= 20 * 1024 * 1024;
42 // Paths resources are loaded from.
43 const char kResourceResourcesPak
[] = "html_viewer.pak";
44 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
45 const char kResourceNativesBlob
[] = "natives_blob.bin";
46 const char kResourceSnapshotBlob
[] = "snapshot_blob.bin";
49 std::set
<std::string
> GetResourcePaths() {
50 std::set
<std::string
> paths
;
51 paths
.insert(kResourceResourcesPak
);
52 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
53 paths
.insert(kResourceNativesBlob
);
54 paths
.insert(kResourceSnapshotBlob
);
61 GlobalState::GlobalState(mojo::ApplicationImpl
* app
)
63 resource_loader_(app
->shell(), GetResourcePaths()),
65 device_pixel_ratio_(1.f
),
66 discardable_memory_allocator_(kDesiredMaxMemory
),
67 compositor_thread_("compositor thread") {
70 GlobalState::~GlobalState() {
71 if (blink_platform_
) {
72 renderer_scheduler_
->Shutdown();
75 #if defined(OS_LINUX) && !defined(OS_ANDROID)
76 if (font_loader_
.get()) {
77 SkFontConfigInterface::SetGlobal(nullptr);
78 // FontLoader is ref counted. We need to explicitly shutdown the background
79 // thread, otherwise the background thread may be shutdown after the app is
80 // torn down, when we're in a bad state.
81 font_loader_
->Shutdown();
86 void GlobalState::InitIfNecessary(const gfx::Size
& screen_size_in_pixels
,
87 float device_pixel_ratio
) {
91 DCHECK_NE(0.f
, device_pixel_ratio
);
94 device_pixel_ratio_
= device_pixel_ratio
;
95 screen_size_in_pixels_
= screen_size_in_pixels
;
97 if (!resource_loader_
.BlockUntilLoaded()) {
98 // Assume on error we're being shut down.
103 #if defined(OS_LINUX) && !defined(OS_ANDROID)
104 font_loader_
= skia::AdoptRef(new font_service::FontLoader(app_
));
105 SkFontConfigInterface::SetGlobal(font_loader_
.get());
109 new ui::mojo::UIInit(screen_size_in_pixels
, device_pixel_ratio
));
110 base::DiscardableMemoryAllocator::SetInstance(&discardable_memory_allocator_
);
112 renderer_scheduler_
= scheduler::RendererScheduler::Create();
113 blink_platform_
.reset(new BlinkPlatformImpl(app_
, renderer_scheduler_
.get()));
114 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
115 gin::V8Initializer::LoadV8SnapshotFromFD(
116 resource_loader_
.ReleaseFile(kResourceSnapshotBlob
).TakePlatformFile(),
118 gin::V8Initializer::LoadV8NativesFromFD(
119 resource_loader_
.ReleaseFile(kResourceNativesBlob
).TakePlatformFile(), 0u,
122 blink::initialize(blink_platform_
.get());
123 base::i18n::InitializeICUWithFileDescriptor(
124 resource_loader_
.GetICUFile().TakePlatformFile(),
125 base::MemoryMappedFile::Region::kWholeFile
);
127 ui::RegisterPathProvider();
129 mojo::logging::InitLogging();
131 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
133 if (command_line
->HasSwitch(kDisableEncryptedMedia
))
134 blink::WebRuntimeFeatures::enableEncryptedMedia(false);
136 base::File pak_file
= resource_loader_
.ReleaseFile(kResourceResourcesPak
);
137 base::File pak_file_2
= pak_file
.Duplicate();
138 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
139 pak_file
.Pass(), base::MemoryMappedFile::Region::kWholeFile
);
140 // TODO(sky): why is this always using 100?
141 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
142 pak_file_2
.Pass(), ui::SCALE_FACTOR_100P
);
144 compositor_thread_
.Start();
146 media_factory_
.reset(
147 new MediaFactory(compositor_thread_
.task_runner(), app_
->shell()));
149 if (command_line
->HasSwitch(kJavaScriptFlags
)) {
150 std::string
flags(command_line
->GetSwitchValueASCII(kJavaScriptFlags
));
151 v8::V8::SetFlagsFromString(flags
.c_str(), static_cast<int>(flags
.size()));
155 } // namespace html_viewer