| OLD | NEW |
| (Empty) |
| 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 "webkit/plugins/ppapi/ppb_directory_reader_impl.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "ppapi/c/pp_completion_callback.h" | |
| 10 #include "ppapi/c/pp_errors.h" | |
| 11 #include "ppapi/c/dev/ppb_directory_reader_dev.h" | |
| 12 #include "ppapi/shared_impl/ppapi_globals.h" | |
| 13 #include "ppapi/shared_impl/resource_tracker.h" | |
| 14 #include "ppapi/thunk/enter.h" | |
| 15 #include "ppapi/thunk/ppb_file_ref_api.h" | |
| 16 #include "webkit/plugins/ppapi/common.h" | |
| 17 #include "webkit/plugins/ppapi/file_callbacks.h" | |
| 18 #include "webkit/plugins/ppapi/plugin_delegate.h" | |
| 19 #include "webkit/plugins/ppapi/plugin_module.h" | |
| 20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
| 21 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | |
| 22 #include "webkit/plugins/ppapi/ppb_file_system_impl.h" | |
| 23 #include "webkit/plugins/ppapi/resource_helper.h" | |
| 24 | |
| 25 using ::ppapi::PpapiGlobals; | |
| 26 using ::ppapi::TrackedCallback; | |
| 27 using ::ppapi::thunk::EnterResourceNoLock; | |
| 28 using ::ppapi::thunk::PPB_DirectoryReader_API; | |
| 29 using ::ppapi::thunk::PPB_FileRef_API; | |
| 30 | |
| 31 namespace webkit { | |
| 32 namespace ppapi { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 std::string FilePathStringToUTF8String(const base::FilePath::StringType& str) { | |
| 37 #if defined(OS_WIN) | |
| 38 return WideToUTF8(str); | |
| 39 #elif defined(OS_POSIX) | |
| 40 return str; | |
| 41 #else | |
| 42 #error "Unsupported platform." | |
| 43 #endif | |
| 44 } | |
| 45 | |
| 46 base::FilePath::StringType UTF8StringToFilePathString(const std::string& str) { | |
| 47 #if defined(OS_WIN) | |
| 48 return UTF8ToWide(str); | |
| 49 #elif defined(OS_POSIX) | |
| 50 return str; | |
| 51 #else | |
| 52 #error "Unsupported platform." | |
| 53 #endif | |
| 54 } | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 PPB_DirectoryReader_Impl::PPB_DirectoryReader_Impl( | |
| 59 PPB_FileRef_Impl* directory_ref) | |
| 60 : Resource(::ppapi::OBJECT_IS_IMPL, directory_ref->pp_instance()), | |
| 61 directory_ref_(directory_ref), | |
| 62 has_more_(true), | |
| 63 entry_(NULL) { | |
| 64 } | |
| 65 | |
| 66 PPB_DirectoryReader_Impl::~PPB_DirectoryReader_Impl() { | |
| 67 } | |
| 68 | |
| 69 // static | |
| 70 PP_Resource PPB_DirectoryReader_Impl::Create(PP_Resource directory_ref) { | |
| 71 EnterResourceNoLock<PPB_FileRef_API> enter(directory_ref, true); | |
| 72 if (enter.failed()) | |
| 73 return 0; | |
| 74 return (new PPB_DirectoryReader_Impl( | |
| 75 static_cast<PPB_FileRef_Impl*>(enter.object())))->GetReference(); | |
| 76 } | |
| 77 | |
| 78 PPB_DirectoryReader_API* PPB_DirectoryReader_Impl::AsPPB_DirectoryReader_API() { | |
| 79 return this; | |
| 80 } | |
| 81 | |
| 82 int32_t PPB_DirectoryReader_Impl::GetNextEntry( | |
| 83 PP_DirectoryEntry_Dev* entry, | |
| 84 scoped_refptr<TrackedCallback> callback) { | |
| 85 if (directory_ref_->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | |
| 86 return PP_ERROR_FAILED; | |
| 87 | |
| 88 entry_ = entry; | |
| 89 if (FillUpEntry()) { | |
| 90 entry_ = NULL; | |
| 91 return PP_OK; | |
| 92 } | |
| 93 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); | |
| 94 if (!plugin_instance) | |
| 95 return PP_ERROR_FAILED; | |
| 96 | |
| 97 if (!plugin_instance->delegate()->ReadDirectory( | |
| 98 directory_ref_->GetFileSystemURL(), | |
| 99 new FileCallbacks(this, callback, NULL, NULL, this))) | |
| 100 return PP_ERROR_FAILED; | |
| 101 | |
| 102 return PP_OK_COMPLETIONPENDING; | |
| 103 } | |
| 104 | |
| 105 void PPB_DirectoryReader_Impl::AddNewEntries( | |
| 106 const std::vector<base::FileUtilProxy::Entry>& entries, bool has_more) { | |
| 107 DCHECK(!entries.empty() || !has_more); | |
| 108 has_more_ = has_more; | |
| 109 | |
| 110 std::string dir_path = directory_ref_->GetCreateInfo().path; | |
| 111 if (dir_path[dir_path.size() - 1] != '/') | |
| 112 dir_path += '/'; | |
| 113 base::FilePath::StringType dir_file_path = UTF8StringToFilePathString(dir_path
); | |
| 114 for (std::vector<base::FileUtilProxy::Entry>::const_iterator it = | |
| 115 entries.begin(); it != entries.end(); it++) { | |
| 116 base::FileUtilProxy::Entry entry; | |
| 117 entry.name = dir_file_path + it->name; | |
| 118 entry.is_directory = it->is_directory; | |
| 119 entries_.push(entry); | |
| 120 } | |
| 121 | |
| 122 FillUpEntry(); | |
| 123 entry_ = NULL; | |
| 124 } | |
| 125 | |
| 126 bool PPB_DirectoryReader_Impl::FillUpEntry() { | |
| 127 DCHECK(entry_); | |
| 128 if (!entries_.empty()) { | |
| 129 base::FileUtilProxy::Entry dir_entry = entries_.front(); | |
| 130 entries_.pop(); | |
| 131 if (entry_->file_ref) { | |
| 132 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource( | |
| 133 entry_->file_ref); | |
| 134 } | |
| 135 | |
| 136 PPB_FileRef_Impl* file_ref = PPB_FileRef_Impl::CreateInternal( | |
| 137 directory_ref_->file_system()->pp_resource(), | |
| 138 FilePathStringToUTF8String(dir_entry.name)); | |
| 139 if (!file_ref) | |
| 140 return false; | |
| 141 entry_->file_ref = file_ref->GetReference(); | |
| 142 entry_->file_type = | |
| 143 (dir_entry.is_directory ? PP_FILETYPE_DIRECTORY : PP_FILETYPE_REGULAR); | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 if (!has_more_) { | |
| 148 entry_->file_ref = 0; | |
| 149 return true; | |
| 150 } | |
| 151 | |
| 152 return false; | |
| 153 } | |
| 154 | |
| 155 } // namespace ppapi | |
| 156 } // namespace webkit | |
| OLD | NEW |