File: content_browser_pepper_host_factory.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (261 lines) | stat: -rw-r--r-- 10,446 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/pepper/content_browser_pepper_host_factory.h"

#include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h"
#include "content/browser/renderer_host/pepper/pepper_browser_font_singleton_host.h"
#include "content/browser/renderer_host/pepper/pepper_file_io_host.h"
#include "content/browser/renderer_host/pepper/pepper_file_ref_host.h"
#include "content/browser/renderer_host/pepper/pepper_file_system_browser_host.h"
#include "content/browser/renderer_host/pepper/pepper_flash_file_message_filter.h"
#include "content/browser/renderer_host/pepper/pepper_gamepad_host.h"
#include "content/browser/renderer_host/pepper/pepper_host_resolver_message_filter.h"
#include "content/browser/renderer_host/pepper/pepper_network_monitor_host.h"
#include "content/browser/renderer_host/pepper/pepper_network_proxy_host.h"
#include "content/browser/renderer_host/pepper/pepper_print_settings_manager.h"
#include "content/browser/renderer_host/pepper/pepper_printing_host.h"
#include "content/browser/renderer_host/pepper/pepper_tcp_server_socket_message_filter.h"
#include "content/browser/renderer_host/pepper/pepper_tcp_socket_message_filter.h"
#include "content/browser/renderer_host/pepper/pepper_truetype_font_host.h"
#include "content/browser/renderer_host/pepper/pepper_truetype_font_list_host.h"
#include "content/browser/renderer_host/pepper/pepper_udp_socket_message_filter.h"
#include "ppapi/host/message_filter_host.h"
#include "ppapi/host/ppapi_host.h"
#include "ppapi/host/resource_host.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/shared_impl/ppapi_permissions.h"

using ppapi::host::MessageFilterHost;
using ppapi::host::ResourceHost;
using ppapi::host::ResourceMessageFilter;
using ppapi::proxy::SerializedTrueTypeFontDesc;
using ppapi::UnpackMessage;

