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