Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Side by Side Diff: content/renderer/pepper/pepper_file_chooser_host.cc

Issue 10544089: Implement the file chooser as a new resource "host" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/host_message_context.h"
12 #include "ppapi/host/ppapi_host.h"
13 #include "ppapi/proxy/ppapi_messages.h"
14 #include "ppapi/proxy/ppb_file_ref_proxy.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h "
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserComplet ion.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserParams. h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
20 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h"
21
22 namespace content {
23
24 class PepperFileChooserHost::CompletionHandler
25 : public WebKit::WebFileChooserCompletion {
26 public:
27 CompletionHandler(const base::WeakPtr<PepperFileChooserHost>& host)
28 : host_(host) {
29 }
30
31 virtual ~CompletionHandler() {}
32
33 virtual void didChooseFile(
34 const WebKit::WebVector<WebKit::WebString>& file_names) {
35 if (host_) {
36 std::vector<PepperFileChooserHost::ChosenFileInfo> files;
37 for (size_t i = 0; i < file_names.size(); i++) {
38 files.push_back(PepperFileChooserHost::ChosenFileInfo(
39 file_names[i].utf8(), std::string()));
40 }
41 host_->StoreChosenFiles(files);
42 }
43
44 // It is the responsibility of this method to delete the instance.
45 delete this;
46 }
47 virtual void didChooseFile(
48 const WebKit::WebVector<SelectedFileInfo>& file_names) {
49 if (host_) {
50 std::vector<PepperFileChooserHost::ChosenFileInfo> files;
51 for (size_t i = 0; i < file_names.size(); i++) {
52 files.push_back(PepperFileChooserHost::ChosenFileInfo(
53 file_names[i].path.utf8(),
54 file_names[i].displayName.utf8()));
55 }
56 host_->StoreChosenFiles(files);
57 }
58
59 // It is the responsibility of this method to delete the instance.
60 delete this;
61 }
62
63 private:
64 base::WeakPtr<PepperFileChooserHost> host_;
65
66 DISALLOW_COPY_AND_ASSIGN(CompletionHandler);
67 };
68
69 PepperFileChooserHost::ChosenFileInfo::ChosenFileInfo(
70 const std::string& path,
71 const std::string& display_name)
72 : path(path),
73 display_name(display_name) {
74 }
75
76
77 PepperFileChooserHost::PepperFileChooserHost(
78 ppapi::host::PpapiHost* host,
79 PP_Instance instance,
80 PP_Resource resource,
81 RenderViewImpl* render_view)
82 : ResourceHost(host, instance, resource),
83 render_view_(render_view),
84 handler_(NULL) {
85 }
86
87 PepperFileChooserHost::~PepperFileChooserHost() {
88 }
89
90 int32_t PepperFileChooserHost::OnResourceMessageReceived(
91 const IPC::Message& msg,
92 ppapi::host::HostMessageContext* context) {
93 if (msg.type() != PpapiHostMsg_FileChooser_Show::ID)
94 return PP_ERROR_FAILED; // Bad message type.
95
96 if (handler_)
97 return PP_ERROR_INPROGRESS; // Already pending.
98
99 bool save_as;
100 bool open_multiple;
101 std::string suggested_file_name;
102 std::vector<std::string> accept_mime_types;
103 if (!PpapiHostMsg_FileChooser_Show::Read(&msg, &save_as, &open_multiple,
104 &suggested_file_name,
105 &accept_mime_types))
106 return PP_ERROR_FAILED;
107 return OnMsgShow(context, save_as, open_multiple, suggested_file_name,
108 accept_mime_types);
109 }
110
111 void PepperFileChooserHost::StoreChosenFiles(
112 const std::vector<ChosenFileInfo>& files) {
113 std::vector<ppapi::PPB_FileRef_CreateInfo> chosen_files;
114 for (size_t i = 0; i < files.size(); i++) {
115 #if defined(OS_WIN)
116 FilePath file_path(UTF8ToWide(files[i].path));
117 #else
118 FilePath file_path(files[i].path);
119 #endif
120
121 webkit::ppapi::PPB_FileRef_Impl* ref =
122 webkit::ppapi::PPB_FileRef_Impl::CreateExternal(
123 pp_instance(), file_path, files[i].display_name);
124 ppapi::PPB_FileRef_CreateInfo create_info;
125 ppapi::proxy::PPB_FileRef_Proxy::SerializeFileRef(ref->GetReference(),
126 &create_info);
127 chosen_files.push_back(create_info);
128 }
129
130 int32_t result_code = (chosen_files.size() > 0) ? PP_OK : PP_ERROR_USERCANCEL;
131 host()->SendReply(reply_params_,
132 PpapiPluginMsg_FileChooser_ShowReply(chosen_files));
133
134 reply_params_ = ppapi::proxy::ResourceMessageReplyParams();
135 handler_ = NULL; // Handler deletes itself.
136 }
137
138 int32_t PepperFileChooserHost::OnMsgShow(
139 ppapi::host::HostMessageContext* context,
140 bool save_as,
141 bool open_multiple,
142 const std::string& suggested_file_name,
143 const std::vector<std::string>& accept_mime_types) {
144 WebKit::WebFileChooserParams params;
145 if (save_as) {
146 params.saveAs = true;
147 params.initialValue = WebKit::WebString::fromUTF8(
148 suggested_file_name.data(), suggested_file_name.size());
149 } else {
150 params.multiSelect = open_multiple;
151 }
152 std::vector<WebKit::WebString> web_accept(accept_mime_types.size());
153 for (size_t i = 0; i < accept_mime_types.size(); i++) {
154 web_accept[0] = WebKit::WebString::fromUTF8(
155 accept_mime_types[i].data(), accept_mime_types[i].size());
156 }
157 params.acceptTypes = web_accept;
158 params.directory = false;
159
160 handler_ = new CompletionHandler(AsWeakPtr());
161 if (!render_view_->runFileChooser(params, handler_)) {
162 delete handler_;
163 handler_ = NULL;
164 return PP_ERROR_NOACCESS;
165 }
166
167 reply_params_ = ppapi::proxy::ResourceMessageReplyParams(
168 context->params.pp_resource(),
169 context->params.sequence());
170 return PP_OK_COMPLETIONPENDING;
171 }
172
173 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/pepper/pepper_file_chooser_host.h ('k') | content/renderer/pepper/pepper_file_chooser_host_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698