Update V8 to version 4.6.35.
[chromium-blink-merge.git] / components / html_viewer / global_state.cc
blobec0f570951c647531596cc5dc7c4cc485ed3ef25
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"
7 #include <string>
9 #include "base/bind.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 namespace html_viewer {
27 namespace {
29 // Disables support for (unprefixed) Encrypted Media Extensions.
30 const char kDisableEncryptedMedia[] = "disable-encrypted-media";
32 // Prevents creation of any output surface.
33 const char kIsHeadless[] = "is-headless";
35 // Specifies the flags passed to JS engine.
36 const char kJavaScriptFlags[] = "js-flags";
38 size_t kDesiredMaxMemory = 20 * 1024 * 1024;
40 // Paths resources are loaded from.
41 const char kResourceResourcesPak[] = "html_viewer.pak";
42 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
43 const char kResourceNativesBlob[] = "natives_blob.bin";
44 const char kResourceSnapshotBlob[] = "snapshot_blob.bin";
45 #endif
47 std::set<std::string> GetResourcePaths() {
48 std::set<std::string> paths;
49 paths.insert(kResourceResourcesPak);
50 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
51 paths.insert(kResourceNativesBlob);
52 paths.insert(kResourceSnapshotBlob);
53 #endif
54 return paths;
57 } // namespace
59 GlobalState::GlobalState(mojo::ApplicationImpl* app)
60 : app_(app),
61 resource_loader_(app->shell(), GetResourcePaths()),
62 is_headless_(
63 base::CommandLine::ForCurrentProcess()->HasSwitch(kIsHeadless)),
64 did_init_(false),
65 device_pixel_ratio_(1.f),
66 discardable_memory_allocator_(kDesiredMaxMemory),
67 compositor_thread_("compositor thread") {
68 if (is_headless_)
69 InitIfNecessary(gfx::Size(1024, 1024), 1.f);
72 GlobalState::~GlobalState() {
73 if (blink_platform_) {
74 renderer_scheduler_->Shutdown();
75 blink::shutdown();
79 void GlobalState::InitIfNecessary(const gfx::Size& screen_size_in_pixels,
80 float device_pixel_ratio) {
81 if (did_init_)
82 return;
84 DCHECK_NE(0.f, device_pixel_ratio);
86 did_init_ = true;
87 device_pixel_ratio_ = device_pixel_ratio;
88 screen_size_in_pixels_ = screen_size_in_pixels;
90 if (!resource_loader_.BlockUntilLoaded()) {
91 // Assume on error we're being shut down.
92 app_->Terminate();
93 return;
96 ui_init_.reset(
97 new ui::mojo::UIInit(screen_size_in_pixels, device_pixel_ratio));
98 base::DiscardableMemoryAllocator::SetInstance(&discardable_memory_allocator_);
100 renderer_scheduler_ = scheduler::RendererScheduler::Create();
101 blink_platform_.reset(new BlinkPlatformImpl(app_, renderer_scheduler_.get()));
102 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
103 gin::V8Initializer::LoadV8SnapshotFromFD(
104 resource_loader_.ReleaseFile(kResourceSnapshotBlob).TakePlatformFile(),
105 0u, 0u);
106 gin::V8Initializer::LoadV8NativesFromFD(
107 resource_loader_.ReleaseFile(kResourceNativesBlob).TakePlatformFile(), 0u,
108 0u);
109 #endif
110 blink::initialize(blink_platform_.get());
111 base::i18n::InitializeICUWithFileDescriptor(
112 resource_loader_.GetICUFile().TakePlatformFile(),
113 base::MemoryMappedFile::Region::kWholeFile);
115 ui::RegisterPathProvider();
117 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
119 logging::LoggingSettings settings;
120 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
121 logging::InitLogging(settings);
122 // Display process ID, thread ID and timestamp in logs.
123 logging::SetLogItems(true, true, true, false);
125 if (command_line->HasSwitch(kDisableEncryptedMedia))
126 blink::WebRuntimeFeatures::enableEncryptedMedia(false);
128 if (!is_headless_) {
129 base::File pak_file = resource_loader_.ReleaseFile(kResourceResourcesPak);
130 base::File pak_file_2 = pak_file.Duplicate();
131 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
132 pak_file.Pass(), base::MemoryMappedFile::Region::kWholeFile);
133 // TODO(sky): why is this always using 100?
134 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
135 pak_file_2.Pass(), ui::SCALE_FACTOR_100P);
138 compositor_thread_.Start();
140 media_factory_.reset(
141 new MediaFactory(compositor_thread_.task_runner(), app_->shell()));
143 if (command_line->HasSwitch(kJavaScriptFlags)) {
144 std::string flags(command_line->GetSwitchValueASCII(kJavaScriptFlags));
145 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
149 } // namespace html_viewer