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