1 // Copyright 2017 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 #ifndef CONTENT_CHILD_CHILD_PROCESS_SANDBOX_SUPPORT_IMPL_LINUX_H_
6 #define CONTENT_CHILD_CHILD_PROCESS_SANDBOX_SUPPORT_IMPL_LINUX_H_
7 
8 #include <stdint.h>
9 
10 #include <map>
11 
12 #include "base/macros.h"
13 #include "base/synchronization/lock.h"
14 #include "base/thread_annotations.h"
15 #include "components/services/font/public/cpp/font_loader.h"
16 #include "third_party/blink/public/platform/linux/web_sandbox_support.h"
17 #include "third_party/skia/include/core/SkRefCnt.h"
18 #include "ui/gfx/font_fallback_linux.h"
19 
20 namespace blink {
21 struct WebFontRenderStyle;
22 }
23 
24 namespace content {
25 
26 // Child-process implementation of the Blink interface that sandboxed processes
27 // use to obtain data from the privileged process (browser), which would
28 // otherwise be blocked by the sandbox.
29 class WebSandboxSupportLinux : public blink::WebSandboxSupport {
30  public:
31   explicit WebSandboxSupportLinux(sk_sp<font_service::FontLoader> font_loader);
32   ~WebSandboxSupportLinux() override;
33 
34   // |fallback_font| will be filled with a font family which provides glyphs for
35   // the Unicode code point specified by |character|, a UTF-32 character.
36   // |preferred_locale| contains the preferred locale identifier for
37   // |character|. Returns false if the request could not be satisfied.
38   bool GetFallbackFontForCharacter(
39       blink::WebUChar32 character,
40       const char* preferred_locale,
41       gfx::FallbackFontData* fallback_font) override;
42 
43   // Matches a font uniquely by postscript name or full font name.  Used in
44   // Blink for @font-face { src: local(arg) } matching.  Provide full font name
45   // or postscript name as argument font_unique_name in UTF-8. |fallback_font|
46   // contains a filename and fontconfig interface id if a match was found.
47   // Returns false, otherwise.
48   bool MatchFontByPostscriptNameOrFullFontName(
49       const char* font_unique_name,
50       gfx::FallbackFontData* fallback_font) override;
51 
52   // Returns rendering settings for a provided font family, size, and style.
53   // |size_and_style| stores the bold setting in its least-significant bit, the
54   // italic setting in its second-least-significant bit, and holds the requested
55   // size in pixels into its remaining bits.
56   void GetWebFontRenderStyleForStrike(const char* family,
57                                       int size,
58                                       bool is_bold,
59                                       bool is_italic,
60                                       float device_scale_factor,
61                                       blink::WebFontRenderStyle* out) override;
62 
63  private:
64   // Blink calls GetFallbackFontForCharacter frequently, so the results are
65   // cached. The cache is protected by this lock.
66   base::Lock lock_;
67   // Maps unicode chars to their fallback fonts.
68   std::map<int32_t, gfx::FallbackFontData> unicode_font_families_
69       GUARDED_BY(lock_);
70 
71   sk_sp<font_service::FontLoader> font_loader_;
72 
73   DISALLOW_COPY_AND_ASSIGN(WebSandboxSupportLinux);
74 };
75 
76 }  // namespace content
77 
78 #endif  // CONTENT_CHILD_CHILD_PROCESS_SANDBOX_SUPPORT_IMPL_LINUX_H_
79