namespace content {

namespace {

const size_t kMaxSocketsAllowed = 1024;

bool CanCreateSocket() {
  return PepperTCPServerSocketMessageFilter::GetNumInstances() +
             PepperTCPSocketMessageFilter::GetNumInstances() +
             PepperUDPSocketMessageFilter::GetNumInstances() <
         kMaxSocketsAllowed;
}

}  // namespace

ContentBrowserPepperHostFactory::ContentBrowserPepperHostFactory(
    BrowserPpapiHostImpl* host)
    : host_(host) {}

ContentBrowserPepperHostFactory::~ContentBrowserPepperHostFactory() {}

scoped_ptr<ResourceHost> ContentBrowserPepperHostFactory::CreateResourceHost(
    ppapi::host::PpapiHost* host,
    PP_Resource resource,
    PP_Instance instance,
    const IPC::Message& message) {
  DCHECK(host == host_->GetPpapiHost());

  // Make sure the plugin is giving us a valid instance for this resource.
  if (!host_->IsValidInstance(instance))
    return scoped_ptr<ResourceHost>();

  // Public interfaces.
  switch (message.type()) {
    case PpapiHostMsg_FileIO_Create::ID: {
      return scoped_ptr<ResourceHost>(
          new PepperFileIOHost(host_, instance, resource));
    }
    case PpapiHostMsg_FileSystem_Create::ID: {
      PP_FileSystemType file_system_type;
      if (!ppapi::UnpackMessage<PpapiHostMsg_FileSystem_Create>(
              message, &file_system_type)) {
        NOTREACHED();
        return scoped_ptr<ResourceHost>();
      }
      return scoped_ptr<ResourceHost>(new PepperFileSystemBrowserHost(
          host_, instance, resource, file_system_type));
    }
    case PpapiHostMsg_Gamepad_Create::ID: {
      return scoped_ptr<ResourceHost>(
          new PepperGamepadHost(host_, instance, resource));
    }
    case PpapiHostMsg_NetworkProxy_Create::ID: {
      return scoped_ptr<ResourceHost>(
          new PepperNetworkProxyHost(host_, instance, resource));
    }
    case PpapiHostMsg_HostResolver_Create::ID: {
      scoped_refptr<ResourceMessageFilter> host_resolver(
          new PepperHostResolverMessageFilter(host_, instance, false));
      return scoped_ptr<ResourceHost>(new MessageFilterHost(
          host_->GetPpapiHost(), instance, resource, host_resolver));
    }
    case PpapiHostMsg_FileRef_CreateForFileAPI::ID: {
      PP_Resource file_system;
      std::string internal_path;
      if (!UnpackMessage<PpapiHostMsg_FileRef_CreateForFileAPI>(
              message, &file_system, &internal_path)) {
        NOTREACHED();
        return scoped_ptr<ResourceHost>();
      }
      return scoped_ptr<ResourceHost>(new PepperFileRefHost(
          host_, instance, resource, file_system, internal_path));
    }
    case PpapiHostMsg_TCPSocket_Create::ID: {
      ppapi::TCPSocketVersion version;
      if (!UnpackMessage<PpapiHostMsg_TCPSocket_Create>(message, &version) ||
          version == ppapi::TCP_SOCKET_VERSION_PRIVATE) {
        return scoped_ptr<ResourceHost>();
      }

      return CreateNewTCPSocket(instance, resource, version);
    }
    case PpapiHostMsg_UDPSocket_Create::ID: {
      if (CanCreateSocket()) {
        scoped_refptr<ResourceMessageFilter> udp_socket(
            new PepperUDPSocketMessageFilter(host_, instance, false));
        return scoped_ptr<ResourceHost>(new MessageFilterHost(
            host_->GetPpapiHost(), instance, resource, udp_socket));
      } else {
        return scoped_ptr<ResourceHost>();
      }
    }
  }

  // Dev interfaces.
  if (GetPermissions().HasPermission(ppapi::PERMISSION_DEV)) {
    switch (message.type()) {
      case PpapiHostMsg_Printing_Create::ID: {
        scoped_ptr<PepperPrintSettingsManager> manager(
            new PepperPrintSettingsManagerImpl());
        return scoped_ptr<ResourceHost>(new PepperPrintingHost(
            host_->GetPpapiHost(), instance, resource, manager.Pass()));
      }
      case PpapiHostMsg_TrueTypeFont_Create::ID: {
        SerializedTrueTypeFontDesc desc;
        if (!UnpackMessage<PpapiHostMsg_TrueTypeFont_Create>(message, &desc)) {
          NOTREACHED();
          return scoped_ptr<ResourceHost>();
        }
        // Check that the family name is valid UTF-8 before passing it to the
        // host OS.
        if (!base::IsStringUTF8(desc.family))
          return scoped_ptr<ResourceHost>();

        return scoped_ptr<ResourceHost>(
            new PepperTrueTypeFontHost(host_, instance, resource, desc));
      }
      case PpapiHostMsg_TrueTypeFontSingleton_Create::ID: {
        return scoped_ptr<ResourceHost>(
            new PepperTrueTypeFontListHost(host_, instance, resource));
      }
    }
  }

  // Private interfaces.
  if (GetPermissions().HasPermission(ppapi::PERMISSION_PRIVATE)) {
    switch (message.type()) {
      case PpapiHostMsg_BrowserFontSingleton_Create::ID:
        return scoped_ptr<ResourceHost>(
            new PepperBrowserFontSingletonHost(host_, instance, resource));
    }
  }

  // Permissions for the following interfaces will be checked at the
  // time of the corresponding instance's methods calls (because
  // permission check can be performed only on the UI
  // thread). Currently these interfaces are available only for
  // whitelisted apps which may not have access to the other private
  // interfaces.
  if (message.type() == PpapiHostMsg_HostResolver_CreatePrivate::ID) {
    scoped_refptr<ResourceMessageFilter> host_resolver(
        new PepperHostResolverMessageFilter(host_, instance, true));
    return scoped_ptr<ResourceHost>(new MessageFilterHost(
        host_->GetPpapiHost(), instance, resource, host_resolver));
  }
  if (message.type() == PpapiHostMsg_TCPServerSocket_CreatePrivate::ID) {
    if (CanCreateSocket()) {
      scoped_refptr<ResourceMessageFilter> tcp_server_socket(
          new PepperTCPServerSocketMessageFilter(this, host_, instance, true));
      return scoped_ptr<ResourceHost>(new MessageFilterHost(
          host_->GetPpapiHost(), instance, resource, tcp_server_socket));
    } else {
      return scoped_ptr<ResourceHost>();
    }
  }
  if (message.type() == PpapiHostMsg_TCPSocket_CreatePrivate::ID) {
    return CreateNewTCPSocket(instance, resource,
                              ppapi::TCP_SOCKET_VERSION_PRIVATE);
  }
  if (message.type() == PpapiHostMsg_UDPSocket_CreatePrivate::ID) {
    if (CanCreateSocket()) {
      scoped_refptr<ResourceMessageFilter> udp_socket(
          new PepperUDPSocketMessageFilter(host_, instance, true));
      return scoped_ptr<ResourceHost>(new MessageFilterHost(
          host_->GetPpapiHost(), instance, resource, udp_socket));
    } else {
      return scoped_ptr<ResourceHost>();
    }
  }
  if (message.type() == PpapiHostMsg_NetworkMonitor_Create::ID) {
    return scoped_ptr<ResourceHost>(
        new PepperNetworkMonitorHost(host_, instance, resource));
  }

  // Flash interfaces.
  if (GetPermissions().HasPermission(ppapi::PERMISSION_FLASH)) {
    switch (message.type()) {
      case PpapiHostMsg_FlashFile_Create::ID: {
        scoped_refptr<ResourceMessageFilter> file_filter(
            new PepperFlashFileMessageFilter(instance, host_));
        return scoped_ptr<ResourceHost>(new MessageFilterHost(
            host_->GetPpapiHost(), instance, resource, file_filter));
      }
    }
  }

  return scoped_ptr<ResourceHost>();
}

scoped_ptr<ppapi::host::ResourceHost>
ContentBrowserPepperHostFactory::CreateAcceptedTCPSocket(
    PP_Instance instance,
    ppapi::TCPSocketVersion version,
    scoped_ptr<net::TCPSocket> socket) {
  if (!CanCreateSocket())
    return scoped_ptr<ResourceHost>();
  scoped_refptr<ResourceMessageFilter> tcp_socket(
      new PepperTCPSocketMessageFilter(
          host_, instance, version, socket.Pass()));
  return scoped_ptr<ResourceHost>(
      new MessageFilterHost(host_->GetPpapiHost(), instance, 0, tcp_socket));
}

scoped_ptr<ppapi::host::ResourceHost>
ContentBrowserPepperHostFactory::CreateNewTCPSocket(
    PP_Instance instance,
    PP_Resource resource,
    ppapi::TCPSocketVersion version) {
  if (!CanCreateSocket())
    return scoped_ptr<ResourceHost>();

  scoped_refptr<ResourceMessageFilter> tcp_socket(
      new PepperTCPSocketMessageFilter(this, host_, instance, version));
  if (!tcp_socket.get())
    return scoped_ptr<ResourceHost>();

  return scoped_ptr<ResourceHost>(new MessageFilterHost(
      host_->GetPpapiHost(), instance, resource, tcp_socket));
}

const ppapi::PpapiPermissions& ContentBrowserPepperHostFactory::GetPermissions()
    const {
  return host_->GetPpapiHost()->permissions();
}

}  // namespace content