Move StartsWith[ASCII] to base namespace.
[chromium-blink-merge.git] / components / html_viewer / setup.cc
blob3da3601d921dab7fcb0f57998b549338c4b1ba66
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/setup.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 "v8/include/v8.h"
24 #if defined(OS_ANDROID)
25 #include "components/html_viewer/ui_setup_android.h"
26 #else
27 #include "components/html_viewer/ui_setup.h"
28 #endif
30 namespace html_viewer {
32 namespace {
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 kResourceIcudtl[] = "icudtl.dat";
47 const char kResourceResourcesPak[] = "html_viewer_resources.pak";
48 const char kResourceUIPak[] = "ui_test.pak";
49 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
50 const char kResourceNativesBlob[] = "natives_blob.bin";
51 const char kResourceSnapshotBlob[] = "snapshot_blob.bin";
52 #endif
54 std::set<std::string> GetResourcePaths() {
55 std::set<std::string> paths;
56 paths.insert(kResourceResourcesPak);
57 paths.insert(kResourceIcudtl);
58 paths.insert(kResourceUIPak);
59 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
60 paths.insert(kResourceNativesBlob);
61 paths.insert(kResourceSnapshotBlob);
62 #endif
63 return paths;
66 } // namespace
68 Setup::Setup(mojo::ApplicationImpl* app)
69 : app_(app),
70 resource_loader_(app->shell(), GetResourcePaths()),
71 is_headless_(
72 base::CommandLine::ForCurrentProcess()->HasSwitch(kIsHeadless)),
73 did_init_(false),
74 device_pixel_ratio_(1.f),
75 discardable_memory_allocator_(kDesiredMaxMemory),
76 compositor_thread_("compositor thread") {
77 if (is_headless_)
78 InitIfNecessary(gfx::Size(1024, 1024), 1.f);
81 Setup::~Setup() {
82 if (blink_platform_) {
83 renderer_scheduler_->Shutdown();
84 blink::shutdown();
88 void Setup::InitIfNecessary(const gfx::Size& screen_size_in_pixels,
89 float device_pixel_ratio) {
90 if (did_init_)
91 return;
93 DCHECK_NE(0.f, device_pixel_ratio);
95 did_init_ = true;
96 device_pixel_ratio_ = device_pixel_ratio;
97 screen_size_in_pixels_ = screen_size_in_pixels;
99 if (!resource_loader_.BlockUntilLoaded()) {
100 // Assume on error we're being shut down.
101 app_->Terminate();
102 return;
105 ui_setup_.reset(new UISetup(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(),
113 0u, 0u);
114 gin::V8Initializer::LoadV8NativesFromFD(
115 resource_loader_.ReleaseFile(kResourceNativesBlob).TakePlatformFile(), 0u,
116 0u);
117 #endif
118 blink::initialize(blink_platform_.get());
119 base::i18n::InitializeICUWithFileDescriptor(
120 resource_loader_.ReleaseFile(kResourceIcudtl).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 // TODO(fsamuel): Slimming paint currently crashes mandoline. Investigate.
134 // See http://crbug.com/499353.
135 blink::WebRuntimeFeatures::enableSlimmingPaint(false);
137 if (command_line->HasSwitch(kDisableEncryptedMedia))
138 blink::WebRuntimeFeatures::enableEncryptedMedia(false);
140 if (!is_headless_) {
141 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
142 resource_loader_.ReleaseFile(kResourceResourcesPak),
143 base::MemoryMappedFile::Region::kWholeFile);
144 // TODO(sky): why is this always using 100?
145 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
146 resource_loader_.ReleaseFile(kResourceUIPak), ui::SCALE_FACTOR_100P);
149 compositor_thread_.Start();
151 media_factory_.reset(
152 new MediaFactory(compositor_thread_.message_loop_proxy(), app_->shell()));
154 if (command_line->HasSwitch(kJavaScriptFlags)) {
155 std::string flags(command_line->GetSwitchValueASCII(kJavaScriptFlags));
156 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
160 } // namespace html_viewer