| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/plugins/ppapi/ppb_directory_reader_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_directory_reader_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "ppapi/c/pp_completion_callback.h" | 9 #include "ppapi/c/pp_completion_callback.h" |
| 10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/c/dev/ppb_directory_reader_dev.h" | 11 #include "ppapi/c/dev/ppb_directory_reader_dev.h" |
| 12 #include "ppapi/thunk/enter.h" |
| 13 #include "ppapi/thunk/ppb_file_ref_api.h" |
| 12 #include "webkit/plugins/ppapi/common.h" | 14 #include "webkit/plugins/ppapi/common.h" |
| 13 #include "webkit/plugins/ppapi/file_callbacks.h" | 15 #include "webkit/plugins/ppapi/file_callbacks.h" |
| 14 #include "webkit/plugins/ppapi/plugin_delegate.h" | 16 #include "webkit/plugins/ppapi/plugin_delegate.h" |
| 15 #include "webkit/plugins/ppapi/plugin_module.h" | 17 #include "webkit/plugins/ppapi/plugin_module.h" |
| 16 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 18 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 17 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | 19 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
| 18 #include "webkit/plugins/ppapi/ppb_file_system_impl.h" | 20 #include "webkit/plugins/ppapi/ppb_file_system_impl.h" |
| 19 #include "webkit/plugins/ppapi/resource_tracker.h" | 21 #include "webkit/plugins/ppapi/resource_tracker.h" |
| 20 | 22 |
| 23 using ::ppapi::thunk::EnterResourceNoLock; |
| 24 using ::ppapi::thunk::PPB_DirectoryReader_API; |
| 25 using ::ppapi::thunk::PPB_FileRef_API; |
| 26 |
| 21 namespace webkit { | 27 namespace webkit { |
| 22 namespace ppapi { | 28 namespace ppapi { |
| 23 | 29 |
| 24 namespace { | 30 namespace { |
| 25 | 31 |
| 26 std::string FilePathStringToUTF8String(const FilePath::StringType& str) { | 32 std::string FilePathStringToUTF8String(const FilePath::StringType& str) { |
| 27 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
| 28 return WideToUTF8(str); | 34 return WideToUTF8(str); |
| 29 #elif defined(OS_POSIX) | 35 #elif defined(OS_POSIX) |
| 30 return str; | 36 return str; |
| 31 #else | 37 #else |
| 32 #error "Unsupported platform." | 38 #error "Unsupported platform." |
| 33 #endif | 39 #endif |
| 34 } | 40 } |
| 35 | 41 |
| 36 FilePath::StringType UTF8StringToFilePathString(const std::string& str) { | 42 FilePath::StringType UTF8StringToFilePathString(const std::string& str) { |
| 37 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
| 38 return UTF8ToWide(str); | 44 return UTF8ToWide(str); |
| 39 #elif defined(OS_POSIX) | 45 #elif defined(OS_POSIX) |
| 40 return str; | 46 return str; |
| 41 #else | 47 #else |
| 42 #error "Unsupported platform." | 48 #error "Unsupported platform." |
| 43 #endif | 49 #endif |
| 44 } | 50 } |
| 45 | 51 |
| 46 PP_Resource Create(PP_Resource directory_ref_id) { | |
| 47 scoped_refptr<PPB_FileRef_Impl> directory_ref( | |
| 48 Resource::GetAs<PPB_FileRef_Impl>(directory_ref_id)); | |
| 49 if (!directory_ref) | |
| 50 return 0; | |
| 51 | |
| 52 PPB_DirectoryReader_Impl* reader = | |
| 53 new PPB_DirectoryReader_Impl(directory_ref); | |
| 54 return reader->GetReference(); | |
| 55 } | |
| 56 | |
| 57 PP_Bool IsDirectoryReader(PP_Resource resource) { | |
| 58 return BoolToPPBool(!!Resource::GetAs<PPB_DirectoryReader_Impl>(resource)); | |
| 59 } | |
| 60 | |
| 61 int32_t GetNextEntry(PP_Resource reader_id, | |
| 62 PP_DirectoryEntry_Dev* entry, | |
| 63 PP_CompletionCallback callback) { | |
| 64 scoped_refptr<PPB_DirectoryReader_Impl> reader( | |
| 65 Resource::GetAs<PPB_DirectoryReader_Impl>(reader_id)); | |
| 66 if (!reader) | |
| 67 return PP_ERROR_BADRESOURCE; | |
| 68 | |
| 69 return reader->GetNextEntry(entry, callback); | |
| 70 } | |
| 71 | |
| 72 const PPB_DirectoryReader_Dev ppb_directoryreader = { | |
| 73 &Create, | |
| 74 &IsDirectoryReader, | |
| 75 &GetNextEntry | |
| 76 }; | |
| 77 | |
| 78 } // namespace | 52 } // namespace |
| 79 | 53 |
| 80 PPB_DirectoryReader_Impl::PPB_DirectoryReader_Impl( | 54 PPB_DirectoryReader_Impl::PPB_DirectoryReader_Impl( |
| 81 PPB_FileRef_Impl* directory_ref) | 55 PPB_FileRef_Impl* directory_ref) |
| 82 : Resource(directory_ref->instance()), | 56 : Resource(directory_ref->instance()), |
| 83 directory_ref_(directory_ref), | 57 directory_ref_(directory_ref), |
| 84 has_more_(true), | 58 has_more_(true), |
| 85 entry_(NULL) { | 59 entry_(NULL) { |
| 86 } | 60 } |
| 87 | 61 |
| 88 PPB_DirectoryReader_Impl::~PPB_DirectoryReader_Impl() { | 62 PPB_DirectoryReader_Impl::~PPB_DirectoryReader_Impl() { |
| 89 } | 63 } |
| 90 | 64 |
| 91 const PPB_DirectoryReader_Dev* PPB_DirectoryReader_Impl::GetInterface() { | 65 // static |
| 92 return &ppb_directoryreader; | 66 PP_Resource PPB_DirectoryReader_Impl::Create(PP_Resource directory_ref) { |
| 67 EnterResourceNoLock<PPB_FileRef_API> enter(directory_ref, true); |
| 68 if (enter.failed()) |
| 69 return 0; |
| 70 |
| 71 PPB_DirectoryReader_Impl* reader = new PPB_DirectoryReader_Impl( |
| 72 static_cast<PPB_FileRef_Impl*>(enter.object())); |
| 73 return reader->GetReference(); |
| 93 } | 74 } |
| 94 | 75 |
| 95 PPB_DirectoryReader_Impl* | 76 PPB_DirectoryReader_Impl* |
| 96 PPB_DirectoryReader_Impl::AsPPB_DirectoryReader_Impl() { | 77 PPB_DirectoryReader_Impl::AsPPB_DirectoryReader_Impl() { |
| 97 return this; | 78 return this; |
| 98 } | 79 } |
| 99 | 80 |
| 81 PPB_DirectoryReader_API* PPB_DirectoryReader_Impl::AsPPB_DirectoryReader_API() { |
| 82 return this; |
| 83 } |
| 84 |
| 100 int32_t PPB_DirectoryReader_Impl::GetNextEntry( | 85 int32_t PPB_DirectoryReader_Impl::GetNextEntry( |
| 101 PP_DirectoryEntry_Dev* entry, | 86 PP_DirectoryEntry_Dev* entry, |
| 102 PP_CompletionCallback callback) { | 87 PP_CompletionCallback callback) { |
| 103 if (directory_ref_->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | 88 if (directory_ref_->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) |
| 104 return PP_ERROR_FAILED; | 89 return PP_ERROR_FAILED; |
| 105 | 90 |
| 106 entry_ = entry; | 91 entry_ = entry; |
| 107 if (FillUpEntry()) { | 92 if (FillUpEntry()) { |
| 108 entry_ = NULL; | 93 entry_ = NULL; |
| 109 return PP_OK; | 94 return PP_OK; |
| 110 } | 95 } |
| 111 | 96 |
| 112 PluginInstance* instance = directory_ref_->GetFileSystem()->instance(); | 97 PluginInstance* instance = directory_ref_->instance(); |
| 113 PP_Resource resource_id = GetReferenceNoAddRef(); | 98 PP_Resource resource_id = GetReferenceNoAddRef(); |
| 114 DCHECK(resource_id != 0); | 99 DCHECK(resource_id != 0); |
| 115 if (!instance->delegate()->ReadDirectory( | 100 if (!instance->delegate()->ReadDirectory( |
| 116 directory_ref_->GetFileSystemURL(), | 101 directory_ref_->GetFileSystemURL(), |
| 117 new FileCallbacks(instance->module()->AsWeakPtr(), | 102 new FileCallbacks(instance->module()->AsWeakPtr(), |
| 118 resource_id, | 103 resource_id, |
| 119 callback, NULL, NULL, this))) | 104 callback, NULL, NULL, this))) |
| 120 return PP_ERROR_FAILED; | 105 return PP_ERROR_FAILED; |
| 121 | 106 |
| 122 return PP_OK_COMPLETIONPENDING; | 107 return PP_OK_COMPLETIONPENDING; |
| 123 } | 108 } |
| 124 | 109 |
| 125 void PPB_DirectoryReader_Impl::AddNewEntries( | 110 void PPB_DirectoryReader_Impl::AddNewEntries( |
| 126 const std::vector<base::FileUtilProxy::Entry>& entries, bool has_more) { | 111 const std::vector<base::FileUtilProxy::Entry>& entries, bool has_more) { |
| 127 DCHECK(!entries.empty() || !has_more); | 112 DCHECK(!entries.empty() || !has_more); |
| 128 has_more_ = has_more; | 113 has_more_ = has_more; |
| 129 std::string dir_path = directory_ref_->GetPath(); | 114 std::string dir_path = directory_ref_->virtual_path(); |
| 130 if (dir_path[dir_path.size() - 1] != '/') | 115 if (dir_path[dir_path.size() - 1] != '/') |
| 131 dir_path += '/'; | 116 dir_path += '/'; |
| 132 FilePath::StringType dir_file_path = UTF8StringToFilePathString(dir_path); | 117 FilePath::StringType dir_file_path = UTF8StringToFilePathString(dir_path); |
| 133 for (std::vector<base::FileUtilProxy::Entry>::const_iterator it = | 118 for (std::vector<base::FileUtilProxy::Entry>::const_iterator it = |
| 134 entries.begin(); it != entries.end(); it++) { | 119 entries.begin(); it != entries.end(); it++) { |
| 135 base::FileUtilProxy::Entry entry; | 120 base::FileUtilProxy::Entry entry; |
| 136 entry.name = dir_file_path + it->name; | 121 entry.name = dir_file_path + it->name; |
| 137 entry.is_directory = it->is_directory; | 122 entry.is_directory = it->is_directory; |
| 138 entries_.push(entry); | 123 entries_.push(entry); |
| 139 } | 124 } |
| 140 | 125 |
| 141 FillUpEntry(); | 126 FillUpEntry(); |
| 142 entry_ = NULL; | 127 entry_ = NULL; |
| 143 } | 128 } |
| 144 | 129 |
| 145 bool PPB_DirectoryReader_Impl::FillUpEntry() { | 130 bool PPB_DirectoryReader_Impl::FillUpEntry() { |
| 146 DCHECK(entry_); | 131 DCHECK(entry_); |
| 147 if (!entries_.empty()) { | 132 if (!entries_.empty()) { |
| 148 base::FileUtilProxy::Entry dir_entry = entries_.front(); | 133 base::FileUtilProxy::Entry dir_entry = entries_.front(); |
| 149 entries_.pop(); | 134 entries_.pop(); |
| 150 if (entry_->file_ref) | 135 if (entry_->file_ref) |
| 151 ResourceTracker::Get()->UnrefResource(entry_->file_ref); | 136 ResourceTracker::Get()->UnrefResource(entry_->file_ref); |
| 152 PPB_FileRef_Impl* file_ref = | 137 PPB_FileRef_Impl* file_ref = |
| 153 new PPB_FileRef_Impl(instance(), directory_ref_->GetFileSystem(), | 138 new PPB_FileRef_Impl(instance(), directory_ref_->file_system(), |
| 154 FilePathStringToUTF8String(dir_entry.name)); | 139 FilePathStringToUTF8String(dir_entry.name)); |
| 155 entry_->file_ref = file_ref->GetReference(); | 140 entry_->file_ref = file_ref->GetReference(); |
| 156 entry_->file_type = | 141 entry_->file_type = |
| 157 (dir_entry.is_directory ? PP_FILETYPE_DIRECTORY : PP_FILETYPE_REGULAR); | 142 (dir_entry.is_directory ? PP_FILETYPE_DIRECTORY : PP_FILETYPE_REGULAR); |
| 158 return true; | 143 return true; |
| 159 } | 144 } |
| 160 | 145 |
| 161 if (!has_more_) { | 146 if (!has_more_) { |
| 162 entry_->file_ref = 0; | 147 entry_->file_ref = 0; |
| 163 return true; | 148 return true; |
| 164 } | 149 } |
| 165 | 150 |
| 166 return false; | 151 return false; |
| 167 } | 152 } |
| 168 | 153 |
| 169 } // namespace ppapi | 154 } // namespace ppapi |
| 170 } // namespace webkit | 155 } // namespace webkit |
| OLD | NEW |