pepper_truetype_font_list_host.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
1b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// found in the LICENSE file.
4b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
5b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "content/browser/renderer_host/pepper/pepper_truetype_font_list_host.h"
6b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
7b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include <algorithm>
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/safe_numerics.h"
10b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "base/threading/sequenced_worker_pool.h"
11b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "content/browser/renderer_host/pepper/pepper_truetype_font_list.h"
12b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "content/common/font_list.h"
13b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "content/public/browser/browser_ppapi_host.h"
14b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
15b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "ppapi/host/dispatch_host_message.h"
16b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "ppapi/host/host_message_context.h"
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/host/resource_message_filter.h"
18b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
20b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)namespace content {
21b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
22b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)namespace {
23b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
24b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// Handles the font list request on the blocking pool.
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class FontMessageFilter : public ppapi::host::ResourceMessageFilter {
26b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) public:
27b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  FontMessageFilter();
28b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
29b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // ppapi::host::ResourceMessageFilter implementation.
30b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage(
31b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      const IPC::Message& msg) OVERRIDE;
32b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  virtual int32_t OnResourceMessageReceived(
33b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      const IPC::Message& msg,
34b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      ppapi::host::HostMessageContext* context) OVERRIDE;
35b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
36b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) private:
37b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  virtual ~FontMessageFilter();
38b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
39b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Message handlers.
40b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  int32_t OnHostMsgGetFontFamilies(ppapi::host::HostMessageContext* context);
41b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  int32_t OnHostMsgGetFontsInFamily(ppapi::host::HostMessageContext* context,
42b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                                    const std::string& family);
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
44b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FontMessageFilter);
45b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)};
46b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)FontMessageFilter::FontMessageFilter() {
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
49b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
50b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)FontMessageFilter::~FontMessageFilter() {
51b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
52b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
53b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)scoped_refptr<base::TaskRunner> FontMessageFilter::OverrideTaskRunnerForMessage(
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const IPC::Message& msg) {
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Use the blocking pool to get the font list (currently the only message)
5658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Since getting the font list is non-threadsafe on Linux (for versions of
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Pango predating 2013), use a sequenced task runner.
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return pool->GetSequencedTaskRunner(
60b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      pool->GetNamedSequenceToken(kFontListSequenceToken));
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int32_t FontMessageFilter::OnResourceMessageReceived(
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const IPC::Message& msg,
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ppapi::host::HostMessageContext* context) {
66b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  IPC_BEGIN_MESSAGE_MAP(FontMessageFilter, msg)
67b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        PpapiHostMsg_TrueTypeFontSingleton_GetFontFamilies,
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        OnHostMsgGetFontFamilies)
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL(
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        PpapiHostMsg_TrueTypeFontSingleton_GetFontsInFamily,
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        OnHostMsgGetFontsInFamily)
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IPC_END_MESSAGE_MAP()
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return PP_ERROR_FAILED;
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
76b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)int32_t FontMessageFilter::OnHostMsgGetFontFamilies(
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ppapi::host::HostMessageContext* context) {
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // OK to use "slow blocking" version since we're on the blocking pool.
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::vector<std::string> font_families;
81b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  GetFontFamilies_SlowBlocking(&font_families);
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Sort the names in case the host platform returns them out of order.
834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::sort(font_families.begin(), font_families.end());
844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int32_t result = base::checked_numeric_cast<int32_t>(font_families.size());
864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ppapi::host::ReplyMessageContext reply_context =
874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      context->MakeReplyMessageContext();
884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  reply_context.params.set_result(result);
894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  context->reply_msg =
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      PpapiPluginMsg_TrueTypeFontSingleton_GetFontFamiliesReply(font_families);
91b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return result;
92b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
93b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
94b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)int32_t FontMessageFilter::OnHostMsgGetFontsInFamily(
95b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    ppapi::host::HostMessageContext* context,
96b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    const std::string& family) {
97b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // OK to use "slow blocking" version since we're on the blocking pool.
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::vector<ppapi::proxy::SerializedTrueTypeFontDesc> fonts_in_family;
99b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  GetFontsInFamily_SlowBlocking(family, &fonts_in_family);
100
101  int32_t result = base::checked_numeric_cast<int32_t>(fonts_in_family.size());
102  ppapi::host::ReplyMessageContext reply_context =
103      context->MakeReplyMessageContext();
104  reply_context.params.set_result(result);
105  context->reply_msg =
106      PpapiPluginMsg_TrueTypeFontSingleton_GetFontsInFamilyReply(
107          fonts_in_family);
108  return result;
109}
110
111}  // namespace
112
113PepperTrueTypeFontListHost::PepperTrueTypeFontListHost(
114    BrowserPpapiHost* host,
115    PP_Instance instance,
116    PP_Resource resource)
117    : ResourceHost(host->GetPpapiHost(), instance, resource) {
118  AddFilter(scoped_refptr<ppapi::host::ResourceMessageFilter>(
119      new FontMessageFilter()));
120}
121
122PepperTrueTypeFontListHost::~PepperTrueTypeFontListHost() {
123}
124
125}  // namespace content
126