1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/file_manager/url_util.h"
7 #include "base/json/json_writer.h"
8 #include "base/values.h"
9 #include "chrome/browser/chromeos/file_manager/app_id.h"
10 #include "net/base/escape.h"
12 namespace file_manager
{
16 // Returns a file manager URL for the given |path|.
17 GURL
GetFileManagerUrl(const char* path
) {
18 return GURL(std::string("chrome-extension://") + kFileManagerAppId
+ path
);
21 // Converts a numeric dialog type to a string.
22 std::string
GetDialogTypeAsString(
23 ui::SelectFileDialog::Type dialog_type
) {
25 switch (dialog_type
) {
26 case ui::SelectFileDialog::SELECT_NONE
:
27 type_str
= "full-page";
30 case ui::SelectFileDialog::SELECT_FOLDER
:
34 case ui::SelectFileDialog::SELECT_UPLOAD_FOLDER
:
35 type_str
= "upload-folder";
38 case ui::SelectFileDialog::SELECT_SAVEAS_FILE
:
39 type_str
= "saveas-file";
42 case ui::SelectFileDialog::SELECT_OPEN_FILE
:
43 type_str
= "open-file";
46 case ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE
:
47 type_str
= "open-multi-file";
59 GURL
GetFileManagerMainPageUrl() {
60 return GetFileManagerUrl("/main.html");
63 GURL
GetFileManagerMainPageUrlWithParams(
64 ui::SelectFileDialog::Type type
,
65 const base::string16
& title
,
66 const GURL
& current_directory_url
,
67 const GURL
& selection_url
,
68 const std::string
& target_name
,
69 const ui::SelectFileDialog::FileTypeInfo
* file_types
,
71 const base::FilePath::StringType
& default_extension
) {
72 base::DictionaryValue arg_value
;
73 arg_value
.SetString("type", GetDialogTypeAsString(type
));
74 arg_value
.SetString("title", title
);
75 arg_value
.SetString("currentDirectoryURL", current_directory_url
.spec());
76 arg_value
.SetString("selectionURL", selection_url
.spec());
77 arg_value
.SetString("targetName", target_name
);
78 arg_value
.SetString("defaultExtension", default_extension
);
81 base::ListValue
* types_list
= new base::ListValue();
82 for (size_t i
= 0; i
< file_types
->extensions
.size(); ++i
) {
83 base::ListValue
* extensions_list
= new base::ListValue();
84 for (size_t j
= 0; j
< file_types
->extensions
[i
].size(); ++j
) {
85 extensions_list
->Append(
86 new base::StringValue(file_types
->extensions
[i
][j
]));
89 base::DictionaryValue
* dict
= new base::DictionaryValue();
90 dict
->Set("extensions", extensions_list
);
92 if (i
< file_types
->extension_description_overrides
.size()) {
93 base::string16 desc
= file_types
->extension_description_overrides
[i
];
94 dict
->SetString("description", desc
);
97 // file_type_index is 1-based. 0 means no selection at all.
98 dict
->SetBoolean("selected",
99 (static_cast<size_t>(file_type_index
) == (i
+ 1)));
101 types_list
->Set(i
, dict
);
103 arg_value
.Set("typeList", types_list
);
105 arg_value
.SetBoolean("includeAllFiles", file_types
->include_all_files
);
108 // If the caller cannot handle Drive path, the file chooser dialog need to
109 // return resolved local native paths to the selected files.
110 arg_value
.SetBoolean("shouldReturnLocalPath",
111 !file_types
|| !file_types
->support_drive
);
113 std::string json_args
;
114 base::JSONWriter::Write(arg_value
, &json_args
);
116 std::string url
= GetFileManagerMainPageUrl().spec() + '?' +
117 net::EscapeUrlEncodedData(json_args
,
118 false); // Space to %20 instead of +.
123 } // namespace file_manager