1 // Copyright 2018 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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_UNIQUE_NAME_LOOKUP_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_UNIQUE_NAME_LOOKUP_H_
7 
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "build/build_config.h"
11 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
12 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
13 #include "third_party/skia/include/core/SkRefCnt.h"
14 #include "third_party/skia/include/core/SkTypeface.h"
15 
16 #if defined(OS_ANDROID) || defined(OS_WIN)
17 #include "third_party/blink/public/common/font_unique_name_lookup/font_table_matcher.h"
18 #endif
19 
20 #include <memory>
21 
22 namespace blink {
23 
24 class FontTableMatcher;
25 
26 class FontUniqueNameLookup {
27   USING_FAST_MALLOC(FontUniqueNameLookup);
28 
29  public:
30   // Factory function to construct a platform specific font unique name lookup
31   // instance. Client must not use this directly as it is thread
32   // specific. Retrieve it from FontGlobalContext instead.
33   static std::unique_ptr<FontUniqueNameLookup> GetPlatformUniqueNameLookup();
34 
35   virtual sk_sp<SkTypeface> MatchUniqueName(const String& font_unique_name) = 0;
36 
37   virtual ~FontUniqueNameLookup() = default;
38 
39   // Below: Methods for asynchronously retrieving the FontUniqueNameLookup
40   // table. Currently needed on Windows, on other platforms the implementation
41   // is synchronous.
42 
43   // Determines whether fonts can be uniquely matched synchronously.
IsFontUniqueNameLookupReadyForSyncLookup()44   virtual bool IsFontUniqueNameLookupReadyForSyncLookup() { return true; }
45 
46   // If fonts cannot be uniquely matched synchronously, send a Mojo IPC call to
47   // prepare the lookup table, and wait for the callback. Once the callback has
48   // been called, IsFontUniqueNameLookupReadyForSyncLookup() will become true.
49   // PrepareFontUniqueNameLookup() must not be called if
50   // IsFontUniqueNameLookupReadyForSyncLookup() is true already.
51   using NotifyFontUniqueNameLookupReady = base::OnceCallback<void()>;
PrepareFontUniqueNameLookup(NotifyFontUniqueNameLookupReady callback)52   virtual void PrepareFontUniqueNameLookup(
53       NotifyFontUniqueNameLookupReady callback) {
54     NOTREACHED();
55   }
56 
57  protected:
58   FontUniqueNameLookup();
59 
60   // Windows and Android share the concept of connecting to a Mojo service for
61   // retrieving a ReadOnlySharedMemoryRegion with the lookup table in it.
62 #if defined(OS_WIN) || defined(OS_ANDROID)
63   std::unique_ptr<FontTableMatcher> font_table_matcher_;
64 #endif
65 
66   DISALLOW_COPY_AND_ASSIGN(FontUniqueNameLookup);
67 };
68 
69 }  // namespace blink
70 
71 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_FONT_UNIQUE_NAME_LOOKUP_
72