| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "components/html_viewer/global_state.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/command_line.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "build/build_config.h" | |
| 15 #include "cc/blink/web_layer_impl.h" | |
| 16 #include "cc/layers/layer_settings.h" | |
| 17 #include "components/html_viewer/blink_platform_impl.h" | |
| 18 #include "components/html_viewer/blink_settings_impl.h" | |
| 19 #include "components/html_viewer/media_factory.h" | |
| 20 #include "components/scheduler/renderer/renderer_scheduler.h" | |
| 21 #include "gin/v8_initializer.h" | |
| 22 #include "mojo/logging/init_logging.h" | |
| 23 #include "mojo/services/tracing/public/cpp/tracing_impl.h" | |
| 24 #include "mojo/shell/public/cpp/shell.h" | |
| 25 #include "third_party/WebKit/public/web/WebKit.h" | |
| 26 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" | |
| 27 #include "ui/base/resource/resource_bundle.h" | |
| 28 #include "ui/base/ui_base_paths.h" | |
| 29 #include "ui/gfx/display.h" | |
| 30 #include "ui/mojo/init/ui_init.h" | |
| 31 #include "v8/include/v8.h" | |
| 32 | |
| 33 #if defined(OS_LINUX) && !defined(OS_ANDROID) | |
| 34 #include "components/font_service/public/cpp/font_loader.h" | |
| 35 #endif | |
| 36 | |
| 37 namespace html_viewer { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 // Disables support for (unprefixed) Encrypted Media Extensions. | |
| 42 const char kDisableEncryptedMedia[] = "disable-encrypted-media"; | |
| 43 | |
| 44 // Specifies the flags passed to JS engine. | |
| 45 const char kJavaScriptFlags[] = "js-flags"; | |
| 46 | |
| 47 size_t kDesiredMaxMemory = 20 * 1024 * 1024; | |
| 48 | |
| 49 // Paths resources are loaded from. | |
| 50 const char kResourceResourcesPak[] = "html_viewer.pak"; | |
| 51 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | |
| 52 const char kResourceNativesBlob[] = "natives_blob.bin"; | |
| 53 const char kResourceSnapshotBlob[] = "snapshot_blob.bin"; | |
| 54 #endif | |
| 55 | |
| 56 std::set<std::string> GetResourcePaths() { | |
| 57 std::set<std::string> paths; | |
| 58 paths.insert(kResourceResourcesPak); | |
| 59 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | |
| 60 paths.insert(kResourceNativesBlob); | |
| 61 paths.insert(kResourceSnapshotBlob); | |
| 62 #endif | |
| 63 return paths; | |
| 64 } | |
| 65 | |
| 66 // TODO(sky): convert to using DisplayService. | |
| 67 std::vector<gfx::Display> DisplaysFromSizeAndScale( | |
| 68 const gfx::Size& screen_size_in_pixels, | |
| 69 float device_pixel_ratio) { | |
| 70 std::vector<gfx::Display> displays(1); | |
| 71 displays[0].set_id(2000); | |
| 72 displays[0].SetScaleAndBounds(device_pixel_ratio, | |
| 73 gfx::Rect(screen_size_in_pixels)); | |
| 74 return displays; | |
| 75 } | |
| 76 | |
| 77 } // namespace | |
| 78 | |
| 79 GlobalState::GlobalState(mojo::Shell* shell, const std::string& url) | |
| 80 : shell_(shell), | |
| 81 resource_loader_(shell, GetResourcePaths()), | |
| 82 did_init_(false), | |
| 83 device_pixel_ratio_(1.f), | |
| 84 discardable_memory_allocator_(kDesiredMaxMemory), | |
| 85 compositor_thread_("compositor thread"), | |
| 86 blink_settings_(new BlinkSettingsImpl()) { | |
| 87 tracing_.Initialize(shell, url); | |
| 88 } | |
| 89 | |
| 90 GlobalState::~GlobalState() { | |
| 91 if (blink_platform_) { | |
| 92 renderer_scheduler_->Shutdown(); | |
| 93 blink::shutdown(); | |
| 94 } | |
| 95 #if defined(OS_LINUX) && !defined(OS_ANDROID) | |
| 96 if (font_loader_.get()) { | |
| 97 SkFontConfigInterface::SetGlobal(nullptr); | |
| 98 // FontLoader is ref counted. We need to explicitly shutdown the background | |
| 99 // thread, otherwise the background thread may be shutdown after the app is | |
| 100 // torn down, when we're in a bad state. | |
| 101 font_loader_->Shutdown(); | |
| 102 } | |
| 103 #endif | |
| 104 } | |
| 105 | |
| 106 void GlobalState::InitIfNecessary(const gfx::Size& screen_size_in_pixels, | |
| 107 float device_pixel_ratio) { | |
| 108 if (did_init_) | |
| 109 return; | |
| 110 | |
| 111 DCHECK_NE(0.f, device_pixel_ratio); | |
| 112 | |
| 113 did_init_ = true; | |
| 114 device_pixel_ratio_ = device_pixel_ratio; | |
| 115 screen_size_in_pixels_ = screen_size_in_pixels; | |
| 116 | |
| 117 if (!resource_loader_.BlockUntilLoaded()) { | |
| 118 // Assume on error we're being shut down. | |
| 119 shell_->Quit(); | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 #if defined(OS_LINUX) && !defined(OS_ANDROID) | |
| 124 font_loader_ = skia::AdoptRef(new font_service::FontLoader(shell_)); | |
| 125 SkFontConfigInterface::SetGlobal(font_loader_.get()); | |
| 126 #endif | |
| 127 | |
| 128 ui_init_.reset(new ui::mojo::UIInit( | |
| 129 DisplaysFromSizeAndScale(screen_size_in_pixels_, device_pixel_ratio_))); | |
| 130 base::DiscardableMemoryAllocator::SetInstance(&discardable_memory_allocator_); | |
| 131 | |
| 132 shell_->ConnectToService("mojo:mus", &gpu_service_); | |
| 133 gpu_service_->GetGpuInfo(base::Bind(&GlobalState::GetGpuInfoCallback, | |
| 134 base::Unretained(this))); | |
| 135 | |
| 136 // Use new animation system (cc::AnimationHost). | |
| 137 cc::LayerSettings layer_settings; | |
| 138 layer_settings.use_compositor_animation_timelines = true; | |
| 139 cc_blink::WebLayerImpl::SetLayerSettings(layer_settings); | |
| 140 blink::WebRuntimeFeatures::enableCompositorAnimationTimelines(true); | |
| 141 | |
| 142 renderer_scheduler_ = scheduler::RendererScheduler::Create(); | |
| 143 blink_platform_.reset( | |
| 144 new BlinkPlatformImpl(this, shell_, renderer_scheduler_.get())); | |
| 145 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | |
| 146 gin::V8Initializer::LoadV8SnapshotFromFD( | |
| 147 resource_loader_.ReleaseFile(kResourceSnapshotBlob).TakePlatformFile(), | |
| 148 0u, 0u); | |
| 149 gin::V8Initializer::LoadV8NativesFromFD( | |
| 150 resource_loader_.ReleaseFile(kResourceNativesBlob).TakePlatformFile(), 0u, | |
| 151 0u); | |
| 152 #endif | |
| 153 blink::initialize(blink_platform_.get()); | |
| 154 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 155 base::File pak_file = resource_loader_.ReleaseFile(kResourceResourcesPak); | |
| 156 | |
| 157 bool initialize_ui = true; | |
| 158 #if defined(COMPONENT_BUILD) | |
| 159 if (command_line->HasSwitch("single-process")) | |
| 160 initialize_ui = false; | |
| 161 #endif | |
| 162 if (initialize_ui) { | |
| 163 ui::RegisterPathProvider(); | |
| 164 base::File pak_file_2 = pak_file.Duplicate(); | |
| 165 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion( | |
| 166 std::move(pak_file_2), base::MemoryMappedFile::Region::kWholeFile); | |
| 167 } | |
| 168 | |
| 169 mojo::InitLogging(); | |
| 170 | |
| 171 if (command_line->HasSwitch(kDisableEncryptedMedia)) | |
| 172 blink::WebRuntimeFeatures::enableEncryptedMedia(false); | |
| 173 | |
| 174 blink_settings_->Init(); | |
| 175 | |
| 176 // TODO(sky): why is this always using 100? | |
| 177 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile( | |
| 178 std::move(pak_file), ui::SCALE_FACTOR_100P); | |
| 179 | |
| 180 compositor_thread_.Start(); | |
| 181 | |
| 182 media_factory_.reset( | |
| 183 new MediaFactory(compositor_thread_.task_runner(), shell_)); | |
| 184 | |
| 185 if (command_line->HasSwitch(kJavaScriptFlags)) { | |
| 186 std::string flags(command_line->GetSwitchValueASCII(kJavaScriptFlags)); | |
| 187 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size())); | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 // TODO(rjkroege): These two functions probably do not interoperate correctly | |
| 192 // with MUS. | |
| 193 const mus::mojom::GpuInfo* GlobalState::GetGpuInfo() { | |
| 194 if (gpu_service_) | |
| 195 CHECK(gpu_service_.WaitForIncomingResponse()) <<"Get GPU info failed!"; | |
| 196 return gpu_info_.get(); | |
| 197 } | |
| 198 | |
| 199 void GlobalState::GetGpuInfoCallback(mus::mojom::GpuInfoPtr gpu_info) { | |
| 200 CHECK(gpu_info); | |
| 201 gpu_info_ = std::move(gpu_info); | |
| 202 gpu_service_.reset(); | |
| 203 } | |
| 204 | |
| 205 } // namespace html_viewer | |
| OLD | NEW |