OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/plugins/ppapi/ppb_file_chooser_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_file_chooser_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
12 #include "ppapi/c/pp_completion_callback.h" | 12 #include "ppapi/c/pp_completion_callback.h" |
13 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCString.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCString.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserComplet
ion.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserComplet
ion.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserParams.
h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserParams.
h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" |
19 #include "webkit/plugins/ppapi/callbacks.h" | 19 #include "webkit/plugins/ppapi/callbacks.h" |
20 #include "webkit/plugins/ppapi/common.h" | 20 #include "webkit/plugins/ppapi/common.h" |
21 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | 21 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
22 #include "webkit/plugins/ppapi/plugin_delegate.h" | 22 #include "webkit/plugins/ppapi/plugin_delegate.h" |
23 #include "webkit/plugins/ppapi/plugin_module.h" | 23 #include "webkit/plugins/ppapi/plugin_module.h" |
24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
25 #include "webkit/plugins/ppapi/resource_tracker.h" | 25 #include "webkit/plugins/ppapi/resource_tracker.h" |
26 #include "webkit/glue/webkit_glue.h" | 26 #include "webkit/glue/webkit_glue.h" |
27 | 27 |
| 28 using ppapi::thunk::PPB_FileChooser_API; |
28 using WebKit::WebCString; | 29 using WebKit::WebCString; |
29 using WebKit::WebFileChooserCompletion; | 30 using WebKit::WebFileChooserCompletion; |
30 using WebKit::WebFileChooserParams; | 31 using WebKit::WebFileChooserParams; |
31 using WebKit::WebString; | 32 using WebKit::WebString; |
32 using WebKit::WebVector; | 33 using WebKit::WebVector; |
33 | 34 |
34 namespace webkit { | 35 namespace webkit { |
35 namespace ppapi { | 36 namespace ppapi { |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
39 PP_Resource Create(PP_Instance instance_id, | |
40 const PP_FileChooserOptions_Dev* options) { | |
41 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | |
42 if (!instance) | |
43 return 0; | |
44 | |
45 if ((options->mode != PP_FILECHOOSERMODE_OPEN) && | |
46 (options->mode != PP_FILECHOOSERMODE_OPENMULTIPLE)) | |
47 return 0; | |
48 | |
49 PPB_FileChooser_Impl* chooser = new PPB_FileChooser_Impl(instance, options); | |
50 return chooser->GetReference(); | |
51 } | |
52 | |
53 PP_Bool IsFileChooser(PP_Resource resource) { | |
54 return BoolToPPBool(!!Resource::GetAs<PPB_FileChooser_Impl>(resource)); | |
55 } | |
56 | |
57 int32_t Show(PP_Resource chooser_id, PP_CompletionCallback callback) { | |
58 scoped_refptr<PPB_FileChooser_Impl> chooser( | |
59 Resource::GetAs<PPB_FileChooser_Impl>(chooser_id)); | |
60 if (!chooser) | |
61 return PP_ERROR_BADRESOURCE; | |
62 | |
63 return chooser->Show(callback); | |
64 } | |
65 | |
66 PP_Resource GetNextChosenFile(PP_Resource chooser_id) { | |
67 scoped_refptr<PPB_FileChooser_Impl> chooser( | |
68 Resource::GetAs<PPB_FileChooser_Impl>(chooser_id)); | |
69 if (!chooser) | |
70 return 0; | |
71 | |
72 scoped_refptr<PPB_FileRef_Impl> file_ref(chooser->GetNextChosenFile()); | |
73 if (!file_ref) | |
74 return 0; | |
75 | |
76 return file_ref->GetReference(); | |
77 } | |
78 | |
79 const PPB_FileChooser_Dev ppb_filechooser = { | |
80 &Create, | |
81 &IsFileChooser, | |
82 &Show, | |
83 &GetNextChosenFile | |
84 }; | |
85 | |
86 class FileChooserCompletionImpl : public WebFileChooserCompletion { | 40 class FileChooserCompletionImpl : public WebFileChooserCompletion { |
87 public: | 41 public: |
88 FileChooserCompletionImpl(PPB_FileChooser_Impl* file_chooser) | 42 FileChooserCompletionImpl(PPB_FileChooser_Impl* file_chooser) |
89 : file_chooser_(file_chooser) { | 43 : file_chooser_(file_chooser) { |
90 DCHECK(file_chooser_); | 44 DCHECK(file_chooser_); |
91 } | 45 } |
92 | 46 |
93 virtual ~FileChooserCompletionImpl() {} | 47 virtual ~FileChooserCompletionImpl() {} |
94 | 48 |
95 virtual void didChooseFile(const WebVector<WebString>& file_names) { | 49 virtual void didChooseFile(const WebVector<WebString>& file_names) { |
(...skipping 17 matching lines...) Expand all Loading... |
113 mode_(options->mode), | 67 mode_(options->mode), |
114 accept_mime_types_(options->accept_mime_types ? | 68 accept_mime_types_(options->accept_mime_types ? |
115 options->accept_mime_types : ""), | 69 options->accept_mime_types : ""), |
116 next_chosen_file_index_(0) { | 70 next_chosen_file_index_(0) { |
117 } | 71 } |
118 | 72 |
119 PPB_FileChooser_Impl::~PPB_FileChooser_Impl() { | 73 PPB_FileChooser_Impl::~PPB_FileChooser_Impl() { |
120 } | 74 } |
121 | 75 |
122 // static | 76 // static |
123 const PPB_FileChooser_Dev* PPB_FileChooser_Impl::GetInterface() { | 77 PP_Resource PPB_FileChooser_Impl::Create( |
124 return &ppb_filechooser; | 78 PP_Instance pp_instance, |
| 79 const PP_FileChooserOptions_Dev* options) { |
| 80 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); |
| 81 if (!instance) |
| 82 return 0; |
| 83 |
| 84 if ((options->mode != PP_FILECHOOSERMODE_OPEN) && |
| 85 (options->mode != PP_FILECHOOSERMODE_OPENMULTIPLE)) |
| 86 return 0; |
| 87 |
| 88 PPB_FileChooser_Impl* chooser = new PPB_FileChooser_Impl(instance, options); |
| 89 return chooser->GetReference(); |
125 } | 90 } |
126 | 91 |
127 PPB_FileChooser_Impl* PPB_FileChooser_Impl::AsPPB_FileChooser_Impl() { | 92 PPB_FileChooser_Impl* PPB_FileChooser_Impl::AsPPB_FileChooser_Impl() { |
128 return this; | 93 return this; |
129 } | 94 } |
130 | 95 |
| 96 PPB_FileChooser_API* PPB_FileChooser_Impl::AsPPB_FileChooser_API() { |
| 97 return this; |
| 98 } |
| 99 |
131 void PPB_FileChooser_Impl::StoreChosenFiles( | 100 void PPB_FileChooser_Impl::StoreChosenFiles( |
132 const std::vector<std::string>& files) { | 101 const std::vector<std::string>& files) { |
133 chosen_files_.clear(); | 102 chosen_files_.clear(); |
134 next_chosen_file_index_ = 0; | 103 next_chosen_file_index_ = 0; |
135 for (std::vector<std::string>::const_iterator it = files.begin(); | 104 for (std::vector<std::string>::const_iterator it = files.begin(); |
136 it != files.end(); ++it) { | 105 it != files.end(); ++it) { |
137 #if defined(OS_WIN) | 106 #if defined(OS_WIN) |
138 FilePath file_path(base::SysUTF8ToWide(*it)); | 107 FilePath file_path(base::SysUTF8ToWide(*it)); |
139 #else | 108 #else |
140 FilePath file_path(*it); | 109 FilePath file_path(*it); |
(...skipping 28 matching lines...) Expand all Loading... |
169 callback_ = new TrackedCompletionCallback( | 138 callback_ = new TrackedCompletionCallback( |
170 instance()->module()->GetCallbackTracker(), resource_id, callback); | 139 instance()->module()->GetCallbackTracker(), resource_id, callback); |
171 } | 140 } |
172 | 141 |
173 void PPB_FileChooser_Impl::RunCallback(int32_t result) { | 142 void PPB_FileChooser_Impl::RunCallback(int32_t result) { |
174 scoped_refptr<TrackedCompletionCallback> callback; | 143 scoped_refptr<TrackedCompletionCallback> callback; |
175 callback.swap(callback_); | 144 callback.swap(callback_); |
176 callback->Run(result); // Will complete abortively if necessary. | 145 callback->Run(result); // Will complete abortively if necessary. |
177 } | 146 } |
178 | 147 |
179 int32_t PPB_FileChooser_Impl::Show(const PP_CompletionCallback& callback) { | 148 int32_t PPB_FileChooser_Impl::Show(PP_CompletionCallback callback) { |
180 int32_t rv = ValidateCallback(callback); | 149 int32_t rv = ValidateCallback(callback); |
181 if (rv != PP_OK) | 150 if (rv != PP_OK) |
182 return rv; | 151 return rv; |
183 | 152 |
184 DCHECK((mode_ == PP_FILECHOOSERMODE_OPEN) || | 153 DCHECK((mode_ == PP_FILECHOOSERMODE_OPEN) || |
185 (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE)); | 154 (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE)); |
186 | 155 |
187 WebFileChooserParams params; | 156 WebFileChooserParams params; |
188 params.multiSelect = (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE); | 157 params.multiSelect = (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE); |
189 params.acceptTypes = WebString::fromUTF8(accept_mime_types_); | 158 params.acceptTypes = WebString::fromUTF8(accept_mime_types_); |
190 params.directory = false; | 159 params.directory = false; |
191 | 160 |
192 if (!instance()->delegate()->RunFileChooser(params, | 161 if (!instance()->delegate()->RunFileChooser(params, |
193 new FileChooserCompletionImpl(this))) | 162 new FileChooserCompletionImpl(this))) |
194 return PP_ERROR_FAILED; | 163 return PP_ERROR_FAILED; |
195 | 164 |
196 RegisterCallback(callback); | 165 RegisterCallback(callback); |
197 return PP_OK_COMPLETIONPENDING; | 166 return PP_OK_COMPLETIONPENDING; |
198 } | 167 } |
199 | 168 |
200 scoped_refptr<PPB_FileRef_Impl> PPB_FileChooser_Impl::GetNextChosenFile() { | 169 PP_Resource PPB_FileChooser_Impl::GetNextChosenFile() { |
201 if (next_chosen_file_index_ >= chosen_files_.size()) | 170 if (next_chosen_file_index_ >= chosen_files_.size()) |
202 return NULL; | 171 return 0; |
203 | 172 |
204 return chosen_files_[next_chosen_file_index_++]; | 173 return chosen_files_[next_chosen_file_index_++]->GetReference(); |
205 } | 174 } |
206 | 175 |
207 } // namespace ppapi | 176 } // namespace ppapi |
208 } // namespace webkit | 177 } // namespace webkit |
OLD | NEW |