12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 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_browser_font_singleton_host.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/threading/sequenced_worker_pool.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/values.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/common/font_list.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_ppapi_host.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/host/dispatch_host_message.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/host/resource_message_filter.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace content {
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Handles the font list request on the blocking pool.
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FontMessageFilter : public ppapi::host::ResourceMessageFilter {
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FontMessageFilter();
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ppapi::host::ResourceMessageFilter implementation.
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage(
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const IPC::Message& msg) OVERRIDE;
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual int32_t OnResourceMessageReceived(
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const IPC::Message& msg,
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ppapi::host::HostMessageContext* context) OVERRIDE;
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~FontMessageFilter();
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Message handler.
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int32_t OnHostMsgGetFontFamilies(ppapi::host::HostMessageContext* context);
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FontMessageFilter);
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
41a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochFontMessageFilter::FontMessageFilter() {}
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
43a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochFontMessageFilter::~FontMessageFilter() {}
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)scoped_refptr<base::TaskRunner> FontMessageFilter::OverrideTaskRunnerForMessage(
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const IPC::Message& msg) {
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Use the blocking pool to get the font list (currently the only message)
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Since getting the font list is non-threadsafe on Linux (for versions of
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Pango predating 2013), use a sequenced task runner.
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return pool->GetSequencedTaskRunner(
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      pool->GetNamedSequenceToken(kFontListSequenceToken));
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int32_t FontMessageFilter::OnResourceMessageReceived(
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const IPC::Message& msg,
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ppapi::host::HostMessageContext* context) {
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPAPI_BEGIN_MESSAGE_MAP(FontMessageFilter, msg)
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        PpapiHostMsg_BrowserFontSingleton_GetFontFamilies,
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        OnHostMsgGetFontFamilies)
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPAPI_END_MESSAGE_MAP()
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PP_ERROR_FAILED;
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int32_t FontMessageFilter::OnHostMsgGetFontFamilies(
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ppapi::host::HostMessageContext* context) {
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // OK to use "slow blocking" version since we're on the blocking pool.
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<base::ListValue> list(GetFontList_SlowBlocking());
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string output;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < list->GetSize(); i++) {
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::ListValue* cur_font;
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!list->GetList(i, &cur_font))
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Each entry is actually a list of (font name, localized name).
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // We only care about the regular name.
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::string font_name;
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!cur_font->GetString(0, &font_name))
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Font names are separated with nulls. We also want an explicit null at
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // the end of the string (Pepper strings aren't null terminated so since
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // we specify there will be a null, it should actually be in the string).
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    output.append(font_name);
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    output.push_back(0);
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  context->reply_msg =
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PpapiPluginMsg_BrowserFontSingleton_GetFontFamiliesReply(output);
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return PP_OK;
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PepperBrowserFontSingletonHost::PepperBrowserFontSingletonHost(
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    BrowserPpapiHost* host,
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PP_Instance instance,
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PP_Resource resource)
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : ResourceHost(host->GetPpapiHost(), instance, resource) {
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AddFilter(scoped_refptr<ppapi::host::ResourceMessageFilter>(
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new FontMessageFilter()));
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
106a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochPepperBrowserFontSingletonHost::~PepperBrowserFontSingletonHost() {}
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace content
109