summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/file_callbacks.cc
blob: 92d443b11ab3bb518f4804a532d7d9d537a4f333 (plain)
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
// Copyright (c) 2010 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 "webkit/plugins/ppapi/file_callbacks.h"

#include "base/file_path.h"
#include "base/logging.h"
#include "ppapi/c/dev/ppb_file_system_dev.h"
#include "ppapi/c/dev/pp_file_info_dev.h"
#include "ppapi/c/pp_errors.h"
#include "webkit/plugins/ppapi/callbacks.h"
#include "webkit/plugins/ppapi/error_util.h"
#include "webkit/plugins/ppapi/plugin_module.h"
#include "webkit/plugins/ppapi/ppb_directory_reader_impl.h"
#include "webkit/plugins/ppapi/ppb_file_system_impl.h"
#include "webkit/fileapi/file_system_types.h"

namespace webkit {
namespace ppapi {

FileCallbacks::FileCallbacks(
    const base::WeakPtr<PluginModule>& module,
    PP_Resource resource_id,
    PP_CompletionCallback callback,
    PP_FileInfo_Dev* info,
    scoped_refptr<PPB_FileSystem_Impl> file_system,
    scoped_refptr<PPB_DirectoryReader_Impl> directory_reader)
    : callback_(new TrackedCompletionCallback(module->GetCallbackTracker(),
                                              resource_id,
                                              callback)),
      info_(info),
      file_system_(file_system),
      directory_reader_(directory_reader) {
}

FileCallbacks::~FileCallbacks() {}

void FileCallbacks::DidSucceed() {
  if (callback_->completed())
    return;

  callback_->Run(PP_OK);
}

void FileCallbacks::DidReadMetadata(
    const base::PlatformFileInfo& file_info,
    const FilePath& unused) {
  if (callback_->completed())
    return;

  DCHECK(info_);
  DCHECK(file_system_);
  info_->size = file_info.size;
  info_->creation_time = file_info.creation_time.ToDoubleT();
  info_->last_access_time = file_info.last_accessed.ToDoubleT();
  info_->last_modified_time = file_info.last_modified.ToDoubleT();
  info_->system_type = file_system_->type();
  if (file_info.is_directory)
    info_->type = PP_FILETYPE_DIRECTORY;
  else
    info_->type = PP_FILETYPE_REGULAR;

  callback_->Run(PP_OK);
}

void FileCallbacks::DidReadDirectory(
    const std::vector<base::FileUtilProxy::Entry>& entries, bool has_more) {
  if (callback_->completed())
    return;

  DCHECK(directory_reader_);
  directory_reader_->AddNewEntries(entries, has_more);

  callback_->Run(PP_OK);
}

void FileCallbacks::DidOpenFileSystem(const std::string&,
                                      const FilePath& root_path) {
  if (callback_->completed())
    return;

  DCHECK(file_system_);
  file_system_->set_root_path(root_path);
  file_system_->set_opened(true);

  callback_->Run(PP_OK);
}

void FileCallbacks::DidFail(base::PlatformFileError error_code) {
  RunCallback(error_code);
}

void FileCallbacks::DidWrite(int64 bytes, bool complete) {
  NOTREACHED();
}

void FileCallbacks::RunCallback(base::PlatformFileError error_code) {
  if (callback_->completed())
    return;

  callback_->Run(PlatformFileErrorToPepperError(error_code));
}

}  // namespace ppapi
}  // namespace webkit