Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / pepper / pepper_truetype_font_list_host.cc
1 // Copyright (c) 2013 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 "content/browser/renderer_host/pepper/pepper_truetype_font_list_host.h"
6
7 #include <algorithm>
8
9 #include "base/numerics/safe_conversions.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "content/browser/renderer_host/pepper/pepper_truetype_font_list.h"
12 #include "content/common/font_list.h"
13 #include "content/public/browser/browser_ppapi_host.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "ppapi/host/dispatch_host_message.h"
16 #include "ppapi/host/host_message_context.h"
17 #include "ppapi/host/resource_message_filter.h"
18 #include "ppapi/proxy/ppapi_messages.h"
19
20 namespace content {
21
22 namespace {
23
24 // Handles the font list request on the blocking pool.
25 class FontMessageFilter : public ppapi::host::ResourceMessageFilter {
26  public:
27   FontMessageFilter();
28
29   // ppapi::host::ResourceMessageFilter implementation.
30   scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage(
31       const IPC::Message& msg) override;
32   int32_t OnResourceMessageReceived(
33       const IPC::Message& msg,
34       ppapi::host::HostMessageContext* context) override;
35
36  private:
37   ~FontMessageFilter() override;
38
39   // Message handlers.
40   int32_t OnHostMsgGetFontFamilies(ppapi::host::HostMessageContext* context);
41   int32_t OnHostMsgGetFontsInFamily(ppapi::host::HostMessageContext* context,
42                                     const std::string& family);
43
44   DISALLOW_COPY_AND_ASSIGN(FontMessageFilter);
45 };
46
47 FontMessageFilter::FontMessageFilter() {}
48
49 FontMessageFilter::~FontMessageFilter() {}
50
51 scoped_refptr<base::TaskRunner> FontMessageFilter::OverrideTaskRunnerForMessage(
52     const IPC::Message& msg) {
53   // Use the blocking pool to get the font list (currently the only message)
54   // Since getting the font list is non-threadsafe on Linux (for versions of
55   // Pango predating 2013), use a sequenced task runner.
56   base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
57   return pool->GetSequencedTaskRunner(
58       pool->GetNamedSequenceToken(kFontListSequenceToken));
59 }
60
61 int32_t FontMessageFilter::OnResourceMessageReceived(
62     const IPC::Message& msg,
63     ppapi::host::HostMessageContext* context) {
64   PPAPI_BEGIN_MESSAGE_MAP(FontMessageFilter, msg)
65     PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
66         PpapiHostMsg_TrueTypeFontSingleton_GetFontFamilies,
67         OnHostMsgGetFontFamilies)
68     PPAPI_DISPATCH_HOST_RESOURCE_CALL(
69         PpapiHostMsg_TrueTypeFontSingleton_GetFontsInFamily,
70         OnHostMsgGetFontsInFamily)
71   PPAPI_END_MESSAGE_MAP()
72   return PP_ERROR_FAILED;
73 }
74
75 int32_t FontMessageFilter::OnHostMsgGetFontFamilies(
76     ppapi::host::HostMessageContext* context) {
77   // OK to use "slow blocking" version since we're on the blocking pool.
78   std::vector<std::string> font_families;
79   GetFontFamilies_SlowBlocking(&font_families);
80   // Sort the names in case the host platform returns them out of order.
81   std::sort(font_families.begin(), font_families.end());
82
83   context->reply_msg =
84       PpapiPluginMsg_TrueTypeFontSingleton_GetFontFamiliesReply(font_families);
85   return base::checked_cast<int32_t>(font_families.size());
86 }
87
88 int32_t FontMessageFilter::OnHostMsgGetFontsInFamily(
89     ppapi::host::HostMessageContext* context,
90     const std::string& family) {
91   // OK to use "slow blocking" version since we're on the blocking pool.
92   std::vector<ppapi::proxy::SerializedTrueTypeFontDesc> fonts_in_family;
93   GetFontsInFamily_SlowBlocking(family, &fonts_in_family);
94
95   context->reply_msg =
96       PpapiPluginMsg_TrueTypeFontSingleton_GetFontsInFamilyReply(
97           fonts_in_family);
98   return base::checked_cast<int32_t>(fonts_in_family.size());
99 }
100
101 }  // namespace
102
103 PepperTrueTypeFontListHost::PepperTrueTypeFontListHost(BrowserPpapiHost* host,
104                                                        PP_Instance instance,
105                                                        PP_Resource resource)
106     : ResourceHost(host->GetPpapiHost(), instance, resource) {
107   AddFilter(scoped_refptr<ppapi::host::ResourceMessageFilter>(
108       new FontMessageFilter()));
109 }
110
111 PepperTrueTypeFontListHost::~PepperTrueTypeFontListHost() {}
112
113 }  // namespace content