pepper_broker_message_filter.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu// Use of this source code is governed by a BSD-style license that can be
3f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu// found in the LICENSE file.
4f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
5f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "chrome/browser/renderer_host/pepper/pepper_broker_message_filter.h"
6f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
7f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include <string>
8f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
9f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "chrome/browser/content_settings/host_content_settings_map.h"
10f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "chrome/browser/profiles/profile.h"
11c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "chrome/common/content_settings.h"
12f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "content/public/browser/browser_ppapi_host.h"
13c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
14c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "content/public/browser/render_process_host.h"
15c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "ipc/ipc_message_macros.h"
16c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "ppapi/c/pp_errors.h"
17f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "ppapi/host/dispatch_host_message.h"
18c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
19c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "url/gurl.h"
20c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
21c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)using content::BrowserPpapiHost;
22c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)using content::BrowserThread;
23c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)using content::RenderProcessHost;
24c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
25f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liunamespace chrome {
26f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
27f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo LiuPepperBrokerMessageFilter::PepperBrokerMessageFilter(
28f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    PP_Instance instance,
29f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    BrowserPpapiHost* host)
30f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    : document_url_(host->GetDocumentURLForInstance(instance)) {
31f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  int unused;
32f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  host->GetRenderFrameIDsForInstance(instance, &render_process_id_, &unused);
33f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}
34f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
35f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo LiuPepperBrokerMessageFilter::~PepperBrokerMessageFilter() {
36f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}
37f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
38f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liuscoped_refptr<base::TaskRunner>
39f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo LiuPepperBrokerMessageFilter::OverrideTaskRunnerForMessage(
40f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    const IPC::Message& message) {
41f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
42f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}
43f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
44f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liuint32_t PepperBrokerMessageFilter::OnResourceMessageReceived(
45f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    const IPC::Message& msg,
46f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    ppapi::host::HostMessageContext* context) {
47f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  IPC_BEGIN_MESSAGE_MAP(PepperBrokerMessageFilter, msg)
48f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Broker_IsAllowed,
49f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu                                        OnIsAllowed)
50f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  IPC_END_MESSAGE_MAP()
51f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  return PP_ERROR_FAILED;
52f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}
53f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
54f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liuint32_t PepperBrokerMessageFilter::OnIsAllowed(
55f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    ppapi::host::HostMessageContext* context) {
56f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
57f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  if (!document_url_.is_valid())
58f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    return PP_ERROR_FAILED;
59f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  RenderProcessHost* render_process_host =
60f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu      RenderProcessHost::FromID(render_process_id_);
61f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  if (!render_process_host)
62f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    return PP_ERROR_FAILED;
63f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  Profile* profile =
64f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu      Profile::FromBrowserContext(render_process_host->GetBrowserContext());
65f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  HostContentSettingsMap* content_settings =
66f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu      profile->GetHostContentSettingsMap();
67f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  ContentSetting setting =
68f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu      content_settings->GetContentSetting(document_url_, document_url_,
69f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu                                          CONTENT_SETTINGS_TYPE_PPAPI_BROKER,
70f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu                                          std::string());
71f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  if (setting == CONTENT_SETTING_ALLOW)
72f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    return PP_OK;
73f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu  return PP_ERROR_FAILED;
74f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}
75f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
76f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu}  // namespace chrome
77f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu