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 "components/content_settings/core/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(PP_Instance instance,
28                                                     BrowserPpapiHost* host)
29    : document_url_(host->GetDocumentURLForInstance(instance)) {
30  int unused;
31  host->GetRenderFrameIDsForInstance(instance, &render_process_id_, &unused);
32}
33
34PepperBrokerMessageFilter::~PepperBrokerMessageFilter() {}
35
36scoped_refptr<base::TaskRunner>
37PepperBrokerMessageFilter::OverrideTaskRunnerForMessage(
38    const IPC::Message& message) {
39  return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
40}
41
42int32_t PepperBrokerMessageFilter::OnResourceMessageReceived(
43    const IPC::Message& msg,
44    ppapi::host::HostMessageContext* context) {
45  PPAPI_BEGIN_MESSAGE_MAP(PepperBrokerMessageFilter, msg)
46    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_Broker_IsAllowed,
47                                        OnIsAllowed)
48  PPAPI_END_MESSAGE_MAP()
49  return PP_ERROR_FAILED;
50}
51
52int32_t PepperBrokerMessageFilter::OnIsAllowed(
53    ppapi::host::HostMessageContext* context) {
54  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
55  if (!document_url_.is_valid())
56    return PP_ERROR_FAILED;
57  RenderProcessHost* render_process_host =
58      RenderProcessHost::FromID(render_process_id_);
59  if (!render_process_host)
60    return PP_ERROR_FAILED;
61  Profile* profile =
62      Profile::FromBrowserContext(render_process_host->GetBrowserContext());
63  HostContentSettingsMap* content_settings =
64      profile->GetHostContentSettingsMap();
65  ContentSetting setting =
66      content_settings->GetContentSetting(document_url_,
67                                          document_url_,
68                                          CONTENT_SETTINGS_TYPE_PPAPI_BROKER,
69                                          std::string());
70  if (setting == CONTENT_SETTING_ALLOW)
71    return PP_OK;
72  return PP_ERROR_FAILED;
73}
74
75}  // namespace chrome
76