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