pepper_broker_message_filter.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 "chrome/browser/renderer_host/pepper/pepper_broker_message_filter.h"
6
7#include <string>
8
9#include "chrome/browser/content_settings/host_content_settings_map.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/common/content_settings.h"
12#include "content/public/browser/browser_ppapi_host.h"
13#include "content/public/browser/browser_thread.h"
14#include "content/public/browser/render_process_host.h"
15#include "ipc/ipc_message_macros.h"
16#include "ppapi/c/pp_errors.h"
17#include "ppapi/host/dispatch_host_message.h"
18#include "ppapi/proxy/ppapi_messages.h"
19#include "url/gurl.h"
20
21using content::BrowserPpapiHost;
22using content::BrowserThread;
23using content::RenderProcessHost;
24
25namespace chrome {
26
27PepperBrokerMessageFilter::PepperBrokerMessageFilter(
28    PP_Instance instance,
29    BrowserPpapiHost* host)
30    : document_url_(host->GetDocumentURLForInstance(instance)) {
31  int unused;
32  host->GetRenderViewIDsForInstance(instance, &render_process_id_, &unused);
33}
34
35PepperBrokerMessageFilter::~PepperBrokerMessageFilter() {
36}
37
38scoped_refptr<base::TaskRunner>
39PepperBrokerMessageFilter::OverrideTaskRunnerForMessage(
40    const IPC::Message& message) {
41  return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
42}
43
44int32_t PepperBrokerMessageFilter::OnResourceMessageReceived(
45    const IPC::Message& msg,
46    ppapi::host::HostMessageContext* context) {
47  IPC_BEGIN_MESSAGE_MAP(PepperBrokerMessageFilter, msg)
48    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Broker_IsAllowed,
49                                        OnIsAllowed)
50  IPC_END_MESSAGE_MAP()
51  return PP_ERROR_FAILED;
52}
53
54int32_t PepperBrokerMessageFilter::OnIsAllowed(
55    ppapi::host::HostMessageContext* context) {
56  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
57  if (!document_url_.is_valid())
58    return PP_ERROR_FAILED;
59  RenderProcessHost* render_process_host =
60      RenderProcessHost::FromID(render_process_id_);
61  if (!render_process_host)
62    return PP_ERROR_FAILED;
63  Profile* profile =
64      Profile::FromBrowserContext(render_process_host->GetBrowserContext());
65  HostContentSettingsMap* content_settings =
66      profile->GetHostContentSettingsMap();
67  ContentSetting setting =
68      content_settings->GetContentSetting(document_url_, document_url_,
69                                          CONTENT_SETTINGS_TYPE_PPAPI_BROKER,
70                                          std::string());
71  if (setting == CONTENT_SETTING_ALLOW)
72    return PP_OK;
73  return PP_ERROR_FAILED;
74}
75
76}  // namespace chrome
77