13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
23551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
33551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// found in the LICENSE file.
43551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
53551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/renderer/pepper/resource_converter.h"
63551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
73551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/bind.h"
83551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/message_loop/message_loop.h"
9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "content/public/renderer/renderer_ppapi_host.h"
100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "content/renderer/pepper/pepper_file_system_host.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/renderer/pepper/pepper_media_stream_audio_track_host.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/renderer/pepper/pepper_media_stream_video_track_host.h"
13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ipc/ipc_message.h"
140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "ppapi/host/ppapi_host.h"
150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "ppapi/host/resource_host.h"
160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ppapi/shared_impl/resource_var.h"
18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ppapi/shared_impl/scoped_pp_var.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/common/fileapi/file_system_util.h"
200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebFileSystem.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
22010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "third_party/WebKit/public/web/WebDOMFileSystem.h"
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "third_party/WebKit/public/web/WebDOMMediaStreamTrack.h"
25a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "third_party/WebKit/public/web/WebLocalFrame.h"
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using ppapi::ResourceVar;
283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace content {
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace {
313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void FlushComplete(
33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const base::Callback<void(bool)>& callback,
344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const std::vector<scoped_refptr<content::HostResourceVar> >& browser_vars,
35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const std::vector<int>& pending_host_ids) {
364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  CHECK(browser_vars.size() == pending_host_ids.size());
374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  for (size_t i = 0; i < browser_vars.size(); ++i) {
384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    browser_vars[i]->set_pending_browser_host_id(pending_host_ids[i]);
39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  callback.Run(true);
413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Converts a blink::WebFileSystem::Type to a PP_FileSystemType.
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)PP_FileSystemType WebFileSystemTypeToPPAPI(blink::WebFileSystem::Type type) {
450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  switch (type) {
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case blink::WebFileSystem::TypeTemporary:
470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      return PP_FILESYSTEMTYPE_LOCALTEMPORARY;
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case blink::WebFileSystem::TypePersistent:
490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      return PP_FILESYSTEMTYPE_LOCALPERSISTENT;
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case blink::WebFileSystem::TypeIsolated:
510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      return PP_FILESYSTEMTYPE_ISOLATED;
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    case blink::WebFileSystem::TypeExternal:
530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      return PP_FILESYSTEMTYPE_EXTERNAL;
540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    default:
550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      NOTREACHED();
560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      return PP_FILESYSTEMTYPE_LOCALTEMPORARY;
570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
6003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// Converts a storage::FileSystemType to a blink::WebFileSystemType.
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Returns true on success, false if |type| does not correspond to a
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// WebFileSystemType.
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool FileApiFileSystemTypeToWebFileSystemType(
6403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::FileSystemType type,
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    blink::WebFileSystemType* result_type) {
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  switch (type) {
6703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case storage::kFileSystemTypeTemporary:
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      *result_type = blink::WebFileSystemTypeTemporary;
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return true;
7003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case storage::kFileSystemTypePersistent:
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      *result_type = blink::WebFileSystemTypePersistent;
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return true;
7303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case storage::kFileSystemTypeIsolated:
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      *result_type = blink::WebFileSystemTypeIsolated;
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return true;
7603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    case storage::kFileSystemTypeExternal:
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      *result_type = blink::WebFileSystemTypeExternal;
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return true;
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    default:
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return false;
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Given a V8 value containing a DOMFileSystem, creates a resource host and
850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// returns the resource information for serialization.
860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// On error, false.
870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)bool DOMFileSystemToResource(
880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    PP_Instance instance,
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RendererPpapiHost* host,
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const blink::WebDOMFileSystem& dom_file_system,
910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    int* pending_renderer_id,
920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_ptr<IPC::Message>* create_message,
930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_ptr<IPC::Message>* browser_host_create_message) {
940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DCHECK(!dom_file_system.isNull());
950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  PP_FileSystemType file_system_type =
970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      WebFileSystemTypeToPPAPI(dom_file_system.type());
980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  GURL root_url = dom_file_system.rootURL();
990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
100a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Raw external file system access is not allowed, but external file system
101a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // access through fileapi is allowed. (Without this check, there would be a
102a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // CHECK failure in FileRefResource.)
103a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if ((file_system_type == PP_FILESYSTEMTYPE_EXTERNAL) &&
104a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      (!root_url.is_valid())) {
1050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return false;
106a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
1070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  *pending_renderer_id = host->GetPpapiHost()->AddPendingResourceHost(
109a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      scoped_ptr<ppapi::host::ResourceHost>(new PepperFileSystemHost(
110a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch          host, instance, 0, root_url, file_system_type)));
1110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (*pending_renderer_id == 0)
1120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return false;
1130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  create_message->reset(
1150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      new PpapiPluginMsg_FileSystem_CreateFromPendingHost(file_system_type));
1160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  browser_host_create_message->reset(
1180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      new PpapiHostMsg_FileSystem_CreateFromRenderer(root_url.spec(),
1190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                                     file_system_type));
1200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return true;
1210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
123a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool ResourceHostToDOMFileSystem(
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::PepperFileSystemHost* file_system_host,
125a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    v8::Handle<v8::Context> context,
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    v8::Handle<v8::Value>* dom_file_system) {
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL root_url = file_system_host->GetRootUrl();
128a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL origin;
12903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  storage::FileSystemType type;
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::FilePath virtual_path;
13103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  storage::ParseFileSystemSchemeURL(root_url, &origin, &type, &virtual_path);
132a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
13303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  std::string name = storage::GetFileSystemName(origin, type);
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  blink::WebFileSystemType blink_type;
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!FileApiFileSystemTypeToWebFileSystemType(type, &blink_type))
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
137c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  blink::WebLocalFrame* frame = blink::WebLocalFrame::frameForContext(context);
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  blink::WebDOMFileSystem web_dom_file_system = blink::WebDOMFileSystem::create(
139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      frame,
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      blink_type,
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      blink::WebString::fromUTF8(name),
142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      root_url,
143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      blink::WebDOMFileSystem::SerializableTypeSerializable);
14446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  *dom_file_system =
14546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      web_dom_file_system.toV8Value(context->Global(), context->GetIsolate());
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return true;
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
149010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ResourceHostToDOMMediaStreamVideoTrack(
150010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    content::PepperMediaStreamVideoTrackHost* host,
151010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    v8::Handle<v8::Context> context,
152010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    v8::Handle<v8::Value>* dom_video_track) {
153010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // TODO(ronghuawu): Implement this once crbug/352219 is resolved.
154010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // blink::WebMediaStreamTrack track = host->track();
155010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // *dom_video_track = track.toV8Value();
156010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return false;
157010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
158010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool DOMMediaStreamTrackToResource(
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PP_Instance instance,
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    RendererPpapiHost* host,
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const blink::WebDOMMediaStreamTrack& dom_media_stream_track,
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int* pending_renderer_id,
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<IPC::Message>* create_message) {
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(!dom_media_stream_track.isNull());
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  *pending_renderer_id = 0;
16723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#if defined(ENABLE_WEBRTC)
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const blink::WebMediaStreamTrack track = dom_media_stream_track.component();
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::string id = track.source().id().utf8();
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (track.source().type() == blink::WebMediaStreamSource::TypeVideo) {
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    *pending_renderer_id = host->GetPpapiHost()->AddPendingResourceHost(
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        scoped_ptr<ppapi::host::ResourceHost>(
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            new PepperMediaStreamVideoTrackHost(host, instance, 0, track)));
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (*pending_renderer_id == 0)
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return false;
177424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    create_message->reset(
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        new PpapiPluginMsg_MediaStreamVideoTrack_CreateFromPendingHost(id));
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else if (track.source().type() == blink::WebMediaStreamSource::TypeAudio) {
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    *pending_renderer_id = host->GetPpapiHost()->AddPendingResourceHost(
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        scoped_ptr<ppapi::host::ResourceHost>(
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            new PepperMediaStreamAudioTrackHost(host, instance, 0, track)));
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (*pending_renderer_id == 0)
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return false;
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    create_message->reset(
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        new PpapiPluginMsg_MediaStreamAudioTrack_CreateFromPendingHost(id));
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
19223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#endif
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
197424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
198424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)ResourceConverter::~ResourceConverter() {}
199424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
200424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)ResourceConverterImpl::ResourceConverterImpl(PP_Instance instance,
201424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                             RendererPpapiHost* host)
202a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    : instance_(instance), host_(host) {}
2033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)ResourceConverterImpl::~ResourceConverterImpl() {
205424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Verify Flush() was called.
206424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(browser_host_create_messages_.empty());
207cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(browser_vars_.empty());
208424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
209424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
2104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool ResourceConverterImpl::FromV8Value(v8::Handle<v8::Object> val,
211424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                        v8::Handle<v8::Context> context,
2124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                        PP_Var* result,
2134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                        bool* was_resource) {
2144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  v8::Context::Scope context_scope(context);
2154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  v8::HandleScope handle_scope(context->GetIsolate());
216424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
2174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  *was_resource = false;
2184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
219f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  blink::WebDOMFileSystem dom_file_system =
220f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      blink::WebDOMFileSystem::fromV8Value(val);
2210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (!dom_file_system.isNull()) {
2220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    int pending_renderer_id;
2230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_ptr<IPC::Message> create_message;
2240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_ptr<IPC::Message> browser_host_create_message;
225a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    if (!DOMFileSystemToResource(instance_,
226a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                 host_,
227a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                 dom_file_system,
228a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                 &pending_renderer_id,
229a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                 &create_message,
2300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                 &browser_host_create_message)) {
2310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      return false;
2320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
2330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    DCHECK(create_message);
2340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    DCHECK(browser_host_create_message);
2350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    scoped_refptr<HostResourceVar> result_var =
2360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        CreateResourceVarWithBrowserHost(
2370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)            pending_renderer_id, *create_message, *browser_host_create_message);
2380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    *result = result_var->GetPPVar();
2390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    *was_resource = true;
2400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return true;
2410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
2420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  blink::WebDOMMediaStreamTrack dom_media_stream_track =
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      blink::WebDOMMediaStreamTrack::fromV8Value(val);
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!dom_media_stream_track.isNull()) {
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int pending_renderer_id;
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<IPC::Message> create_message;
248a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    if (!DOMMediaStreamTrackToResource(instance_,
249a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                       host_,
250a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                       dom_media_stream_track,
251a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                       &pending_renderer_id,
252a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                       &create_message)) {
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return false;
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DCHECK(create_message);
256a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    scoped_refptr<HostResourceVar> result_var =
257a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        CreateResourceVar(pending_renderer_id, *create_message);
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    *result = result_var->GetPPVar();
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    *was_resource = true;
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The value was not convertible to a resource. Return true with
2640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // |was_resource| set to false. As per the interface of FromV8Value, |result|
2650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // may be left unmodified in this case.
2664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return true;
267424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
268424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
269f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void ResourceConverterImpl::Reset() {
270f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  browser_host_create_messages_.clear();
271f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  browser_vars_.clear();
272f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
273f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
274f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)bool ResourceConverterImpl::NeedsFlush() {
275f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return !browser_host_create_messages_.empty();
276f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
277f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
278424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void ResourceConverterImpl::Flush(const base::Callback<void(bool)>& callback) {
279424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  host_->CreateBrowserResourceHosts(
280424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      instance_,
281424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      browser_host_create_messages_,
282cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::Bind(&FlushComplete, callback, browser_vars_));
283424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  browser_host_create_messages_.clear();
284cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  browser_vars_.clear();
285424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
286424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
287a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool ResourceConverterImpl::ToV8Value(const PP_Var& var,
288a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                      v8::Handle<v8::Context> context,
289a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                      v8::Handle<v8::Value>* result) {
290a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(var.type == PP_VARTYPE_RESOURCE);
291a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
292a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ResourceVar* resource = ResourceVar::FromPPVar(var);
293a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!resource) {
294a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    NOTREACHED();
295a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
296a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
297a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  PP_Resource resource_id = resource->GetPPResource();
298a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
299a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Get the renderer-side resource host for this resource.
300a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  content::RendererPpapiHost* renderer_ppapi_host =
301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      content::RendererPpapiHost::GetForPPInstance(instance_);
302a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!renderer_ppapi_host) {
303a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // This should never happen: the RendererPpapiHost is owned by the module
304a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // and should outlive instances associated with it. However, if it doesn't
305a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // for some reason, we do not want to crash.
306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    NOTREACHED();
307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
308a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
309a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  ::ppapi::host::PpapiHost* ppapi_host = renderer_ppapi_host->GetPpapiHost();
310a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ::ppapi::host::ResourceHost* resource_host =
311a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ppapi_host->GetResourceHost(resource_id);
312a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (resource_host == NULL) {
313a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    LOG(ERROR) << "No resource host for resource #" << resource_id;
314a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
315a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
316a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
317a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Convert to the appropriate type of resource host.
318a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (resource_host->IsFileSystemHost()) {
319a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return ResourceHostToDOMFileSystem(
320a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        static_cast<content::PepperFileSystemHost*>(resource_host),
321a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        context,
322a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        result);
323010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  } else if (resource_host->IsMediaStreamVideoTrackHost()) {
324010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return ResourceHostToDOMMediaStreamVideoTrack(
325010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        static_cast<content::PepperMediaStreamVideoTrackHost*>(resource_host),
326010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        context,
327010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        result);
328a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else {
329a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    LOG(ERROR) << "The type of resource #" << resource_id
330a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               << " cannot be converted to a JavaScript object.";
331a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
332a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
333a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
334a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)scoped_refptr<HostResourceVar> ResourceConverterImpl::CreateResourceVar(
3364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    int pending_renderer_id,
337424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const IPC::Message& create_message) {
3384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return new HostResourceVar(pending_renderer_id, create_message);
3393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
3403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
3414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)scoped_refptr<HostResourceVar>
3424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)ResourceConverterImpl::CreateResourceVarWithBrowserHost(
3434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    int pending_renderer_id,
344424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const IPC::Message& create_message,
345424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const IPC::Message& browser_host_create_message) {
3464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_refptr<HostResourceVar> result =
3474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      CreateResourceVar(pending_renderer_id, create_message);
348424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  browser_host_create_messages_.push_back(browser_host_create_message);
349cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  browser_vars_.push_back(result);
350424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return result;
3513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
3523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
3533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}  // namespace content
354