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 16 namespace content { 17 18 namespace { 19 20 // Handles the font list request on the blocking pool. 21 class 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 41 FontMessageFilter::FontMessageFilter() { 42 } 43 44 FontMessageFilter::~FontMessageFilter() { 45 } 46 47 scoped_refptr<base::TaskRunner> FontMessageFilter::OverrideTaskRunnerForMessage( 48 const IPC::Message& msg) { 49 // Use the blocking pool to get the font list (currently the only message) 50 // Since getting the font list is non-threadsafe on Linux (for versions of 51 // Pango predating 2013), use a sequenced task runner. 52 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); 53 return pool->GetSequencedTaskRunner( 54 pool->GetNamedSequenceToken(kFontListSequenceToken)); 55 } 56 57 int32_t FontMessageFilter::OnResourceMessageReceived( 58 const IPC::Message& msg, 59 ppapi::host::HostMessageContext* context) { 60 IPC_BEGIN_MESSAGE_MAP(FontMessageFilter, msg) 61 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( 62 PpapiHostMsg_BrowserFontSingleton_GetFontFamilies, 63 OnHostMsgGetFontFamilies) 64 IPC_END_MESSAGE_MAP() 65 return PP_ERROR_FAILED; 66 } 67 68 int32_t FontMessageFilter::OnHostMsgGetFontFamilies( 69 ppapi::host::HostMessageContext* context) { 70 // OK to use "slow blocking" version since we're on the blocking pool. 71 scoped_ptr<base::ListValue> list(GetFontList_SlowBlocking()); 72 73 std::string output; 74 for (size_t i = 0; i < list->GetSize(); i++) { 75 base::ListValue* cur_font; 76 if (!list->GetList(i, &cur_font)) 77 continue; 78 79 // Each entry is actually a list of (font name, localized name). 80 // We only care about the regular name. 81 std::string font_name; 82 if (!cur_font->GetString(0, &font_name)) 83 continue; 84 85 // Font names are separated with nulls. We also want an explicit null at 86 // the end of the string (Pepper strings aren't null terminated so since 87 // we specify there will be a null, it should actually be in the string). 88 output.append(font_name); 89 output.push_back(0); 90 } 91 92 context->reply_msg = 93 PpapiPluginMsg_BrowserFontSingleton_GetFontFamiliesReply(output); 94 return PP_OK; 95 } 96 97 } // namespace 98 99 PepperBrowserFontSingletonHost::PepperBrowserFontSingletonHost( 100 BrowserPpapiHost* host, 101 PP_Instance instance, 102 PP_Resource resource) 103 : ResourceHost(host->GetPpapiHost(), instance, resource) { 104 AddFilter(scoped_refptr<ppapi::host::ResourceMessageFilter>( 105 new FontMessageFilter())); 106 } 107 108 PepperBrowserFontSingletonHost::~PepperBrowserFontSingletonHost() { 109 } 110 111 } // namespace content 112