Disable ContentSettingBubbleModelTest.RPHAllow which is flaky.
[chromium-blink-merge.git] / content / renderer / pepper / pepper_file_chooser_host.cc
blob29ff7372dff0b83e8da28cc466a57db44bbed279
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.
5 #include "content/renderer/pepper/pepper_file_chooser_host.h"
7 #include "base/files/file_path.h"
8 #include "base/utf_string_conversions.h"
9 #include "content/public/renderer/renderer_ppapi_host.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/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/Platform/chromium/public/WebCString.h"
17 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
18 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserCompletion.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserParams.h"
21 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h"
23 namespace content {
25 class PepperFileChooserHost::CompletionHandler
26 : public WebKit::WebFileChooserCompletion {
27 public:
28 CompletionHandler(const base::WeakPtr<PepperFileChooserHost>& host)
29 : host_(host) {
32 virtual ~CompletionHandler() {}
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()));
42 host_->StoreChosenFiles(files);
45 // It is the responsibility of this method to delete the instance.
46 delete this;
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()));
57 host_->StoreChosenFiles(files);
60 // It is the responsibility of this method to delete the instance.
61 delete this;
64 private:
65 base::WeakPtr<PepperFileChooserHost> host_;
67 DISALLOW_COPY_AND_ASSIGN(CompletionHandler);
70 PepperFileChooserHost::ChosenFileInfo::ChosenFileInfo(
71 const std::string& path,
72 const std::string& display_name)
73 : path(path),
74 display_name(display_name) {
78 PepperFileChooserHost::PepperFileChooserHost(
79 RendererPpapiHost* host,
80 PP_Instance instance,
81 PP_Resource resource)
82 : ResourceHost(host->GetPpapiHost(), instance, resource),
83 renderer_ppapi_host_(host),
84 handler_(NULL) {
87 PepperFileChooserHost::~PepperFileChooserHost() {
90 int32_t PepperFileChooserHost::OnResourceMessageReceived(
91 const IPC::Message& msg,
92 ppapi::host::HostMessageContext* context) {
93 IPC_BEGIN_MESSAGE_MAP(PepperFileChooserHost, msg)
94 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileChooser_Show, OnShow)
95 IPC_END_MESSAGE_MAP()
96 return PP_ERROR_FAILED;
99 void PepperFileChooserHost::StoreChosenFiles(
100 const std::vector<ChosenFileInfo>& files) {
101 std::vector<ppapi::PPB_FileRef_CreateInfo> chosen_files;
102 for (size_t i = 0; i < files.size(); i++) {
103 #if defined(OS_WIN)
104 base::FilePath file_path(UTF8ToWide(files[i].path));
105 #else
106 base::FilePath file_path(files[i].path);
107 #endif
109 webkit::ppapi::PPB_FileRef_Impl* ref =
110 webkit::ppapi::PPB_FileRef_Impl::CreateExternal(
111 pp_instance(), file_path, files[i].display_name);
112 ppapi::PPB_FileRef_CreateInfo create_info;
113 ppapi::proxy::PPB_FileRef_Proxy::SerializeFileRef(ref->GetReference(),
114 &create_info);
115 chosen_files.push_back(create_info);
118 reply_context_.params.set_result(
119 (chosen_files.size() > 0) ? PP_OK : PP_ERROR_USERCANCEL);
120 host()->SendReply(reply_context_,
121 PpapiPluginMsg_FileChooser_ShowReply(chosen_files));
123 reply_context_ = ppapi::host::ReplyMessageContext();
124 handler_ = NULL; // Handler deletes itself.
127 int32_t PepperFileChooserHost::OnShow(
128 ppapi::host::HostMessageContext* context,
129 bool save_as,
130 bool open_multiple,
131 const std::string& suggested_file_name,
132 const std::vector<std::string>& accept_mime_types) {
133 if (handler_)
134 return PP_ERROR_INPROGRESS; // Already pending.
136 if (!host()->permissions().HasPermission(
137 ppapi::PERMISSION_BYPASS_USER_GESTURE) &&
138 !renderer_ppapi_host_->HasUserGesture(pp_instance())) {
139 return PP_ERROR_NO_USER_GESTURE;
142 WebKit::WebFileChooserParams params;
143 if (save_as) {
144 params.saveAs = true;
145 params.initialValue = WebKit::WebString::fromUTF8(
146 suggested_file_name.data(), suggested_file_name.size());
147 } else {
148 params.multiSelect = open_multiple;
150 std::vector<WebKit::WebString> mine_types(accept_mime_types.size());
151 for (size_t i = 0; i < accept_mime_types.size(); i++) {
152 mine_types[i] = WebKit::WebString::fromUTF8(
153 accept_mime_types[i].data(), accept_mime_types[i].size());
155 params.acceptTypes = mine_types;
156 params.directory = false;
158 handler_ = new CompletionHandler(AsWeakPtr());
159 RenderViewImpl* render_view = static_cast<RenderViewImpl*>(
160 renderer_ppapi_host_->GetRenderViewForInstance(pp_instance()));
161 if (!render_view || !render_view->runFileChooser(params, handler_)) {
162 delete handler_;
163 handler_ = NULL;
164 return PP_ERROR_NOACCESS;
167 reply_context_ = context->MakeReplyMessageContext();
168 return PP_OK_COMPLETIONPENDING;
171 } // namespace content