file_system_resource.cc revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright (c) 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/file_system_resource.h"
6
7#include "base/bind.h"
8#include "ipc/ipc_message.h"
9#include "ppapi/c/pp_errors.h"
10#include "ppapi/proxy/ppapi_messages.h"
11#include "ppapi/shared_impl/tracked_callback.h"
12
13using ppapi::thunk::PPB_FileSystem_API;
14
15namespace ppapi {
16namespace proxy {
17
18FileSystemResource::FileSystemResource(Connection connection,
19                                       PP_Instance instance,
20                                       PP_FileSystemType type)
21    : PluginResource(connection, instance),
22      type_(type),
23      called_open_(false),
24      callback_count_(0),
25      callback_result_(PP_OK) {
26  DCHECK(type_ != PP_FILESYSTEMTYPE_INVALID);
27  SendCreate(RENDERER, PpapiHostMsg_FileSystem_Create(type_));
28  SendCreate(BROWSER, PpapiHostMsg_FileSystem_Create(type_));
29}
30
31FileSystemResource::FileSystemResource(Connection connection,
32                                       PP_Instance instance,
33                                       int pending_renderer_id,
34                                       int pending_browser_id,
35                                       PP_FileSystemType type)
36    : PluginResource(connection, instance),
37      type_(type),
38      called_open_(true),
39      callback_count_(0),
40      callback_result_(PP_OK) {
41  DCHECK(type_ != PP_FILESYSTEMTYPE_INVALID);
42  AttachToPendingHost(RENDERER, pending_renderer_id);
43  AttachToPendingHost(BROWSER, pending_browser_id);
44}
45
46FileSystemResource::~FileSystemResource() {
47}
48
49PPB_FileSystem_API* FileSystemResource::AsPPB_FileSystem_API() {
50  return this;
51}
52
53int32_t FileSystemResource::Open(int64_t expected_size,
54                                 scoped_refptr<TrackedCallback> callback) {
55  DCHECK(type_ != PP_FILESYSTEMTYPE_ISOLATED);
56  if (called_open_)
57    return PP_ERROR_FAILED;
58  called_open_ = true;
59
60  Call<PpapiPluginMsg_FileSystem_OpenReply>(RENDERER,
61      PpapiHostMsg_FileSystem_Open(expected_size),
62      base::Bind(&FileSystemResource::OpenComplete,
63                 this,
64                 callback));
65  Call<PpapiPluginMsg_FileSystem_OpenReply>(BROWSER,
66      PpapiHostMsg_FileSystem_Open(expected_size),
67      base::Bind(&FileSystemResource::OpenComplete,
68                 this,
69                 callback));
70  return PP_OK_COMPLETIONPENDING;
71}
72
73PP_FileSystemType FileSystemResource::GetType() {
74  return type_;
75}
76
77int32_t FileSystemResource::InitIsolatedFileSystem(
78    const std::string& fsid,
79    PP_IsolatedFileSystemType_Private type,
80    const base::Callback<void(int32_t)>& callback) {
81  // This call is mutually exclusive with Open() above, so we can reuse the
82  // called_open state.
83  DCHECK(type_ == PP_FILESYSTEMTYPE_ISOLATED);
84  if (called_open_)
85    return PP_ERROR_FAILED;
86  called_open_ = true;
87
88  Call<PpapiPluginMsg_FileSystem_InitIsolatedFileSystemReply>(RENDERER,
89      PpapiHostMsg_FileSystem_InitIsolatedFileSystem(fsid, type),
90      base::Bind(&FileSystemResource::InitIsolatedFileSystemComplete,
91      this,
92      callback));
93  Call<PpapiPluginMsg_FileSystem_InitIsolatedFileSystemReply>(BROWSER,
94      PpapiHostMsg_FileSystem_InitIsolatedFileSystem(fsid, type),
95      base::Bind(&FileSystemResource::InitIsolatedFileSystemComplete,
96      this,
97      callback));
98  return PP_OK_COMPLETIONPENDING;
99}
100
101void FileSystemResource::OpenComplete(
102    scoped_refptr<TrackedCallback> callback,
103    const ResourceMessageReplyParams& params) {
104  ++callback_count_;
105  // Prioritize worse result since only one status can be returned.
106  if (params.result() != PP_OK)
107    callback_result_ = params.result();
108  // Received callback from browser and renderer.
109  if (callback_count_ == 2)
110    callback->Run(callback_result_);
111}
112
113void FileSystemResource::InitIsolatedFileSystemComplete(
114    const base::Callback<void(int32_t)>& callback,
115    const ResourceMessageReplyParams& params) {
116  ++callback_count_;
117  // Prioritize worse result since only one status can be returned.
118  if (params.result() != PP_OK)
119    callback_result_ = params.result();
120  // Received callback from browser and renderer.
121  if (callback_count_ == 2)
122    callback.Run(callback_result_);
123}
124
125}  // namespace proxy
126}  // namespace ppapi
127