- add sources.
[platform/framework/web/crosswalk.git] / src / ppapi / cpp / private / flash_file.cc
1 // Copyright (c) 2012 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/cpp/private/flash_file.h"
6
7 #include "ppapi/c/pp_bool.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/cpp/file_ref.h"
10 #include "ppapi/cpp/instance_handle.h"
11 #include "ppapi/cpp/module_impl.h"
12
13 namespace pp {
14
15 // FileModuleLocal -------------------------------------------------------------
16
17 namespace {
18
19 template <> const char* interface_name<PPB_Flash_File_ModuleLocal_3_0>() {
20   return PPB_FLASH_FILE_MODULELOCAL_INTERFACE_3_0;
21 }
22
23 }  // namespace
24
25 namespace flash {
26
27 static FileModuleLocal::DirEntry ConvertDirEntry(const PP_DirEntry_Dev& entry) {
28   FileModuleLocal::DirEntry rv = { entry.name, PP_ToBool(entry.is_dir) };
29   return rv;
30 }
31
32 // static
33 bool FileModuleLocal::IsAvailable() {
34   return has_interface<PPB_Flash_File_ModuleLocal_3_0>();
35 }
36
37 // static
38 PP_FileHandle FileModuleLocal::OpenFile(const InstanceHandle& instance,
39                                         const std::string& path,
40                                         int32_t mode) {
41   PP_FileHandle file_handle = PP_kInvalidFileHandle;
42   int32_t result = PP_ERROR_FAILED;
43   if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
44     result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
45         OpenFile(instance.pp_instance(), path.c_str(), mode, &file_handle);
46   }
47   return (result == PP_OK) ? file_handle : PP_kInvalidFileHandle;
48 }
49
50 // static
51 bool FileModuleLocal::RenameFile(const InstanceHandle& instance,
52                                  const std::string& path_from,
53                                  const std::string& path_to) {
54   int32_t result = PP_ERROR_FAILED;
55   if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
56     result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
57         RenameFile(instance.pp_instance(), path_from.c_str(), path_to.c_str());
58   }
59   return result == PP_OK;
60 }
61
62 // static
63 bool FileModuleLocal::DeleteFileOrDir(const InstanceHandle& instance,
64                                       const std::string& path,
65                                       bool recursive) {
66   int32_t result = PP_ERROR_FAILED;
67   if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
68     result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
69         DeleteFileOrDir(instance.pp_instance(), path.c_str(),
70                         PP_FromBool(recursive));
71   }
72   return result == PP_OK;
73 }
74
75 // static
76 bool FileModuleLocal::CreateDir(const InstanceHandle& instance,
77                                 const std::string& path) {
78   int32_t result = PP_ERROR_FAILED;
79   if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
80     result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
81         CreateDir(instance.pp_instance(), path.c_str());
82   }
83   return result == PP_OK;
84 }
85
86 // static
87 bool FileModuleLocal::QueryFile(const InstanceHandle& instance,
88                                 const std::string& path,
89                                 PP_FileInfo* info) {
90   int32_t result = PP_ERROR_FAILED;
91   if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
92     result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
93         QueryFile(instance.pp_instance(), path.c_str(), info);
94   }
95   return result == PP_OK;
96 }
97
98 // static
99 bool FileModuleLocal::GetDirContents(
100     const InstanceHandle& instance,
101     const std::string& path,
102     std::vector<DirEntry>* dir_contents) {
103   dir_contents->clear();
104
105   int32_t result = PP_ERROR_FAILED;
106   if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
107     PP_DirContents_Dev* contents = NULL;
108     result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
109         GetDirContents(instance.pp_instance(), path.c_str(), &contents);
110     if (result == PP_OK && contents) {
111       for (int32_t i = 0; i < contents->count; i++)
112         dir_contents->push_back(ConvertDirEntry(contents->entries[i]));
113     }
114     if (contents) {
115         get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
116             FreeDirContents(instance.pp_instance(), contents);
117     }
118   }
119   return result == PP_OK;
120 }
121
122 // static
123 PP_FileHandle FileModuleLocal::CreateTemporaryFile(
124     const InstanceHandle& instance) {
125   PP_FileHandle file_handle = PP_kInvalidFileHandle;
126   int32_t result = PP_ERROR_FAILED;
127   if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
128     result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
129         CreateTemporaryFile(instance.pp_instance(), &file_handle);
130   }
131   return (result == PP_OK) ? file_handle : PP_kInvalidFileHandle;
132 }
133
134 }  // namespace flash
135
136 // FileFileRef -----------------------------------------------------------------
137
138 namespace {
139
140 template <> const char* interface_name<PPB_Flash_File_FileRef>() {
141   return PPB_FLASH_FILE_FILEREF_INTERFACE;
142 }
143
144 }  // namespace
145
146 namespace flash {
147
148 // static
149 bool FileFileRef::IsAvailable() {
150   return has_interface<PPB_Flash_File_FileRef>();
151 }
152
153 // static
154 PP_FileHandle FileFileRef::OpenFile(const pp::FileRef& resource,
155                                     int32_t mode) {
156   PP_FileHandle file_handle = PP_kInvalidFileHandle;
157   int32_t result = PP_ERROR_FAILED;
158   if (has_interface<PPB_Flash_File_FileRef>()) {
159     result = get_interface<PPB_Flash_File_FileRef>()->
160         OpenFile(resource.pp_resource(), mode, &file_handle);
161   }
162   return (result == PP_OK) ? file_handle : PP_kInvalidFileHandle;
163 }
164
165 // static
166 bool FileFileRef::QueryFile(const pp::FileRef& resource,
167                             PP_FileInfo* info) {
168   int32_t result = PP_ERROR_FAILED;
169   if (has_interface<PPB_Flash_File_FileRef>()) {
170     result = get_interface<PPB_Flash_File_FileRef>()->
171         QueryFile(resource.pp_resource(), info);
172   }
173   return result == PP_OK;
174 }
175
176 }  // namespace flash
177
178 }  // namespace pp