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 "ppapi/shared_impl/var.h" | 14 #include "ppapi/shared_impl/var.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCString.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCString.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserComplet
ion.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserComplet
ion.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserParams.
h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFileChooserParams.
h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" |
| 20 #include "webkit/glue/webkit_glue.h" |
20 #include "webkit/plugins/ppapi/callbacks.h" | 21 #include "webkit/plugins/ppapi/callbacks.h" |
21 #include "webkit/plugins/ppapi/common.h" | 22 #include "webkit/plugins/ppapi/common.h" |
22 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | 23 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
23 #include "webkit/plugins/ppapi/plugin_delegate.h" | 24 #include "webkit/plugins/ppapi/plugin_delegate.h" |
24 #include "webkit/plugins/ppapi/plugin_module.h" | 25 #include "webkit/plugins/ppapi/plugin_module.h" |
25 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 26 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
26 #include "webkit/plugins/ppapi/resource_tracker.h" | 27 #include "webkit/plugins/ppapi/resource_helper.h" |
27 #include "webkit/glue/webkit_glue.h" | |
28 | 28 |
29 using ppapi::StringVar; | 29 using ppapi::StringVar; |
30 using ppapi::thunk::PPB_FileChooser_API; | 30 using ppapi::thunk::PPB_FileChooser_API; |
31 using WebKit::WebCString; | 31 using WebKit::WebCString; |
32 using WebKit::WebFileChooserCompletion; | 32 using WebKit::WebFileChooserCompletion; |
33 using WebKit::WebFileChooserParams; | 33 using WebKit::WebFileChooserParams; |
34 using WebKit::WebString; | 34 using WebKit::WebString; |
35 using WebKit::WebVector; | 35 using WebKit::WebVector; |
36 | 36 |
37 namespace webkit { | 37 namespace webkit { |
(...skipping 18 matching lines...) Expand all Loading... |
56 file_chooser_->StoreChosenFiles(files); | 56 file_chooser_->StoreChosenFiles(files); |
57 } | 57 } |
58 | 58 |
59 private: | 59 private: |
60 scoped_refptr<PPB_FileChooser_Impl> file_chooser_; | 60 scoped_refptr<PPB_FileChooser_Impl> file_chooser_; |
61 }; | 61 }; |
62 | 62 |
63 } // namespace | 63 } // namespace |
64 | 64 |
65 PPB_FileChooser_Impl::PPB_FileChooser_Impl( | 65 PPB_FileChooser_Impl::PPB_FileChooser_Impl( |
66 PluginInstance* instance, | 66 PP_Instance instance, |
67 PP_FileChooserMode_Dev mode, | 67 PP_FileChooserMode_Dev mode, |
68 const PP_Var& accept_mime_types) | 68 const PP_Var& accept_mime_types) |
69 : Resource(instance), | 69 : Resource(instance), |
70 mode_(mode), | 70 mode_(mode), |
71 next_chosen_file_index_(0) { | 71 next_chosen_file_index_(0) { |
72 scoped_refptr<StringVar> accept = StringVar::FromPPVar(accept_mime_types); | 72 scoped_refptr<StringVar> accept = StringVar::FromPPVar(accept_mime_types); |
73 if (accept) | 73 if (accept) |
74 accept_mime_types_ = accept->value(); | 74 accept_mime_types_ = accept->value(); |
75 } | 75 } |
76 | 76 |
77 PPB_FileChooser_Impl::~PPB_FileChooser_Impl() { | 77 PPB_FileChooser_Impl::~PPB_FileChooser_Impl() { |
78 } | 78 } |
79 | 79 |
80 // static | 80 // static |
81 PP_Resource PPB_FileChooser_Impl::Create( | 81 PP_Resource PPB_FileChooser_Impl::Create( |
82 PluginInstance* instance, | 82 PP_Instance instance, |
83 PP_FileChooserMode_Dev mode, | 83 PP_FileChooserMode_Dev mode, |
84 const PP_Var& accept_mime_types) { | 84 const PP_Var& accept_mime_types) { |
85 if (mode != PP_FILECHOOSERMODE_OPEN && | 85 if (mode != PP_FILECHOOSERMODE_OPEN && |
86 mode != PP_FILECHOOSERMODE_OPENMULTIPLE) | 86 mode != PP_FILECHOOSERMODE_OPENMULTIPLE) |
87 return 0; | 87 return 0; |
88 return (new PPB_FileChooser_Impl(instance, mode, | 88 return (new PPB_FileChooser_Impl(instance, mode, |
89 accept_mime_types))->GetReference(); | 89 accept_mime_types))->GetReference(); |
90 } | 90 } |
91 | 91 |
92 PPB_FileChooser_Impl* PPB_FileChooser_Impl::AsPPB_FileChooser_Impl() { | 92 PPB_FileChooser_Impl* PPB_FileChooser_Impl::AsPPB_FileChooser_Impl() { |
(...skipping 10 matching lines...) Expand all Loading... |
103 next_chosen_file_index_ = 0; | 103 next_chosen_file_index_ = 0; |
104 for (std::vector<std::string>::const_iterator it = files.begin(); | 104 for (std::vector<std::string>::const_iterator it = files.begin(); |
105 it != files.end(); ++it) { | 105 it != files.end(); ++it) { |
106 #if defined(OS_WIN) | 106 #if defined(OS_WIN) |
107 FilePath file_path(base::SysUTF8ToWide(*it)); | 107 FilePath file_path(base::SysUTF8ToWide(*it)); |
108 #else | 108 #else |
109 FilePath file_path(*it); | 109 FilePath file_path(*it); |
110 #endif | 110 #endif |
111 | 111 |
112 chosen_files_.push_back(make_scoped_refptr( | 112 chosen_files_.push_back(make_scoped_refptr( |
113 new PPB_FileRef_Impl(instance(), file_path))); | 113 new PPB_FileRef_Impl(pp_instance(), file_path))); |
114 } | 114 } |
115 | 115 |
116 RunCallback((chosen_files_.size() > 0) ? PP_OK : PP_ERROR_USERCANCEL); | 116 RunCallback((chosen_files_.size() > 0) ? PP_OK : PP_ERROR_USERCANCEL); |
117 } | 117 } |
118 | 118 |
119 int32_t PPB_FileChooser_Impl::ValidateCallback( | 119 int32_t PPB_FileChooser_Impl::ValidateCallback( |
120 const PP_CompletionCallback& callback) { | 120 const PP_CompletionCallback& callback) { |
121 // We only support non-blocking calls. | 121 // We only support non-blocking calls. |
122 if (!callback.func) | 122 if (!callback.func) |
123 return PP_ERROR_BADARGUMENT; | 123 return PP_ERROR_BADARGUMENT; |
124 | 124 |
125 if (callback_.get() && !callback_->completed()) | 125 if (callback_.get() && !callback_->completed()) |
126 return PP_ERROR_INPROGRESS; | 126 return PP_ERROR_INPROGRESS; |
127 | 127 |
128 return PP_OK; | 128 return PP_OK; |
129 } | 129 } |
130 | 130 |
131 void PPB_FileChooser_Impl::RegisterCallback( | 131 void PPB_FileChooser_Impl::RegisterCallback( |
132 const PP_CompletionCallback& callback) { | 132 const PP_CompletionCallback& callback) { |
133 DCHECK(callback.func); | 133 DCHECK(callback.func); |
134 DCHECK(!callback_.get() || callback_->completed()); | 134 DCHECK(!callback_.get() || callback_->completed()); |
135 | 135 |
136 callback_ = new TrackedCompletionCallback( | 136 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); |
137 instance()->module()->GetCallbackTracker(), pp_resource(), callback); | 137 if (!plugin_module) |
| 138 return; |
| 139 |
| 140 callback_ = new TrackedCompletionCallback(plugin_module->GetCallbackTracker(), |
| 141 pp_resource(), callback); |
138 } | 142 } |
139 | 143 |
140 void PPB_FileChooser_Impl::RunCallback(int32_t result) { | 144 void PPB_FileChooser_Impl::RunCallback(int32_t result) { |
141 scoped_refptr<TrackedCompletionCallback> callback; | 145 scoped_refptr<TrackedCompletionCallback> callback; |
142 callback.swap(callback_); | 146 callback.swap(callback_); |
143 callback->Run(result); // Will complete abortively if necessary. | 147 callback->Run(result); // Will complete abortively if necessary. |
144 } | 148 } |
145 | 149 |
146 int32_t PPB_FileChooser_Impl::Show(const PP_CompletionCallback& callback) { | 150 int32_t PPB_FileChooser_Impl::Show(const PP_CompletionCallback& callback) { |
147 int32_t rv = ValidateCallback(callback); | 151 int32_t rv = ValidateCallback(callback); |
148 if (rv != PP_OK) | 152 if (rv != PP_OK) |
149 return rv; | 153 return rv; |
150 | 154 |
151 DCHECK((mode_ == PP_FILECHOOSERMODE_OPEN) || | 155 DCHECK((mode_ == PP_FILECHOOSERMODE_OPEN) || |
152 (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE)); | 156 (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE)); |
153 | 157 |
154 WebFileChooserParams params; | 158 WebFileChooserParams params; |
155 params.multiSelect = (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE); | 159 params.multiSelect = (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE); |
156 params.acceptTypes = WebString::fromUTF8(accept_mime_types_); | 160 params.acceptTypes = WebString::fromUTF8(accept_mime_types_); |
157 params.directory = false; | 161 params.directory = false; |
158 | 162 |
159 if (!instance()->delegate()->RunFileChooser(params, | 163 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
160 new FileChooserCompletionImpl(this))) | 164 if (!plugin_delegate) |
| 165 return PP_ERROR_FAILED; |
| 166 |
| 167 if (!plugin_delegate->RunFileChooser(params, |
| 168 new FileChooserCompletionImpl(this))) |
161 return PP_ERROR_FAILED; | 169 return PP_ERROR_FAILED; |
162 | 170 |
163 RegisterCallback(callback); | 171 RegisterCallback(callback); |
164 return PP_OK_COMPLETIONPENDING; | 172 return PP_OK_COMPLETIONPENDING; |
165 } | 173 } |
166 | 174 |
167 PP_Resource PPB_FileChooser_Impl::GetNextChosenFile() { | 175 PP_Resource PPB_FileChooser_Impl::GetNextChosenFile() { |
168 if (next_chosen_file_index_ >= chosen_files_.size()) | 176 if (next_chosen_file_index_ >= chosen_files_.size()) |
169 return 0; | 177 return 0; |
170 | 178 |
171 return chosen_files_[next_chosen_file_index_++]->GetReference(); | 179 return chosen_files_[next_chosen_file_index_++]->GetReference(); |
172 } | 180 } |
173 | 181 |
174 } // namespace ppapi | 182 } // namespace ppapi |
175 } // namespace webkit | 183 } // namespace webkit |
OLD | NEW |