1// Copyright 2013 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 "ppapi/proxy/isolated_file_system_private_resource.h"
6
7#include "base/bind.h"
8#include "base/logging.h"
9#include "ppapi/c/pp_errors.h"
10#include "ppapi/c/pp_file_info.h"
11#include "ppapi/proxy/file_system_resource.h"
12#include "ppapi/proxy/ppapi_messages.h"
13#include "ppapi/proxy/resource_message_params.h"
14#include "ppapi/shared_impl/host_resource.h"
15#include "ppapi/shared_impl/tracked_callback.h"
16#include "ppapi/thunk/enter.h"
17
18namespace ppapi {
19namespace proxy {
20
21namespace {
22void RunTrackedCallback(scoped_refptr<TrackedCallback> callback,
23                        int32_t rc) {
24  callback->Run(rc);
25}
26}  // namespace
27
28IsolatedFileSystemPrivateResource::IsolatedFileSystemPrivateResource(
29    Connection connection, PP_Instance instance)
30    : PluginResource(connection, instance) {
31  SendCreate(BROWSER, PpapiHostMsg_IsolatedFileSystem_Create());
32}
33
34IsolatedFileSystemPrivateResource::~IsolatedFileSystemPrivateResource() {
35}
36
37thunk::PPB_IsolatedFileSystem_Private_API*
38IsolatedFileSystemPrivateResource::AsPPB_IsolatedFileSystem_Private_API() {
39  return this;
40}
41
42int32_t IsolatedFileSystemPrivateResource::Open(
43    PP_Instance /* unused */,
44    PP_IsolatedFileSystemType_Private type,
45    PP_Resource* file_system_resource,
46    scoped_refptr<TrackedCallback> callback) {
47  if (!file_system_resource)
48    return PP_ERROR_BADARGUMENT;
49
50  Call<PpapiPluginMsg_IsolatedFileSystem_BrowserOpenReply>(BROWSER,
51      PpapiHostMsg_IsolatedFileSystem_BrowserOpen(type),
52      base::Bind(&IsolatedFileSystemPrivateResource::OnBrowserOpenComplete,
53                 this, type, file_system_resource, callback));
54  return PP_OK_COMPLETIONPENDING;
55}
56
57void IsolatedFileSystemPrivateResource::OnBrowserOpenComplete(
58    PP_IsolatedFileSystemType_Private type,
59    PP_Resource* file_system_resource,
60    scoped_refptr<TrackedCallback> callback,
61    const ResourceMessageReplyParams& params,
62    const std::string& fsid) {
63  if (!TrackedCallback::IsPending(callback))
64    return;
65
66  if (params.result() != PP_OK) {
67    callback->Run(params.result());
68    return;
69  }
70
71  FileSystemResource* fs = new FileSystemResource(
72      connection(), pp_instance(), PP_FILESYSTEMTYPE_ISOLATED);
73  *file_system_resource = fs->GetReference();
74  if (*file_system_resource == 0)
75    callback->Run(PP_ERROR_FAILED);
76  fs->InitIsolatedFileSystem(
77      fsid, type, base::Bind(&RunTrackedCallback, callback));
78}
79
80}  // namespace proxy
81}  // namespace ppapi
82