1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "win8/metro_driver/file_picker_ash.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_util.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/win/metro.h"
14 #include "base/win/scoped_comptr.h"
15 #include "ui/metro_viewer/metro_viewer_messages.h"
16 #include "win8/metro_driver/chrome_app_view_ash.h"
17 #include "win8/metro_driver/winrt_utils.h"
21 typedef winfoundtn::Collections::IVector
<HSTRING
> StringVectorItf
;
23 // TODO(siggi): Complete this implementation and move it to a common place.
24 class StringVectorImpl
: public mswr::RuntimeClass
<StringVectorItf
> {
27 std::for_each(strings_
.begin(), strings_
.end(), ::WindowsDeleteString
);
30 HRESULT
RuntimeClassInitialize(const std::vector
<base::string16
>& list
) {
31 for (size_t i
= 0; i
< list
.size(); ++i
)
32 strings_
.push_back(MakeHString(list
[i
]));
37 // IVector<HSTRING> implementation.
38 STDMETHOD(GetAt
)(unsigned index
, HSTRING
* item
) {
39 if (index
>= strings_
.size())
42 return ::WindowsDuplicateString(strings_
[index
], item
);
44 STDMETHOD(get_Size
)(unsigned *size
) {
45 if (strings_
.size() > UINT_MAX
)
47 *size
= static_cast<unsigned>(strings_
.size());
50 STDMETHOD(GetView
)(winfoundtn::Collections::IVectorView
<HSTRING
> **view
) {
53 STDMETHOD(IndexOf
)(HSTRING value
, unsigned *index
, boolean
*found
) {
58 STDMETHOD(SetAt
)(unsigned index
, HSTRING item
) {
61 STDMETHOD(InsertAt
)(unsigned index
, HSTRING item
) {
64 STDMETHOD(RemoveAt
)(unsigned index
) {
67 STDMETHOD(Append
)(HSTRING item
) {
70 STDMETHOD(RemoveAtEnd
)() {
78 std::vector
<HSTRING
> strings_
;
83 FilePickerSessionBase::FilePickerSessionBase(ChromeAppViewAsh
* app_view
,
84 const base::string16
& title
,
85 const base::string16
& filter
,
86 const base::FilePath
& default_path
)
87 : app_view_(app_view
),
90 default_path_(default_path
),
94 bool FilePickerSessionBase::Run() {
100 bool FilePickerSessionBase::DoFilePicker() {
101 // The file picker will fail if spawned from a snapped application,
102 // so let's attempt to unsnap first if we're in that state.
103 HRESULT hr
= ChromeAppViewAsh::Unsnap();
105 LOG(ERROR
) << "Failed to unsnap for file picker, error 0x" << hr
;
108 hr
= StartFilePicker();
110 LOG(ERROR
) << "Failed to start file picker, error 0x"
117 OpenFilePickerSession::OpenFilePickerSession(
118 ChromeAppViewAsh
* app_view
,
119 const base::string16
& title
,
120 const base::string16
& filter
,
121 const base::FilePath
& default_path
,
122 bool allow_multi_select
)
123 : FilePickerSessionBase(app_view
, title
, filter
, default_path
),
124 allow_multi_select_(allow_multi_select
) {
127 HRESULT
OpenFilePickerSession::SinglePickerDone(SingleFileAsyncOp
* async
,
128 AsyncStatus status
) {
129 if (status
== Completed
) {
130 mswr::ComPtr
<winstorage::IStorageFile
> file
;
131 HRESULT hr
= async
->GetResults(file
.GetAddressOf());
134 mswr::ComPtr
<winstorage::IStorageItem
> storage_item
;
136 hr
= file
.As(&storage_item
);
138 mswrw::HString file_path
;
140 hr
= storage_item
->get_Path(file_path
.GetAddressOf());
144 const wchar_t* path_str
=
145 ::WindowsGetStringRawBuffer(file_path
.Get(), &path_len
);
151 LOG(ERROR
) << "NULL IStorageItem";
154 LOG(ERROR
) << "Unexpected async status " << static_cast<int>(status
);
156 app_view_
->OnOpenFileCompleted(this, success_
);
160 HRESULT
OpenFilePickerSession::MultiPickerDone(MultiFileAsyncOp
* async
,
161 AsyncStatus status
) {
162 if (status
== Completed
) {
163 mswr::ComPtr
<StorageFileVectorCollection
> files
;
164 HRESULT hr
= async
->GetResults(files
.GetAddressOf());
167 base::string16 result
;
169 hr
= ComposeMultiFileResult(files
.Get(), &result
);
173 // The code below has been copied from the
174 // SelectFileDialogImpl::RunOpenMultiFileDialog function in
175 // select_file_dialog_win.cc.
177 // Consolidate this into a common place.
178 const wchar_t* selection
= result
.c_str();
179 std::vector
<base::FilePath
> files
;
181 while (*selection
) { // Empty string indicates end of list.
182 files
.push_back(base::FilePath(selection
));
183 // Skip over filename and null-terminator.
184 selection
+= files
.back().value().length() + 1;
188 } else if (files
.size() == 1) {
189 // When there is one file, it contains the path and filename.
191 } else if (files
.size() > 1) {
192 // Otherwise, the first string is the path, and the remainder are
194 std::vector
<base::FilePath
>::iterator path
= files
.begin();
195 for (std::vector
<base::FilePath
>::iterator file
= path
+ 1;
196 file
!= files
.end(); ++file
) {
197 filenames_
.push_back(path
->Append(*file
));
202 LOG(ERROR
) << "NULL StorageFileVectorCollection";
205 LOG(ERROR
) << "Unexpected async status " << static_cast<int>(status
);
207 app_view_
->OnOpenFileCompleted(this, success_
);
211 HRESULT
OpenFilePickerSession::StartFilePicker() {
212 mswrw::HStringReference
class_name(
213 RuntimeClass_Windows_Storage_Pickers_FileOpenPicker
);
215 // Create the file picker.
216 mswr::ComPtr
<winstorage::Pickers::IFileOpenPicker
> picker
;
217 HRESULT hr
= ::Windows::Foundation::ActivateInstance(
218 class_name
.Get(), picker
.GetAddressOf());
221 // Set the file type filter
222 mswr::ComPtr
<winfoundtn::Collections::IVector
<HSTRING
>> filter
;
223 hr
= picker
->get_FileTypeFilter(filter
.GetAddressOf());
227 if (filter_
.empty()) {
228 hr
= filter
->Append(mswrw::HStringReference(L
"*").Get());
232 // The filter is a concatenation of zero terminated string pairs,
233 // where each pair is {description, extension}. The concatenation ends
234 // with a zero length string - e.g. a double zero terminator.
235 const wchar_t* walk
= filter_
.c_str();
236 while (*walk
!= L
'\0') {
237 // Walk past the description.
238 walk
+= wcslen(walk
) + 1;
240 // We should have an extension, but bail on malformed filters.
244 // There can be a single extension, or a list of semicolon-separated ones.
245 std::vector
<base::string16
> extensions_win32_style
;
246 size_t extension_count
= Tokenize(walk
, L
";", &extensions_win32_style
);
247 DCHECK_EQ(extension_count
, extensions_win32_style
.size());
249 // Metro wants suffixes only, not patterns.
250 mswrw::HString extension
;
251 for (size_t i
= 0; i
< extensions_win32_style
.size(); ++i
) {
252 if (extensions_win32_style
[i
] == L
"*.*") {
253 // The wildcard filter is "*" for Metro. The string "*.*" produces
254 // an "invalid parameter" error.
255 hr
= extension
.Set(L
"*");
257 // Metro wants suffixes only, not patterns.
259 base::FilePath(extensions_win32_style
[i
]).Extension();
260 if ((ext
.size() < 2) ||
261 (ext
.find_first_of(L
"*?") != base::string16::npos
)) {
264 hr
= extension
.Set(ext
.c_str());
267 hr
= filter
->Append(extension
.Get());
272 // Walk past the extension.
273 walk
+= wcslen(walk
) + 1;
277 // Spin up a single or multi picker as appropriate.
278 if (allow_multi_select_
) {
279 mswr::ComPtr
<MultiFileAsyncOp
> completion
;
280 hr
= picker
->PickMultipleFilesAsync(&completion
);
284 // Create the callback method.
285 typedef winfoundtn::IAsyncOperationCompletedHandler
<
286 StorageFileVectorCollection
*> HandlerDoneType
;
287 mswr::ComPtr
<HandlerDoneType
> handler(mswr::Callback
<HandlerDoneType
>(
288 this, &OpenFilePickerSession::MultiPickerDone
));
289 DCHECK(handler
.Get() != NULL
);
290 hr
= completion
->put_Completed(handler
.Get());
294 mswr::ComPtr
<SingleFileAsyncOp
> completion
;
295 hr
= picker
->PickSingleFileAsync(&completion
);
299 // Create the callback method.
300 typedef winfoundtn::IAsyncOperationCompletedHandler
<
301 winstorage::StorageFile
*> HandlerDoneType
;
302 mswr::ComPtr
<HandlerDoneType
> handler(mswr::Callback
<HandlerDoneType
>(
303 this, &OpenFilePickerSession::SinglePickerDone
));
304 DCHECK(handler
.Get() != NULL
);
305 hr
= completion
->put_Completed(handler
.Get());
311 HRESULT
OpenFilePickerSession::ComposeMultiFileResult(
312 StorageFileVectorCollection
* files
, base::string16
* result
) {
313 DCHECK(files
!= NULL
);
314 DCHECK(result
!= NULL
);
316 // Empty the output string.
319 unsigned int num_files
= 0;
320 HRESULT hr
= files
->get_Size(&num_files
);
324 // Make sure we return an error on an empty collection.
325 if (num_files
== 0) {
326 DLOG(ERROR
) << "Empty collection on input.";
330 // This stores the base path that should be the parent of all the files.
331 base::FilePath base_path
;
333 // Iterate through the collection and append the file paths to the result.
334 for (unsigned int i
= 0; i
< num_files
; ++i
) {
335 mswr::ComPtr
<winstorage::IStorageFile
> file
;
336 hr
= files
->GetAt(i
, file
.GetAddressOf());
340 mswr::ComPtr
<winstorage::IStorageItem
> storage_item
;
341 hr
= file
.As(&storage_item
);
345 mswrw::HString file_path_str
;
346 hr
= storage_item
->get_Path(file_path_str
.GetAddressOf());
350 base::FilePath
file_path(MakeStdWString(file_path_str
.Get()));
351 if (base_path
.empty()) {
352 DCHECK(result
->empty());
353 base_path
= file_path
.DirName();
355 // Append the path, including the terminating zero.
356 // We do this only for the first file.
357 result
->append(base_path
.value().c_str(), base_path
.value().size() + 1);
359 DCHECK(!result
->empty());
360 DCHECK(!base_path
.empty());
361 DCHECK(base_path
== file_path
.DirName());
363 // Append the base name, including the terminating zero.
364 base::FilePath base_name
= file_path
.BaseName();
365 result
->append(base_name
.value().c_str(), base_name
.value().size() + 1);
368 DCHECK(!result
->empty());
373 SaveFilePickerSession::SaveFilePickerSession(
374 ChromeAppViewAsh
* app_view
,
375 const MetroViewerHostMsg_SaveAsDialogParams
& params
)
376 : FilePickerSessionBase(app_view
,
379 params
.suggested_name
),
380 filter_index_(params
.filter_index
) {
383 int SaveFilePickerSession::filter_index() const {
385 // Add support for returning the correct filter index. This does not work in
386 // regular Chrome metro on trunk as well.
387 // BUG: https://code.google.com/p/chromium/issues/detail?id=172704
388 return filter_index_
;
391 HRESULT
SaveFilePickerSession::StartFilePicker() {
392 mswrw::HStringReference
class_name(
393 RuntimeClass_Windows_Storage_Pickers_FileSavePicker
);
395 // Create the file picker.
396 mswr::ComPtr
<winstorage::Pickers::IFileSavePicker
> picker
;
397 HRESULT hr
= ::Windows::Foundation::ActivateInstance(
398 class_name
.Get(), picker
.GetAddressOf());
401 typedef winfoundtn::Collections::IMap
<HSTRING
, StringVectorItf
*>
403 mswr::ComPtr
<StringVectorMap
> choices
;
404 hr
= picker
->get_FileTypeChoices(choices
.GetAddressOf());
408 if (!filter_
.empty()) {
409 // The filter is a concatenation of zero terminated string pairs,
410 // where each pair is {description, extension list}. The concatenation ends
411 // with a zero length string - e.g. a double zero terminator.
412 const wchar_t* walk
= filter_
.c_str();
413 while (*walk
!= L
'\0') {
414 mswrw::HString description
;
415 hr
= description
.Set(walk
);
419 // Walk past the description.
420 walk
+= wcslen(walk
) + 1;
422 // We should have an extension, but bail on malformed filters.
426 // There can be a single extension, or a list of semicolon-separated ones.
427 std::vector
<base::string16
> extensions_win32_style
;
428 size_t extension_count
= Tokenize(walk
, L
";", &extensions_win32_style
);
429 DCHECK_EQ(extension_count
, extensions_win32_style
.size());
431 // Metro wants suffixes only, not patterns. Also, metro does not support
432 // the all files ("*") pattern in the save picker.
433 std::vector
<base::string16
> extensions
;
434 for (size_t i
= 0; i
< extensions_win32_style
.size(); ++i
) {
436 base::FilePath(extensions_win32_style
[i
]).Extension();
437 if ((ext
.size() < 2) ||
438 (ext
.find_first_of(L
"*?") != base::string16::npos
))
440 extensions
.push_back(ext
);
443 if (!extensions
.empty()) {
444 // Convert to a Metro collection class.
445 mswr::ComPtr
<StringVectorItf
> list
;
446 hr
= mswr::MakeAndInitialize
<StringVectorImpl
>(
447 list
.GetAddressOf(), extensions
);
451 // Finally set the filter.
452 boolean replaced
= FALSE
;
453 hr
= choices
->Insert(description
.Get(), list
.Get(), &replaced
);
456 DCHECK_EQ(FALSE
, replaced
);
459 // Walk past the extension(s).
460 walk
+= wcslen(walk
) + 1;
464 // The save picker requires at least one choice. Callers are strongly advised
465 // to provide sensible choices. If none were given, fallback to .dat.
466 uint32 num_choices
= 0;
467 hr
= choices
->get_Size(&num_choices
);
471 if (num_choices
== 0) {
472 mswrw::HString description
;
473 // TODO(grt): Get a properly translated string. This can't be done from
474 // within metro_driver. Consider preprocessing the filter list in Chrome
475 // land to ensure it has this entry if all others are patterns. In that
476 // case, this whole block of code can be removed.
477 hr
= description
.Set(L
"Data File");
481 mswr::ComPtr
<StringVectorItf
> list
;
482 hr
= mswr::MakeAndInitialize
<StringVectorImpl
>(
483 list
.GetAddressOf(), std::vector
<base::string16
>(1, L
".dat"));
487 boolean replaced
= FALSE
;
488 hr
= choices
->Insert(description
.Get(), list
.Get(), &replaced
);
491 DCHECK_EQ(FALSE
, replaced
);
494 if (!default_path_
.empty()) {
495 base::string16 file_part
= default_path_
.BaseName().value();
496 // If the suggested_name is a root directory, then don't set it as the
498 if (file_part
.size() == 1 && file_part
[0] == L
'\\')
500 hr
= picker
->put_SuggestedFileName(
501 mswrw::HStringReference(file_part
.c_str()).Get());
506 mswr::ComPtr
<SaveFileAsyncOp
> completion
;
507 hr
= picker
->PickSaveFileAsync(&completion
);
511 // Create the callback method.
512 typedef winfoundtn::IAsyncOperationCompletedHandler
<
513 winstorage::StorageFile
*> HandlerDoneType
;
514 mswr::ComPtr
<HandlerDoneType
> handler(mswr::Callback
<HandlerDoneType
>(
515 this, &SaveFilePickerSession::FilePickerDone
));
516 DCHECK(handler
.Get() != NULL
);
517 hr
= completion
->put_Completed(handler
.Get());
522 HRESULT
SaveFilePickerSession::FilePickerDone(SaveFileAsyncOp
* async
,
523 AsyncStatus status
) {
524 if (status
== Completed
) {
525 mswr::ComPtr
<winstorage::IStorageFile
> file
;
526 HRESULT hr
= async
->GetResults(file
.GetAddressOf());
529 mswr::ComPtr
<winstorage::IStorageItem
> storage_item
;
531 hr
= file
.As(&storage_item
);
533 mswrw::HString file_path
;
535 hr
= storage_item
->get_Path(file_path
.GetAddressOf());
538 base::string16 path_str
= MakeStdWString(file_path
.Get());
543 LOG(ERROR
) << "NULL IStorageItem";
546 LOG(ERROR
) << "Unexpected async status " << static_cast<int>(status
);
548 app_view_
->OnSaveFileCompleted(this, success_
);
552 FolderPickerSession::FolderPickerSession(ChromeAppViewAsh
* app_view
,
553 const base::string16
& title
)
554 : FilePickerSessionBase(app_view
, title
, L
"", base::FilePath()) {}
556 HRESULT
FolderPickerSession::StartFilePicker() {
557 mswrw::HStringReference
class_name(
558 RuntimeClass_Windows_Storage_Pickers_FolderPicker
);
560 // Create the folder picker.
561 mswr::ComPtr
<winstorage::Pickers::IFolderPicker
> picker
;
562 HRESULT hr
= ::Windows::Foundation::ActivateInstance(
563 class_name
.Get(), picker
.GetAddressOf());
566 // Set the file type filter
567 mswr::ComPtr
<winfoundtn::Collections::IVector
<HSTRING
>> filter
;
568 hr
= picker
->get_FileTypeFilter(filter
.GetAddressOf());
572 hr
= filter
->Append(mswrw::HStringReference(L
"*").Get());
576 mswr::ComPtr
<FolderPickerAsyncOp
> completion
;
577 hr
= picker
->PickSingleFolderAsync(&completion
);
581 // Create the callback method.
582 typedef winfoundtn::IAsyncOperationCompletedHandler
<
583 winstorage::StorageFolder
*> HandlerDoneType
;
584 mswr::ComPtr
<HandlerDoneType
> handler(mswr::Callback
<HandlerDoneType
>(
585 this, &FolderPickerSession::FolderPickerDone
));
586 DCHECK(handler
.Get() != NULL
);
587 hr
= completion
->put_Completed(handler
.Get());
591 HRESULT
FolderPickerSession::FolderPickerDone(FolderPickerAsyncOp
* async
,
592 AsyncStatus status
) {
593 if (status
== Completed
) {
594 mswr::ComPtr
<winstorage::IStorageFolder
> folder
;
595 HRESULT hr
= async
->GetResults(folder
.GetAddressOf());
598 mswr::ComPtr
<winstorage::IStorageItem
> storage_item
;
600 hr
= folder
.As(&storage_item
);
602 mswrw::HString file_path
;
604 hr
= storage_item
->get_Path(file_path
.GetAddressOf());
607 base::string16 path_str
= MakeStdWString(file_path
.Get());
612 LOG(ERROR
) << "NULL IStorageItem";
615 LOG(ERROR
) << "Unexpected async status " << static_cast<int>(status
);
617 app_view_
->OnFolderPickerCompleted(this, success_
);