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 "ppapi/proxy/file_chooser_resource.h" | |
6 | |
7 #include "base/string_split.h" | |
8 #include "ipc/ipc_message.h" | |
9 #include "ppapi/c/pp_errors.h" | |
10 #include "ppapi/proxy/ppapi_messages.h" | |
11 #include "ppapi/proxy/ppb_file_ref_proxy.h" | |
12 #include "ppapi/shared_impl/var.h" | |
13 | |
14 namespace ppapi { | |
15 namespace proxy { | |
16 | |
17 FileChooserResource::FileChooserResource(IPC::Sender* sender, | |
18 PP_Instance instance, | |
19 PP_FileChooserMode_Dev mode, | |
20 const std::string& accept_types) | |
21 : PluginResource(sender, instance), | |
22 mode_(mode) { | |
23 PopulateAcceptTypes(accept_types, &accept_types_); | |
24 } | |
25 | |
26 FileChooserResource::~FileChooserResource() { | |
27 } | |
28 | |
29 thunk::PPB_FileChooser_API* FileChooserResource::AsPPB_FileChooser_API() { | |
30 return this; | |
31 } | |
32 | |
33 int32_t FileChooserResource::Show(const PP_ArrayOutput& output, | |
34 scoped_refptr<TrackedCallback> callback) { | |
bbudge
2012/07/02 20:06:06
Shouldn't we check that we have a user gesture her
brettw
2012/07/07 05:28:04
This was addressed on the host side (this code is
| |
35 return ShowWithoutUserGesture(PP_FALSE, PP_MakeUndefined(), output, callback); | |
36 } | |
37 | |
38 int32_t FileChooserResource::ShowWithoutUserGesture( | |
39 PP_Bool save_as, | |
40 PP_Var suggested_file_name, | |
41 const PP_ArrayOutput& output, | |
42 scoped_refptr<TrackedCallback> callback) { | |
43 int32_t result = ShowInternal(PP_FALSE, suggested_file_name, callback); | |
44 if (result == PP_OK_COMPLETIONPENDING) | |
45 output_.set_pp_array_output(output); | |
46 return result; | |
47 } | |
48 | |
49 int32_t FileChooserResource::Show0_5(scoped_refptr<TrackedCallback> callback) { | |
bbudge
2012/07/02 20:06:06
User gesture?
| |
50 return ShowInternal(PP_FALSE, PP_MakeUndefined(), callback); | |
51 } | |
52 | |
53 PP_Resource FileChooserResource::GetNextChosenFile() { | |
54 if (file_queue_.empty()) | |
55 return 0; | |
56 | |
57 // Return the next resource in the queue. These resource have already been | |
bbudge
2012/07/02 20:06:06
s/resource/resources
| |
58 // addrefed (they're currently owned by the FileChooser) and returning them | |
59 // transfers ownership of that reference to the plugin. | |
60 PP_Resource next = file_queue_.front(); | |
61 file_queue_.pop(); | |
62 return next; | |
63 } | |
64 | |
65 int32_t FileChooserResource::ShowWithoutUserGesture0_5( | |
66 PP_Bool save_as, | |
67 PP_Var suggested_file_name, | |
68 scoped_refptr<TrackedCallback> callback) { | |
69 return ShowInternal(save_as, suggested_file_name, callback); | |
70 } | |
71 | |
72 // static | |
73 void FileChooserResource::PopulateAcceptTypes( | |
74 const std::string& input, | |
75 std::vector<std::string>* output) { | |
76 if (input.empty()) | |
77 return; | |
bbudge
2012/07/02 20:06:06
Check that output is non-NULL?
brettw
2012/07/07 05:28:04
For simple out params I don't usually encourage th
| |
78 | |
79 std::vector<std::string> type_list; | |
80 base::SplitString(input, ',', &type_list); | |
81 output->reserve(type_list.size()); | |
82 | |
83 for (size_t i = 0; i < type_list.size(); ++i) { | |
84 std::string type = type_list[i]; | |
85 TrimWhitespaceASCII(type, TRIM_ALL, &type); | |
86 | |
87 // If the type is a single character, it definitely cannot be valid. In the | |
88 // case of a file extension it would be a single ".". In the case of a MIME | |
89 // type it would just be a "/". | |
90 if (type.length() < 2) | |
91 continue; | |
92 if (type.find_first_of('/') == std::string::npos && type[0] != '.') | |
93 continue; | |
94 StringToLowerASCII(&type); | |
95 output->push_back(type); | |
96 } | |
97 } | |
98 | |
99 void FileChooserResource::OnReplyReceived(int /* sequence */, | |
100 int32_t result, | |
101 const IPC::Message& msg) { | |
102 PpapiPluginMsg_FileChooser_ShowReply::Schema::Param param; | |
103 PpapiPluginMsg_FileChooser_ShowReply::Read(&msg, ¶m); | |
104 const std::vector<ppapi::PPB_FileRef_CreateInfo>& chosen_files = param.a; | |
105 | |
106 if (output_.is_valid()) { | |
107 // Using v0.6 of the API with the output array. | |
108 std::vector<PP_Resource> files; | |
109 for (size_t i = 0; i < chosen_files.size(); i++) | |
110 files.push_back(PPB_FileRef_Proxy::DeserializeFileRef(chosen_files[i])); | |
111 output_.StoreResourceVector(files); | |
112 } else { | |
113 // Convert each of the passed in file infos to resources. These will be | |
114 // owned by the FileChooser object until they're passed to the plugin. | |
115 DCHECK(file_queue_.empty()); | |
116 for (size_t i = 0; i < chosen_files.size(); i++) { | |
117 file_queue_.push(PPB_FileRef_Proxy::DeserializeFileRef( | |
118 chosen_files[i])); | |
119 } | |
120 } | |
121 | |
122 // Notify the plugin of the new data. | |
123 TrackedCallback::ClearAndRun(&callback_, PP_OK); | |
124 // DANGER: May delete |this|! | |
125 } | |
126 | |
127 int32_t FileChooserResource::ShowInternal( | |
128 PP_Bool save_as, | |
129 const PP_Var& suggested_file_name, | |
130 scoped_refptr<TrackedCallback> callback) { | |
131 if (TrackedCallback::IsPending(callback_)) | |
132 return PP_ERROR_INPROGRESS; | |
133 | |
134 if (!sent_create_to_renderer()) | |
135 SendCreateToRenderer(PpapiHostMsg_FileChooser_Create()); | |
136 | |
137 callback_ = callback; | |
138 StringVar* sugg_str = StringVar::FromPPVar(suggested_file_name); | |
139 | |
140 CallRenderer(PpapiHostMsg_FileChooser_Show( | |
141 PP_ToBool(save_as), | |
142 mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE, | |
143 sugg_str ? sugg_str->value() : std::string(), | |
144 accept_types_)); | |
145 return PP_OK_COMPLETIONPENDING; | |
146 } | |
147 | |
148 } // namespace proxy | |
149 } // namespace ppapi | |
OLD | NEW |