1// Copyright (c) 2012 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_browser_font_singleton_host.h"
6
7#include "base/threading/sequenced_worker_pool.h"
8#include "base/values.h"
9#include "content/common/font_list.h"
10#include "content/public/browser/browser_ppapi_host.h"
11#include "content/public/browser/browser_thread.h"
12#include "ppapi/host/dispatch_host_message.h"
13#include "ppapi/host/resource_message_filter.h"
14#include "ppapi/proxy/ppapi_messages.h"
15
16namespace content {
17
18namespace {
19
20// Handles the font list request on the blocking pool.
21class FontMessageFilter : public ppapi::host::ResourceMessageFilter {
22 public:
23  FontMessageFilter();
24
25  // ppapi::host::ResourceMessageFilter implementation.
26  virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage(
27      const IPC::Message& msg) OVERRIDE;
28  virtual int32_t OnResourceMessageReceived(
29      const IPC::Message& msg,
30      ppapi::host::HostMessageContext* context) OVERRIDE;
31
32 private:
33  virtual ~FontMessageFilter();
34
35  // Message handler.
36  int32_t OnHostMsgGetFontFamilies(ppapi::host::HostMessageContext* context);
37
38  DISALLOW_COPY_AND_ASSIGN(FontMessageFilter);
39};
40
41FontMessageFilter::FontMessageFilter() {}
42
43FontMessageFilter::~FontMessageFilter() {}
44
45scoped_refptr<base::TaskRunner> FontMessageFilter::OverrideTaskRunnerForMessage(
46    const IPC::Message& msg) {
47  // Use the blocking pool to get the font list (currently the only message)
48  // Since getting the font list is non-threadsafe on Linux (for versions of
49  // Pango predating 2013), use a sequenced task runner.
50  base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
51  return pool->GetSequencedTaskRunner(
52      pool->GetNamedSequenceToken(kFontListSequenceToken));
53}
54
55int32_t FontMessageFilter::OnResourceMessageReceived(
56    const IPC::Message& msg,
57    ppapi::host::HostMessageContext* context) {
58  PPAPI_BEGIN_MESSAGE_MAP(FontMessageFilter, msg)
59    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
60        PpapiHostMsg_BrowserFontSingleton_GetFontFamilies,
61        OnHostMsgGetFontFamilies)
62  PPAPI_END_MESSAGE_MAP()
63  return PP_ERROR_FAILED;
64}
65
66int32_t FontMessageFilter::OnHostMsgGetFontFamilies(
67    ppapi::host::HostMessageContext* context) {
68  // OK to use "slow blocking" version since we're on the blocking pool.
69  scoped_ptr<base::ListValue> list(GetFontList_SlowBlocking());
70
71  std::string output;
72  for (size_t i = 0; i < list->GetSize(); i++) {
73    base::ListValue* cur_font;
74    if (!list->GetList(i, &cur_font))
75      continue;
76
77    // Each entry is actually a list of (font name, localized name).
78    // We only care about the regular name.
79    std::string font_name;
80    if (!cur_font->GetString(0, &font_name))
81      continue;
82
83    // Font names are separated with nulls. We also want an explicit null at
84    // the end of the string (Pepper strings aren't null terminated so since
85    // we specify there will be a null, it should actually be in the string).
86    output.append(font_name);
87    output.push_back(0);
88  }
89
90  context->reply_msg =
91      PpapiPluginMsg_BrowserFontSingleton_GetFontFamiliesReply(output);
92  return PP_OK;
93}
94
95}  // namespace
96
97PepperBrowserFontSingletonHost::PepperBrowserFontSingletonHost(
98    BrowserPpapiHost* host,
99    PP_Instance instance,
100    PP_Resource resource)
101    : ResourceHost(host->GetPpapiHost(), instance, resource) {
102  AddFilter(scoped_refptr<ppapi::host::ResourceMessageFilter>(
103      new FontMessageFilter()));
104}
105
106PepperBrowserFontSingletonHost::~PepperBrowserFontSingletonHost() {}
107
108}  // namespace content
109