OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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_file_chooser_host.h" | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "content/renderer/render_view_impl.h" | |
10 #include "ppapi/c/pp_errors.h" | |
11 #include "ppapi/host/dispatch_host_message.h" | |
12 #include "ppapi/host/host_message_context.h" | |
13 #include "ppapi/host/ppapi_host.h" | |
14 #include "ppapi/proxy/ppapi_messages.h" | |
15 #include "ppapi/proxy/ppb_file_ref_proxy.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h " | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserComplet ion.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserParams. h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | |
21 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | |
22 | |
23 namespace content { | |
24 | |
25 class PepperFileChooserHost::CompletionHandler | |
26 : public WebKit::WebFileChooserCompletion { | |
27 public: | |
28 CompletionHandler(const base::WeakPtr<PepperFileChooserHost>& host) | |
29 : host_(host) { | |
30 } | |
31 | |
32 virtual ~CompletionHandler() {} | |
33 | |
34 virtual void didChooseFile( | |
35 const WebKit::WebVector<WebKit::WebString>& file_names) { | |
36 if (host_) { | |
37 std::vector<PepperFileChooserHost::ChosenFileInfo> files; | |
38 for (size_t i = 0; i < file_names.size(); i++) { | |
39 files.push_back(PepperFileChooserHost::ChosenFileInfo( | |
40 file_names[i].utf8(), std::string())); | |
41 } | |
42 host_->StoreChosenFiles(files); | |
43 } | |
44 | |
45 // It is the responsibility of this method to delete the instance. | |
46 delete this; | |
47 } | |
48 virtual void didChooseFile( | |
49 const WebKit::WebVector<SelectedFileInfo>& file_names) { | |
50 if (host_) { | |
51 std::vector<PepperFileChooserHost::ChosenFileInfo> files; | |
52 for (size_t i = 0; i < file_names.size(); i++) { | |
53 files.push_back(PepperFileChooserHost::ChosenFileInfo( | |
54 file_names[i].path.utf8(), | |
55 file_names[i].displayName.utf8())); | |
56 } | |
57 host_->StoreChosenFiles(files); | |
58 } | |
59 | |
60 // It is the responsibility of this method to delete the instance. | |
61 delete this; | |
62 } | |
63 | |
64 private: | |
65 base::WeakPtr<PepperFileChooserHost> host_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(CompletionHandler); | |
68 }; | |
69 | |
70 PepperFileChooserHost::ChosenFileInfo::ChosenFileInfo( | |
71 const std::string& path, | |
72 const std::string& display_name) | |
73 : path(path), | |
74 display_name(display_name) { | |
75 } | |
76 | |
77 | |
78 PepperFileChooserHost::PepperFileChooserHost( | |
79 ppapi::host::PpapiHost* host, | |
80 PP_Instance instance, | |
81 PP_Resource resource, | |
82 RenderViewImpl* render_view) | |
83 : ResourceHost(host, instance, resource), | |
84 render_view_(render_view), | |
85 handler_(NULL) { | |
86 } | |
87 | |
88 PepperFileChooserHost::~PepperFileChooserHost() { | |
89 } | |
90 | |
91 int32_t PepperFileChooserHost::OnResourceMessageReceived( | |
92 const IPC::Message& msg, | |
93 ppapi::host::HostMessageContext* context) { | |
94 IPC_BEGIN_MESSAGE_MAP(PepperFileChooserHost, msg) | |
95 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileChooser_Show, | |
96 OnMsgShow) | |
97 IPC_END_MESSAGE_MAP() | |
98 return PP_ERROR_FAILED; | |
99 } | |
100 | |
101 void PepperFileChooserHost::StoreChosenFiles( | |
102 const std::vector<ChosenFileInfo>& files) { | |
103 std::vector<ppapi::PPB_FileRef_CreateInfo> chosen_files; | |
104 for (size_t i = 0; i < files.size(); i++) { | |
105 #if defined(OS_WIN) | |
106 FilePath file_path(UTF8ToWide(files[i].path)); | |
107 #else | |
108 FilePath file_path(files[i].path); | |
109 #endif | |
110 | |
111 webkit::ppapi::PPB_FileRef_Impl* ref = | |
112 webkit::ppapi::PPB_FileRef_Impl::CreateExternal( | |
113 pp_instance(), file_path, files[i].display_name); | |
114 ppapi::PPB_FileRef_CreateInfo create_info; | |
115 ppapi::proxy::PPB_FileRef_Proxy::SerializeFileRef(ref->GetReference(), | |
116 &create_info); | |
117 chosen_files.push_back(create_info); | |
118 } | |
119 | |
120 int32_t result_code = (chosen_files.size() > 0) ? PP_OK : PP_ERROR_USERCANCEL; | |
121 host()->SendReply(reply_params_, | |
122 PpapiPluginMsg_FileChooser_ShowReply(chosen_files)); | |
123 | |
124 reply_params_ = ppapi::proxy::ResourceMessageReplyParams(); | |
125 handler_ = NULL; // Handler deletes itself. | |
126 } | |
127 | |
128 int32_t PepperFileChooserHost::OnMsgShow( | |
129 ppapi::host::HostMessageContext* context, | |
130 bool save_as, | |
131 bool open_multiple, | |
132 const std::string& suggested_file_name, | |
133 const std::vector<std::string>& accept_mime_types) { | |
134 if (handler_) | |
135 return PP_ERROR_INPROGRESS; // Already pending. | |
136 | |
137 WebKit::WebFileChooserParams params; | |
138 if (save_as) { | |
139 params.saveAs = true; | |
140 params.initialValue = WebKit::WebString::fromUTF8( | |
141 suggested_file_name.data(), suggested_file_name.size()); | |
142 } else { | |
143 params.multiSelect = open_multiple; | |
144 } | |
145 std::vector<WebKit::WebString> web_accept(accept_mime_types.size()); | |
bbudge
2012/07/02 20:06:06
I like 'mime_types' or 'web_accept_mime_types' bet
| |
146 for (size_t i = 0; i < accept_mime_types.size(); i++) { | |
147 web_accept[0] = WebKit::WebString::fromUTF8( | |
bbudge
2012/07/02 20:06:06
s/0/i
brettw
2012/07/07 05:28:04
WHOOPS! Thanks for catching this!
| |
148 accept_mime_types[i].data(), accept_mime_types[i].size()); | |
149 } | |
150 params.acceptTypes = web_accept; | |
151 params.directory = false; | |
152 | |
153 handler_ = new CompletionHandler(AsWeakPtr()); | |
154 if (!render_view_->runFileChooser(params, handler_)) { | |
155 delete handler_; | |
156 handler_ = NULL; | |
157 return PP_ERROR_NOACCESS; | |
158 } | |
159 | |
160 reply_params_ = ppapi::proxy::ResourceMessageReplyParams( | |
161 context->params.pp_resource(), | |
162 context->params.sequence()); | |
163 return PP_OK_COMPLETIONPENDING; | |
164 } | |
165 | |
166 } // namespace content | |
OLD | NEW |