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