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