OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 <string> | |
6 | |
7 #include "base/at_exit.h" | |
8 #include "base/command_line.h" | |
9 #include "base/files/file_path.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/path_service.h" | |
12 #include "base/run_loop.h" | |
13 #include "base/threading/thread.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "blimp/client/app/blimp_startup.h" | |
16 #include "blimp/client/app/linux/blimp_client_context_delegate_linux.h" | |
17 #include "blimp/client/app/linux/blimp_display_manager.h" | |
18 #include "blimp/client/app/linux/blimp_display_manager_delegate_main.h" | |
19 #include "blimp/client/public/blimp_client_context.h" | |
20 #include "blimp/client/public/contents/blimp_navigation_controller.h" | |
21 #include "blimp/client/support/compositor/compositor_dependencies_impl.h" | |
22 #include "components/pref_registry/pref_registry_syncable.h" | |
23 #include "components/prefs/command_line_pref_store.h" | |
24 #include "components/prefs/in_memory_pref_store.h" | |
25 #include "components/prefs/pref_service.h" | |
26 #include "components/prefs/pref_service_factory.h" | |
27 #include "skia/ext/fontmgr_default_linux.h" | |
28 #include "third_party/skia/include/ports/SkFontConfigInterface.h" | |
29 #include "third_party/skia/include/ports/SkFontMgr.h" | |
30 #include "third_party/skia/include/ports/SkFontMgr_android.h" | |
31 #include "ui/base/resource/resource_bundle.h" | |
32 #include "ui/gfx/x/x11_connection.h" | |
33 | |
34 namespace { | |
35 // Specifies directory where android fonts are stored. | |
36 const char kAndroidFontsPath[] = "android-fonts-path"; | |
37 const char kDefaultUrl[] = "https://www.google.com"; | |
38 constexpr int kWindowWidth = 800; | |
39 constexpr int kWindowHeight = 600; | |
40 | |
41 class BlimpShellCommandLinePrefStore : public CommandLinePrefStore { | |
42 public: | |
43 explicit BlimpShellCommandLinePrefStore(const base::CommandLine* command_line) | |
44 : CommandLinePrefStore(command_line) { | |
45 blimp::client::BlimpClientContext::ApplyBlimpSwitches(this); | |
46 } | |
47 | |
48 protected: | |
49 ~BlimpShellCommandLinePrefStore() override = default; | |
50 }; | |
51 | |
52 bool HasAndroidFontSwitch() { | |
53 return base::CommandLine::ForCurrentProcess()->HasSwitch(kAndroidFontsPath); | |
54 } | |
55 | |
56 std::string GetAndroidFontsDirectory() { | |
57 std::string android_fonts_dir = | |
58 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
59 kAndroidFontsPath); | |
60 if (android_fonts_dir.size() > 0 && android_fonts_dir.back() != '/') { | |
61 android_fonts_dir += '/'; | |
62 } | |
63 return android_fonts_dir; | |
64 } | |
65 | |
66 sk_sp<SkFontMgr> CreateAndroidFontMgr(std::string android_fonts_dir) { | |
67 SkFontMgr_Android_CustomFonts custom; | |
68 custom.fSystemFontUse = | |
69 SkFontMgr_Android_CustomFonts::SystemFontUse::kOnlyCustom; | |
70 custom.fBasePath = android_fonts_dir.c_str(); | |
71 | |
72 std::string font_config; | |
73 std::string fallback_font_config; | |
74 if (android_fonts_dir.find("kitkat") != std::string::npos) { | |
75 font_config = android_fonts_dir + "system_fonts.xml"; | |
76 fallback_font_config = android_fonts_dir + "fallback_fonts.xml"; | |
77 custom.fFallbackFontsXml = fallback_font_config.c_str(); | |
78 } else { | |
79 font_config = android_fonts_dir + "fonts.xml"; | |
80 custom.fFallbackFontsXml = nullptr; | |
81 } | |
82 custom.fFontsXml = font_config.c_str(); | |
83 custom.fIsolated = true; | |
84 | |
85 return SkFontMgr_New_Android(&custom); | |
86 } | |
87 | |
88 void SetupAndroidFontManager() { | |
89 if (HasAndroidFontSwitch()) { | |
90 SetDefaultSkiaFactory(CreateAndroidFontMgr(GetAndroidFontsDirectory())); | |
91 } | |
92 } | |
93 | |
94 void InitializeResourceBundle() { | |
95 base::FilePath pak_file; | |
96 bool pak_file_valid = base::PathService::Get(base::DIR_MODULE, &pak_file); | |
97 CHECK(pak_file_valid); | |
98 pak_file = pak_file.Append(FILE_PATH_LITERAL("blimp_shell.pak")); | |
99 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file); | |
100 } | |
101 | |
102 } // namespace | |
103 | |
104 int main(int argc, const char**argv) { | |
105 base::AtExitManager at_exit; | |
106 base::CommandLine::Init(argc, argv); | |
107 SetupAndroidFontManager(); | |
108 | |
109 CHECK(gfx::InitializeThreadedX11()); | |
110 | |
111 blimp::client::InitializeLogging(); | |
112 blimp::client::InitializeMainMessageLoop(); | |
113 InitializeResourceBundle(); | |
114 | |
115 base::Thread io_thread("BlimpIOThread"); | |
116 base::Thread::Options options; | |
117 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
118 io_thread.StartWithOptions(options); | |
119 | |
120 // Creating this using "new" and passing to context using "WrapUnique" as | |
121 // opposed to "MakeUnique" because we'll need to pass the compositor | |
122 // dependencies to the display manager as well. | |
123 blimp::client::CompositorDependencies* compositor_dependencies = | |
124 new blimp::client::CompositorDependenciesImpl(); | |
125 // Creating the context delegate before the context so that the context is | |
126 // destroyed before the delegate. | |
127 std::unique_ptr<blimp::client::BlimpClientContextDelegate> context_delegate = | |
128 base::MakeUnique<blimp::client::BlimpClientContextDelegateLinux>(); | |
129 | |
130 // Create PrefRegistry and register blimp preferences with it. | |
131 scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry = | |
132 new ::user_prefs::PrefRegistrySyncable(); | |
133 blimp::client::BlimpClientContext::RegisterPrefs(pref_registry.get()); | |
134 | |
135 PrefServiceFactory pref_service_factory; | |
136 | |
137 // Create command line and user preference stores. | |
138 pref_service_factory.set_command_line_prefs( | |
139 make_scoped_refptr(new BlimpShellCommandLinePrefStore( | |
140 base::CommandLine::ForCurrentProcess()))); | |
141 pref_service_factory.set_user_prefs(new InMemoryPrefStore()); | |
142 | |
143 // Create a PrefService binding the PrefRegistry to the pref stores. | |
144 // The PrefService owns the PrefRegistry and pref stores. | |
145 std::unique_ptr<PrefService> pref_service = | |
146 pref_service_factory.Create(pref_registry.get()); | |
147 | |
148 auto context = base::WrapUnique<blimp::client::BlimpClientContext>( | |
149 blimp::client::BlimpClientContext::Create( | |
150 io_thread.task_runner(), io_thread.task_runner(), | |
151 base::WrapUnique(compositor_dependencies), pref_service.get())); | |
152 context->SetDelegate(context_delegate.get()); | |
153 | |
154 context->Connect(); | |
155 | |
156 // If there is a non-switch argument to the command line, load that url. | |
157 base::CommandLine::StringVector args = | |
158 base::CommandLine::ForCurrentProcess()->GetArgs(); | |
159 std::string url = args.size() > 0 ? args[0] : kDefaultUrl; | |
160 std::unique_ptr<blimp::client::BlimpContents> contents = | |
161 context->CreateBlimpContents(nullptr); | |
162 contents->GetNavigationController().LoadURL(GURL(url)); | |
163 | |
164 std::unique_ptr<blimp::client::BlimpDisplayManagerDelegate> | |
165 display_manager_delegate = | |
166 base::MakeUnique<blimp::client::BlimpDisplayManagerDelegateMain>(); | |
167 blimp::client::BlimpDisplayManager display_manager( | |
168 display_manager_delegate.get(), compositor_dependencies); | |
169 display_manager.SetWindowSize(gfx::Size(kWindowWidth, kWindowHeight)); | |
170 display_manager.SetBlimpContents(std::move(contents)); | |
171 | |
172 base::RunLoop().Run(); | |
173 } | |
OLD | NEW |