OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 "webkit/glue/plugins/pepper_file_chooser.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/logging.h" | |
11 #include "ppapi/c/pp_completion_callback.h" | |
12 #include "ppapi/c/pp_errors.h" | |
13 #include "third_party/WebKit/WebKit/chromium/public/WebCString.h" | |
14 #include "third_party/WebKit/WebKit/chromium/public/WebFileChooserCompletion.h" | |
15 #include "third_party/WebKit/WebKit/chromium/public/WebFileChooserParams.h" | |
16 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" | |
17 #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" | |
18 #include "webkit/glue/plugins/pepper_common.h" | |
19 #include "webkit/glue/plugins/pepper_file_ref.h" | |
20 #include "webkit/glue/plugins/pepper_plugin_delegate.h" | |
21 #include "webkit/glue/plugins/pepper_plugin_instance.h" | |
22 #include "webkit/glue/plugins/pepper_resource_tracker.h" | |
23 #include "webkit/glue/webkit_glue.h" | |
24 | |
25 using WebKit::WebCString; | |
26 using WebKit::WebFileChooserCompletion; | |
27 using WebKit::WebFileChooserParams; | |
28 using WebKit::WebString; | |
29 using WebKit::WebVector; | |
30 | |
31 namespace pepper { | |
32 | |
33 namespace { | |
34 | |
35 PP_Resource Create(PP_Instance instance_id, | |
36 const PP_FileChooserOptions_Dev* options) { | |
37 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | |
38 if (!instance) | |
39 return 0; | |
40 | |
41 if ((options->mode != PP_FILECHOOSERMODE_OPEN) && | |
42 (options->mode != PP_FILECHOOSERMODE_OPENMULTIPLE)) | |
43 return 0; | |
44 | |
45 FileChooser* chooser = new FileChooser(instance, options); | |
46 return chooser->GetReference(); | |
47 } | |
48 | |
49 PP_Bool IsFileChooser(PP_Resource resource) { | |
50 return BoolToPPBool(!!Resource::GetAs<FileChooser>(resource)); | |
51 } | |
52 | |
53 int32_t Show(PP_Resource chooser_id, PP_CompletionCallback callback) { | |
54 scoped_refptr<FileChooser> chooser( | |
55 Resource::GetAs<FileChooser>(chooser_id)); | |
56 if (!chooser) | |
57 return PP_ERROR_BADRESOURCE; | |
58 | |
59 return chooser->Show(callback); | |
60 } | |
61 | |
62 PP_Resource GetNextChosenFile(PP_Resource chooser_id) { | |
63 scoped_refptr<FileChooser> chooser( | |
64 Resource::GetAs<FileChooser>(chooser_id)); | |
65 if (!chooser) | |
66 return 0; | |
67 | |
68 scoped_refptr<FileRef> file_ref(chooser->GetNextChosenFile()); | |
69 if (!file_ref) | |
70 return 0; | |
71 | |
72 return file_ref->GetReference(); | |
73 } | |
74 | |
75 const PPB_FileChooser_Dev ppb_filechooser = { | |
76 &Create, | |
77 &IsFileChooser, | |
78 &Show, | |
79 &GetNextChosenFile | |
80 }; | |
81 | |
82 class FileChooserCompletionImpl : public WebFileChooserCompletion { | |
83 public: | |
84 FileChooserCompletionImpl(pepper::FileChooser* file_chooser) | |
85 : file_chooser_(file_chooser) { | |
86 DCHECK(file_chooser_); | |
87 } | |
88 | |
89 virtual ~FileChooserCompletionImpl() {} | |
90 | |
91 virtual void didChooseFile(const WebVector<WebString>& file_names) { | |
92 std::vector<std::string> files; | |
93 for (size_t i = 0; i < file_names.size(); i++) | |
94 files.push_back(file_names[i].utf8().data()); | |
95 | |
96 file_chooser_->StoreChosenFiles(files); | |
97 } | |
98 | |
99 private: | |
100 FileChooser* file_chooser_; | |
101 }; | |
102 | |
103 } // namespace | |
104 | |
105 FileChooser::FileChooser(PluginInstance* instance, | |
106 const PP_FileChooserOptions_Dev* options) | |
107 : Resource(instance->module()), | |
108 delegate_(instance->delegate()), | |
109 mode_(options->mode), | |
110 accept_mime_types_(options->accept_mime_types), | |
111 completion_callback_() { | |
112 } | |
113 | |
114 FileChooser::~FileChooser() { | |
115 } | |
116 | |
117 // static | |
118 const PPB_FileChooser_Dev* FileChooser::GetInterface() { | |
119 return &ppb_filechooser; | |
120 } | |
121 | |
122 FileChooser* FileChooser::AsFileChooser() { | |
123 return this; | |
124 } | |
125 | |
126 void FileChooser::StoreChosenFiles(const std::vector<std::string>& files) { | |
127 next_chosen_file_index_ = 0; | |
128 std::vector<std::string>::const_iterator end_it = files.end(); | |
129 for (std::vector<std::string>::const_iterator it = files.begin(); | |
130 it != end_it; it++) { | |
131 chosen_files_.push_back(make_scoped_refptr( | |
132 new FileRef(module(), FilePath().AppendASCII(*it)))); | |
133 } | |
134 | |
135 if (!completion_callback_.func) | |
136 return; | |
137 | |
138 PP_CompletionCallback callback = {0}; | |
139 std::swap(callback, completion_callback_); | |
140 PP_RunCompletionCallback(&callback, 0); | |
141 } | |
142 | |
143 int32_t FileChooser::Show(PP_CompletionCallback callback) { | |
144 DCHECK((mode_ == PP_FILECHOOSERMODE_OPEN) || | |
145 (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE)); | |
146 DCHECK(!completion_callback_.func); | |
147 completion_callback_ = callback; | |
148 | |
149 WebFileChooserParams params; | |
150 params.multiSelect = (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE); | |
151 params.acceptTypes = WebString::fromUTF8(accept_mime_types_); | |
152 params.directory = false; | |
153 | |
154 return delegate_->RunFileChooser( | |
155 params, new FileChooserCompletionImpl(this)); | |
156 } | |
157 | |
158 scoped_refptr<FileRef> FileChooser::GetNextChosenFile() { | |
159 if (next_chosen_file_index_ >= chosen_files_.size()) | |
160 return NULL; | |
161 | |
162 return chosen_files_[next_chosen_file_index_++]; | |
163 } | |
164 | |
165 } // namespace pepper | |
OLD | NEW |