OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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/glue/plugins/pepper_directory_reader.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 "webkit/glue/plugins/pepper_file_callbacks.h" | |
13 #include "webkit/glue/plugins/pepper_common.h" | |
14 #include "webkit/glue/plugins/pepper_file_ref.h" | |
15 #include "webkit/glue/plugins/pepper_file_system.h" | |
16 #include "webkit/glue/plugins/pepper_plugin_delegate.h" | |
17 #include "webkit/glue/plugins/pepper_plugin_instance.h" | |
18 #include "webkit/glue/plugins/pepper_plugin_module.h" | |
19 #include "webkit/glue/plugins/pepper_resource_tracker.h" | |
20 | |
21 namespace pepper { | |
22 | |
23 namespace { | |
24 | |
25 std::string FilePathStringToUTF8String(const FilePath::StringType& str) { | |
26 #if defined(OS_WIN) | |
27 return WideToUTF8(str); | |
28 #elif defined(OS_POSIX) | |
29 return str; | |
30 #else | |
31 #error "Unsupported platform." | |
32 #endif | |
33 } | |
34 | |
35 FilePath::StringType UTF8StringToFilePathString(const std::string& str) { | |
36 #if defined(OS_WIN) | |
37 return UTF8ToWide(str); | |
38 #elif defined(OS_POSIX) | |
39 return str; | |
40 #else | |
41 #error "Unsupported platform." | |
42 #endif | |
43 } | |
44 | |
45 PP_Resource Create(PP_Resource directory_ref_id) { | |
46 scoped_refptr<FileRef> directory_ref( | |
47 Resource::GetAs<FileRef>(directory_ref_id)); | |
48 if (!directory_ref) | |
49 return 0; | |
50 | |
51 DirectoryReader* reader = new DirectoryReader(directory_ref); | |
52 return reader->GetReference(); | |
53 } | |
54 | |
55 PP_Bool IsDirectoryReader(PP_Resource resource) { | |
56 return BoolToPPBool(!!Resource::GetAs<DirectoryReader>(resource)); | |
57 } | |
58 | |
59 int32_t GetNextEntry(PP_Resource reader_id, | |
60 PP_DirectoryEntry_Dev* entry, | |
61 PP_CompletionCallback callback) { | |
62 scoped_refptr<DirectoryReader> reader( | |
63 Resource::GetAs<DirectoryReader>(reader_id)); | |
64 if (!reader) | |
65 return PP_ERROR_BADRESOURCE; | |
66 | |
67 return reader->GetNextEntry(entry, callback); | |
68 } | |
69 | |
70 const PPB_DirectoryReader_Dev ppb_directoryreader = { | |
71 &Create, | |
72 &IsDirectoryReader, | |
73 &GetNextEntry | |
74 }; | |
75 | |
76 } // namespace | |
77 | |
78 DirectoryReader::DirectoryReader(FileRef* directory_ref) | |
79 : Resource(directory_ref->module()), | |
80 directory_ref_(directory_ref), | |
81 has_more_(true), | |
82 entry_(NULL) { | |
83 } | |
84 | |
85 DirectoryReader::~DirectoryReader() { | |
86 } | |
87 | |
88 const PPB_DirectoryReader_Dev* DirectoryReader::GetInterface() { | |
89 return &ppb_directoryreader; | |
90 } | |
91 | |
92 DirectoryReader* DirectoryReader::AsDirectoryReader() { | |
93 return this; | |
94 } | |
95 | |
96 int32_t DirectoryReader::GetNextEntry(PP_DirectoryEntry_Dev* entry, | |
97 PP_CompletionCallback callback) { | |
98 if (directory_ref_->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | |
99 return PP_ERROR_FAILED; | |
100 | |
101 entry_ = entry; | |
102 if (FillUpEntry()) { | |
103 entry_ = NULL; | |
104 return PP_OK; | |
105 } | |
106 | |
107 PluginInstance* instance = directory_ref_->GetFileSystem()->instance(); | |
108 if (!instance->delegate()->ReadDirectory( | |
109 directory_ref_->GetSystemPath(), | |
110 new FileCallbacks(instance->module()->AsWeakPtr(), | |
111 callback, NULL, NULL, this))) | |
112 return PP_ERROR_FAILED; | |
113 | |
114 return PP_ERROR_WOULDBLOCK; | |
115 } | |
116 | |
117 void DirectoryReader::AddNewEntries( | |
118 const std::vector<base::FileUtilProxy::Entry>& entries, bool has_more) { | |
119 DCHECK(!entries.empty()); | |
120 has_more_ = has_more; | |
121 std::string dir_path = directory_ref_->GetPath(); | |
122 if (dir_path[dir_path.size() - 1] != '/') | |
123 dir_path += '/'; | |
124 FilePath::StringType dir_file_path = UTF8StringToFilePathString(dir_path); | |
125 for (std::vector<base::FileUtilProxy::Entry>::const_iterator it = | |
126 entries.begin(); it != entries.end(); it++) { | |
127 base::FileUtilProxy::Entry entry; | |
128 entry.name = dir_file_path + it->name; | |
129 entry.is_directory = it->is_directory; | |
130 entries_.push(entry); | |
131 } | |
132 | |
133 FillUpEntry(); | |
134 entry_ = NULL; | |
135 } | |
136 | |
137 bool DirectoryReader::FillUpEntry() { | |
138 DCHECK(entry_); | |
139 if (!entries_.empty()) { | |
140 base::FileUtilProxy::Entry dir_entry = entries_.front(); | |
141 entries_.pop(); | |
142 if (entry_->file_ref) | |
143 ResourceTracker::Get()->UnrefResource(entry_->file_ref); | |
144 FileRef* file_ref = new FileRef(module(), directory_ref_->GetFileSystem(), | |
145 FilePathStringToUTF8String(dir_entry.name)); | |
146 entry_->file_ref = file_ref->GetReference(); | |
147 entry_->file_type = | |
148 (dir_entry.is_directory ? PP_FILETYPE_DIRECTORY : PP_FILETYPE_REGULAR); | |
149 return true; | |
150 } | |
151 | |
152 if (!has_more_) { | |
153 entry_->file_ref = 0; | |
154 return true; | |
155 } | |
156 | |
157 return false; | |
158 } | |
159 | |
160 } // namespace pepper | |
OLD | NEW |