Don't preload rarely seen large images
[chromium-blink-merge.git] / components / html_viewer / global_state.cc
blob67ef469cb184268a77ac0b3d3c6b6714a9290629
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 kResourceIcudtl[] = "icudtl.dat";
42 const char kResourceResourcesPak[] = "html_viewer_resources.pak";
43 const char kResourceUIPak[] = "ui_test.pak";
44 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
45 const char kResourceNativesBlob[] = "natives_blob.bin";
46 const char kResourceSnapshotBlob[] = "snapshot_blob.bin";
47 #endif
49 std::set<std::string> GetResourcePaths() {
50 std::set<std::string> paths;
51 paths.insert(kResourceResourcesPak);
52 paths.insert(kResourceIcudtl);
53 paths.insert(kResourceUIPak);
54 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
55 paths.insert(kResourceNativesBlob);
56 paths.insert(kResourceSnapshotBlob);
57 #endif
58 return paths;
61 } // namespace
63 GlobalState::GlobalState(mojo::ApplicationImpl* app)
64 : app_(app),
65 resource_loader_(app->shell(), GetResourcePaths()),
66 is_headless_(
67 base::CommandLine::ForCurrentProcess()->HasSwitch(kIsHeadless)),
68 did_init_(false),
69 device_pixel_ratio_(1.f),
70 discardable_memory_allocator_(kDesiredMaxMemory),
71 compositor_thread_("compositor thread") {
72 if (is_headless_)
73 InitIfNecessary(gfx::Size(1024, 1024), 1.f);
76 GlobalState::~GlobalState() {
77 if (blink_platform_) {
78 renderer_scheduler_->Shutdown();
79 blink::shutdown();
83 void GlobalState::InitIfNecessary(const gfx::Size& screen_size_in_pixels,
84 float device_pixel_ratio) {
85 if (did_init_)
86 return;
88 DCHECK_NE(0.f, device_pixel_ratio);
90 did_init_ = true;
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.
96 app_->Terminate();
97 return;
100 ui_init_.reset(
101 new ui::mojo::UIInit(screen_size_in_pixels, device_pixel_ratio));
102 base::DiscardableMemoryAllocator::SetInstance(&discardable_memory_allocator_);
104 renderer_scheduler_ = scheduler::RendererScheduler::Create();
105 blink_platform_.reset(new BlinkPlatformImpl(app_, renderer_scheduler_.get()));
106 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
107 gin::V8Initializer::LoadV8SnapshotFromFD(
108 resource_loader_.ReleaseFile(kResourceSnapshotBlob).TakePlatformFile(),
109 0u, 0u);
110 gin::V8Initializer::LoadV8NativesFromFD(
111 resource_loader_.ReleaseFile(kResourceNativesBlob).TakePlatformFile(), 0u,
112 0u);
113 #endif
114 blink::initialize(blink_platform_.get());
115 base::i18n::InitializeICUWithFileDescriptor(
116 resource_loader_.ReleaseFile(kResourceIcudtl).TakePlatformFile(),
117 base::MemoryMappedFile::Region::kWholeFile);
119 ui::RegisterPathProvider();
121 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
123 logging::LoggingSettings settings;
124 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
125 logging::InitLogging(settings);
126 // Display process ID, thread ID and timestamp in logs.
127 logging::SetLogItems(true, true, true, false);
129 if (command_line->HasSwitch(kDisableEncryptedMedia))
130 blink::WebRuntimeFeatures::enableEncryptedMedia(false);
132 if (!is_headless_) {
133 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
134 resource_loader_.ReleaseFile(kResourceResourcesPak),
135 base::MemoryMappedFile::Region::kWholeFile);
136 // TODO(sky): why is this always using 100?
137 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
138 resource_loader_.ReleaseFile(kResourceUIPak), ui::SCALE_FACTOR_100P);
141 compositor_thread_.Start();
143 media_factory_.reset(
144 new MediaFactory(compositor_thread_.task_runner(), app_->shell()));
146 if (command_line->HasSwitch(kJavaScriptFlags)) {
147 std::string flags(command_line->GetSwitchValueASCII(kJavaScriptFlags));
148 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
152 } // namespace html_viewer