OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "content/renderer/pepper/pepper_directory_reader_host.h" | |
6 | |
7 #include "content/public/renderer/renderer_ppapi_host.h" | |
8 #include "ppapi/c/pp_completion_callback.h" | |
9 #include "ppapi/c/pp_errors.h" | |
10 #include "ppapi/host/dispatch_host_message.h" | |
11 #include "ppapi/host/ppapi_host.h" | |
12 #include "ppapi/proxy/enter_proxy.h" | |
13 #include "ppapi/proxy/ppapi_messages.h" | |
14 #include "ppapi/proxy/ppb_file_ref_proxy.h" | |
15 #include "ppapi/shared_impl/file_type_conversion.h" | |
16 #include "ppapi/shared_impl/ppb_file_ref_shared.h" | |
17 #include "webkit/plugins/ppapi/file_callbacks.h" | |
18 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
19 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | |
20 #include "webkit/plugins/ppapi/ppb_file_system_impl.h" | |
21 #include "webkit/plugins/ppapi/resource_helper.h" | |
22 | |
23 using ppapi::proxy::PPB_FileRef_Proxy; | |
24 using ppapi::thunk::EnterResource; | |
25 using ppapi::thunk::EnterResourceCreation; | |
26 using ppapi::thunk::PPB_DirectoryReader_API; | |
27 using ppapi::thunk::PPB_FileRef_API; | |
28 using webkit::ppapi::PPB_FileRef_Impl; | |
29 | |
30 namespace content { | |
31 | |
32 namespace { | |
33 | |
34 std::string FilePathStringToUTF8String(const FilePath::StringType& str) { | |
35 #if defined(OS_WIN) | |
36 return WideToUTF8(str); | |
37 #elif defined(OS_POSIX) | |
38 return str; | |
39 #else | |
40 #error "Unsupported platform." | |
41 #endif | |
42 } | |
43 | |
44 FilePath::StringType UTF8StringToFilePathString(const std::string& str) { | |
45 #if defined(OS_WIN) | |
46 return UTF8ToWide(str); | |
47 #elif defined(OS_POSIX) | |
48 return str; | |
49 #else | |
50 #error "Unsupported platform." | |
51 #endif | |
52 } | |
53 | |
54 class ReadDirectoryCallback : public fileapi::FileSystemCallbackDispatcher { | |
55 public: | |
56 ReadDirectoryCallback( | |
57 scoped_refptr<ppapi::TrackedCallback> callback, | |
58 const base::WeakPtr<content::PepperDirectoryReaderHost>& host) | |
59 : callback_(callback), | |
60 host_(host) {} | |
dmichael (off chromium)
2013/01/29 21:22:42
^ nit: line up host_ with callback_
nhiroki
2013/01/30 09:24:51
Removed host_.
| |
61 virtual ~ReadDirectoryCallback() {} | |
62 | |
63 virtual void DidSucceed() OVERRIDE { | |
64 if (callback_->completed()) | |
65 return; | |
66 callback_->Run(PP_OK); | |
67 } | |
68 | |
69 virtual void DidReadMetadata(const base::PlatformFileInfo& file_info, | |
70 const FilePath& platform_path) OVERRIDE { | |
71 NOTREACHED(); | |
72 } | |
73 | |
74 virtual void DidReadDirectory( | |
75 const std::vector<base::FileUtilProxy::Entry>& entries, | |
76 bool has_more) OVERRIDE { | |
77 if (callback_->completed()) | |
78 return; | |
79 if (host_ && host_->AddNewEntries(entries, has_more)) | |
80 callback_->Run(PP_OK); | |
81 else | |
82 callback_->Run(PP_ERROR_FAILED); | |
83 } | |
84 | |
85 virtual void DidOpenFileSystem(const std::string& name, | |
86 const GURL& root) OVERRIDE { | |
87 NOTREACHED(); | |
88 } | |
89 | |
90 virtual void DidFail(base::PlatformFileError error) OVERRIDE { | |
91 callback_->Run(ppapi::PlatformFileErrorToPepperError(error)); | |
92 } | |
93 | |
94 virtual void DidWrite(int64 bytes, bool complete) OVERRIDE { | |
95 NOTREACHED(); | |
96 } | |
97 | |
98 virtual void DidOpenFile(base::PlatformFile file) OVERRIDE { | |
99 NOTREACHED(); | |
100 } | |
101 | |
102 private: | |
103 scoped_refptr<ppapi::TrackedCallback> callback_; | |
104 base::WeakPtr<content::PepperDirectoryReaderHost> host_; | |
105 }; | |
106 | |
107 } // namespace | |
108 | |
109 PepperDirectoryReaderHost::PepperDirectoryReaderHost( | |
110 RendererPpapiHost* host, | |
111 PP_Instance instance, | |
112 PP_Resource resource) | |
113 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
114 renderer_ppapi_host_(host), | |
115 directory_ref_(0), | |
116 has_more_(true) { | |
117 } | |
118 | |
119 PepperDirectoryReaderHost::~PepperDirectoryReaderHost() { | |
120 } | |
121 | |
122 int32_t PepperDirectoryReaderHost::OnResourceMessageReceived( | |
123 const IPC::Message& msg, | |
124 ppapi::host::HostMessageContext* context) { | |
125 IPC_BEGIN_MESSAGE_MAP(PepperDirectoryReaderHost, msg) | |
126 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
127 PpapiHostMsg_DirectoryReader_GetEntries, OnGetEntries) | |
128 IPC_END_MESSAGE_MAP() | |
129 return PP_ERROR_FAILED; | |
130 } | |
131 | |
132 bool PepperDirectoryReaderHost::AddNewEntries( | |
133 const std::vector<base::FileUtilProxy::Entry>& entries, | |
134 bool has_more) { | |
135 DCHECK(!entries.empty() || !has_more); | |
136 has_more_ = has_more; | |
137 | |
138 std::string dir_path = directory_ref_->GetCreateInfo().path; | |
139 if (dir_path[dir_path.size() - 1] != '/') | |
140 dir_path += '/'; | |
141 FilePath::StringType dir_file_path = UTF8StringToFilePathString(dir_path); | |
142 DCHECK(!entries.empty()); | |
143 | |
144 for (std::vector<base::FileUtilProxy::Entry>::const_iterator it = | |
145 entries.begin(); it != entries.end(); ++it) { | |
dmichael (off chromium)
2013/01/29 21:22:42
nit: entries.begin() should be indented. I'd sugge
nhiroki
2013/01/30 09:24:51
Done.
| |
146 PPB_FileRef_Impl* file_ref = PPB_FileRef_Impl::CreateInternal( | |
dmichael (off chromium)
2013/01/29 21:22:42
You should usually wrap this immediately in a scop
nhiroki
2013/01/30 09:24:51
Done.
| |
147 directory_ref_->file_system()->pp_resource(), | |
148 FilePathStringToUTF8String(dir_file_path + it->name)); | |
149 | |
150 if (!file_ref) { | |
151 host_resources_.clear(); | |
152 file_types_.clear(); | |
153 return false; | |
154 } | |
155 | |
156 ppapi::PPB_FileRef_CreateInfo info; | |
157 PPB_FileRef_Proxy::SerializeFileRef(file_ref->GetReference(), &info); | |
dmichael (off chromium)
2013/01/29 21:22:42
We generally shouldn't be using stuff from ppapi/p
nhiroki
2013/01/30 09:24:51
Done.
| |
158 host_resources_.push_back(info); | |
159 file_types_.push_back(it->is_directory ? | |
160 PP_FILETYPE_DIRECTORY : PP_FILETYPE_REGULAR); | |
161 } | |
162 | |
163 return true; | |
164 } | |
165 | |
166 // static | |
167 void PepperDirectoryReaderHost::OnGetEntriesCallback( | |
168 void* data, | |
169 int32_t result) { | |
170 PepperDirectoryReaderHost* self = | |
171 static_cast<PepperDirectoryReaderHost*>(data); | |
172 self->SendGetEntriesReply(result); | |
173 } | |
174 | |
175 int32_t PepperDirectoryReaderHost::OnGetEntries( | |
176 ppapi::host::HostMessageContext* host_context, | |
177 ppapi::HostResource resource) { | |
178 reply_context_ = host_context->MakeReplyMessageContext(); | |
179 | |
180 EnterResource<PPB_FileRef_API> enter(resource.host_resource(), true); | |
181 if (enter.failed()) | |
182 return PP_ERROR_FAILED; | |
183 directory_ref_ = static_cast<PPB_FileRef_Impl*>(enter.object()); | |
184 | |
185 if (directory_ref_->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | |
186 return PP_ERROR_FAILED; | |
187 | |
188 webkit::ppapi::PluginInstance* plugin_instance = | |
189 renderer_ppapi_host_->GetPluginInstance(pp_instance()); | |
190 if (!plugin_instance) | |
191 return PP_ERROR_FAILED; | |
192 | |
193 ppapi::TrackedCallback* callback = new ppapi::TrackedCallback( | |
194 ppapi::PpapiGlobals::Get()->GetResourceTracker()->GetResource( | |
195 resource.host_resource()), | |
196 PP_MakeCompletionCallback( | |
197 &PepperDirectoryReaderHost::OnGetEntriesCallback, | |
198 AsWeakPtr())); | |
dmichael (off chromium)
2013/01/29 21:22:42
Why are you using completion callbacks instead of
nhiroki
2013/01/30 09:24:51
Done.
| |
199 | |
200 if (!plugin_instance->delegate()->ReadDirectory( | |
201 directory_ref_->GetFileSystemURL(), | |
202 new ReadDirectoryCallback(callback, AsWeakPtr()))) | |
203 return PP_ERROR_FAILED; | |
204 return PP_OK_COMPLETIONPENDING; | |
205 } | |
206 | |
207 void PepperDirectoryReaderHost::SendGetEntriesReply(int32_t result) { | |
208 reply_context_.params.set_result(result); | |
209 host()->SendReply( | |
210 reply_context_, | |
211 PpapiPluginMsg_DirectoryReader_GetEntriesReply(host_resources_, | |
212 file_types_, | |
213 has_more_)); | |
214 host_resources_.clear(); | |
215 file_types_.clear(); | |
216 } | |
217 | |
218 } // namespace content | |
OLD | NEW |