| 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; | |
| 29 using WebKit::WebCString; | 28 using WebKit::WebCString; |
| 30 using WebKit::WebFileChooserCompletion; | 29 using WebKit::WebFileChooserCompletion; |
| 31 using WebKit::WebFileChooserParams; | 30 using WebKit::WebFileChooserParams; |
| 32 using WebKit::WebString; | 31 using WebKit::WebString; |
| 33 using WebKit::WebVector; | 32 using WebKit::WebVector; |
| 34 | 33 |
| 35 namespace webkit { | 34 namespace webkit { |
| 36 namespace ppapi { | 35 namespace ppapi { |
| 37 | 36 |
| 38 namespace { | 37 namespace { |
| 39 | 38 |
| 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 |
| 40 class FileChooserCompletionImpl : public WebFileChooserCompletion { | 86 class FileChooserCompletionImpl : public WebFileChooserCompletion { |
| 41 public: | 87 public: |
| 42 FileChooserCompletionImpl(PPB_FileChooser_Impl* file_chooser) | 88 FileChooserCompletionImpl(PPB_FileChooser_Impl* file_chooser) |
| 43 : file_chooser_(file_chooser) { | 89 : file_chooser_(file_chooser) { |
| 44 DCHECK(file_chooser_); | 90 DCHECK(file_chooser_); |
| 45 } | 91 } |
| 46 | 92 |
| 47 virtual ~FileChooserCompletionImpl() {} | 93 virtual ~FileChooserCompletionImpl() {} |
| 48 | 94 |
| 49 virtual void didChooseFile(const WebVector<WebString>& file_names) { | 95 virtual void didChooseFile(const WebVector<WebString>& file_names) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 67 mode_(options->mode), | 113 mode_(options->mode), |
| 68 accept_mime_types_(options->accept_mime_types ? | 114 accept_mime_types_(options->accept_mime_types ? |
| 69 options->accept_mime_types : ""), | 115 options->accept_mime_types : ""), |
| 70 next_chosen_file_index_(0) { | 116 next_chosen_file_index_(0) { |
| 71 } | 117 } |
| 72 | 118 |
| 73 PPB_FileChooser_Impl::~PPB_FileChooser_Impl() { | 119 PPB_FileChooser_Impl::~PPB_FileChooser_Impl() { |
| 74 } | 120 } |
| 75 | 121 |
| 76 // static | 122 // static |
| 77 PP_Resource PPB_FileChooser_Impl::Create( | 123 const PPB_FileChooser_Dev* PPB_FileChooser_Impl::GetInterface() { |
| 78 PP_Instance pp_instance, | 124 return &ppb_filechooser; |
| 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(); | |
| 90 } | 125 } |
| 91 | 126 |
| 92 PPB_FileChooser_Impl* PPB_FileChooser_Impl::AsPPB_FileChooser_Impl() { | 127 PPB_FileChooser_Impl* PPB_FileChooser_Impl::AsPPB_FileChooser_Impl() { |
| 93 return this; | 128 return this; |
| 94 } | 129 } |
| 95 | 130 |
| 96 PPB_FileChooser_API* PPB_FileChooser_Impl::AsPPB_FileChooser_API() { | |
| 97 return this; | |
| 98 } | |
| 99 | |
| 100 void PPB_FileChooser_Impl::StoreChosenFiles( | 131 void PPB_FileChooser_Impl::StoreChosenFiles( |
| 101 const std::vector<std::string>& files) { | 132 const std::vector<std::string>& files) { |
| 102 chosen_files_.clear(); | 133 chosen_files_.clear(); |
| 103 next_chosen_file_index_ = 0; | 134 next_chosen_file_index_ = 0; |
| 104 for (std::vector<std::string>::const_iterator it = files.begin(); | 135 for (std::vector<std::string>::const_iterator it = files.begin(); |
| 105 it != files.end(); ++it) { | 136 it != files.end(); ++it) { |
| 106 #if defined(OS_WIN) | 137 #if defined(OS_WIN) |
| 107 FilePath file_path(base::SysUTF8ToWide(*it)); | 138 FilePath file_path(base::SysUTF8ToWide(*it)); |
| 108 #else | 139 #else |
| 109 FilePath file_path(*it); | 140 FilePath file_path(*it); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 138 callback_ = new TrackedCompletionCallback( | 169 callback_ = new TrackedCompletionCallback( |
| 139 instance()->module()->GetCallbackTracker(), resource_id, callback); | 170 instance()->module()->GetCallbackTracker(), resource_id, callback); |
| 140 } | 171 } |
| 141 | 172 |
| 142 void PPB_FileChooser_Impl::RunCallback(int32_t result) { | 173 void PPB_FileChooser_Impl::RunCallback(int32_t result) { |
| 143 scoped_refptr<TrackedCompletionCallback> callback; | 174 scoped_refptr<TrackedCompletionCallback> callback; |
| 144 callback.swap(callback_); | 175 callback.swap(callback_); |
| 145 callback->Run(result); // Will complete abortively if necessary. | 176 callback->Run(result); // Will complete abortively if necessary. |
| 146 } | 177 } |
| 147 | 178 |
| 148 int32_t PPB_FileChooser_Impl::Show(PP_CompletionCallback callback) { | 179 int32_t PPB_FileChooser_Impl::Show(const PP_CompletionCallback& callback) { |
| 149 int32_t rv = ValidateCallback(callback); | 180 int32_t rv = ValidateCallback(callback); |
| 150 if (rv != PP_OK) | 181 if (rv != PP_OK) |
| 151 return rv; | 182 return rv; |
| 152 | 183 |
| 153 DCHECK((mode_ == PP_FILECHOOSERMODE_OPEN) || | 184 DCHECK((mode_ == PP_FILECHOOSERMODE_OPEN) || |
| 154 (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE)); | 185 (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE)); |
| 155 | 186 |
| 156 WebFileChooserParams params; | 187 WebFileChooserParams params; |
| 157 params.multiSelect = (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE); | 188 params.multiSelect = (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE); |
| 158 params.acceptTypes = WebString::fromUTF8(accept_mime_types_); | 189 params.acceptTypes = WebString::fromUTF8(accept_mime_types_); |
| 159 params.directory = false; | 190 params.directory = false; |
| 160 | 191 |
| 161 if (!instance()->delegate()->RunFileChooser(params, | 192 if (!instance()->delegate()->RunFileChooser(params, |
| 162 new FileChooserCompletionImpl(this))) | 193 new FileChooserCompletionImpl(this))) |
| 163 return PP_ERROR_FAILED; | 194 return PP_ERROR_FAILED; |
| 164 | 195 |
| 165 RegisterCallback(callback); | 196 RegisterCallback(callback); |
| 166 return PP_OK_COMPLETIONPENDING; | 197 return PP_OK_COMPLETIONPENDING; |
| 167 } | 198 } |
| 168 | 199 |
| 169 PP_Resource PPB_FileChooser_Impl::GetNextChosenFile() { | 200 scoped_refptr<PPB_FileRef_Impl> PPB_FileChooser_Impl::GetNextChosenFile() { |
| 170 if (next_chosen_file_index_ >= chosen_files_.size()) | 201 if (next_chosen_file_index_ >= chosen_files_.size()) |
| 171 return 0; | 202 return NULL; |
| 172 | 203 |
| 173 return chosen_files_[next_chosen_file_index_++]->GetReference(); | 204 return chosen_files_[next_chosen_file_index_++]; |
| 174 } | 205 } |
| 175 | 206 |
| 176 } // namespace ppapi | 207 } // namespace ppapi |
| 177 } // namespace webkit | 208 } // namespace webkit |
| OLD | NEW |