12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/browser/renderer_host/pepper/pepper_truetype_font_list_host.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <algorithm>
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/numerics/safe_conversions.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/threading/sequenced_worker_pool.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/browser/renderer_host/pepper/pepper_truetype_font_list.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/common/font_list.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_ppapi_host.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/host/dispatch_host_message.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/host/host_message_context.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/host/resource_message_filter.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace content {
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Handles the font list request on the blocking pool.
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FontMessageFilter : public ppapi::host::ResourceMessageFilter {
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FontMessageFilter();
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ppapi::host::ResourceMessageFilter implementation.
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage(
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const IPC::Message& msg) OVERRIDE;
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual int32_t OnResourceMessageReceived(
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const IPC::Message& msg,
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ppapi::host::HostMessageContext* context) OVERRIDE;
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~FontMessageFilter();
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Message handlers.
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int32_t OnHostMsgGetFontFamilies(ppapi::host::HostMessageContext* context);
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  int32_t OnHostMsgGetFontsInFamily(ppapi::host::HostMessageContext* context,
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                    const std::string& family);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FontMessageFilter);
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
47a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochFontMessageFilter::FontMessageFilter() {}
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
49a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochFontMessageFilter::~FontMessageFilter() {}
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)scoped_refptr<base::TaskRunner> FontMessageFilter::OverrideTaskRunnerForMessage(
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const IPC::Message& msg) {
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Use the blocking pool to get the font list (currently the only message)
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Since getting the font list is non-threadsafe on Linux (for versions of
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Pango predating 2013), use a sequenced task runner.
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return pool->GetSequencedTaskRunner(
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      pool->GetNamedSequenceToken(kFontListSequenceToken));
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int32_t FontMessageFilter::OnResourceMessageReceived(
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const IPC::Message& msg,
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ppapi::host::HostMessageContext* context) {
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPAPI_BEGIN_MESSAGE_MAP(FontMessageFilter, msg)
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        PpapiHostMsg_TrueTypeFontSingleton_GetFontFamilies,
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        OnHostMsgGetFontFamilies)
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL(
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        PpapiHostMsg_TrueTypeFontSingleton_GetFontsInFamily,
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        OnHostMsgGetFontsInFamily)
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPAPI_END_MESSAGE_MAP()
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PP_ERROR_FAILED;
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int32_t FontMessageFilter::OnHostMsgGetFontFamilies(
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ppapi::host::HostMessageContext* context) {
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // OK to use "slow blocking" version since we're on the blocking pool.
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<std::string> font_families;
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetFontFamilies_SlowBlocking(&font_families);
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sort the names in case the host platform returns them out of order.
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::sort(font_families.begin(), font_families.end());
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  context->reply_msg =
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PpapiPluginMsg_TrueTypeFontSingleton_GetFontFamiliesReply(font_families);
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return base::checked_cast<int32_t>(font_families.size());
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)int32_t FontMessageFilter::OnHostMsgGetFontsInFamily(
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ppapi::host::HostMessageContext* context,
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& family) {
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // OK to use "slow blocking" version since we're on the blocking pool.
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::vector<ppapi::proxy::SerializedTrueTypeFontDesc> fonts_in_family;
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GetFontsInFamily_SlowBlocking(family, &fonts_in_family);
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  context->reply_msg =
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      PpapiPluginMsg_TrueTypeFontSingleton_GetFontsInFamilyReply(
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          fonts_in_family);
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return base::checked_cast<int32_t>(fonts_in_family.size());
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
103a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochPepperTrueTypeFontListHost::PepperTrueTypeFontListHost(BrowserPpapiHost* host,
104a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                                       PP_Instance instance,
105a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                                       PP_Resource resource)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : ResourceHost(host->GetPpapiHost(), instance, resource) {
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AddFilter(scoped_refptr<ppapi::host::ResourceMessageFilter>(
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new FontMessageFilter()));
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
111a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochPepperTrueTypeFontListHost::~PepperTrueTypeFontListHost() {}
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace content
114