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
GetFileManagerBaseUrl() {
60 return GetFileManagerUrl("/");
63 GURL
GetFileManagerMainPageUrl() {
64 return GetFileManagerUrl("/main.html");
67 GURL
GetFileManagerMainPageUrlWithParams(
68 ui::SelectFileDialog::Type type
,
69 const base::string16
& title
,
70 const GURL
& current_directory_url
,
71 const GURL
& selection_url
,
72 const std::string
& target_name
,
73 const ui::SelectFileDialog::FileTypeInfo
* file_types
,
75 const base::FilePath::StringType
& default_extension
) {
76 base::DictionaryValue arg_value
;
77 arg_value
.SetString("type", GetDialogTypeAsString(type
));
78 arg_value
.SetString("title", title
);
79 arg_value
.SetString("currentDirectoryURL", current_directory_url
.spec());
80 arg_value
.SetString("selectionURL", selection_url
.spec());
81 arg_value
.SetString("targetName", target_name
);
82 arg_value
.SetString("defaultExtension", default_extension
);
85 base::ListValue
* types_list
= new base::ListValue();
86 for (size_t i
= 0; i
< file_types
->extensions
.size(); ++i
) {
87 base::ListValue
* extensions_list
= new base::ListValue();
88 for (size_t j
= 0; j
< file_types
->extensions
[i
].size(); ++j
) {
89 extensions_list
->Append(
90 new base::StringValue(file_types
->extensions
[i
][j
]));
93 base::DictionaryValue
* dict
= new base::DictionaryValue();
94 dict
->Set("extensions", extensions_list
);
96 if (i
< file_types
->extension_description_overrides
.size()) {
97 base::string16 desc
= file_types
->extension_description_overrides
[i
];
98 dict
->SetString("description", desc
);
101 // file_type_index is 1-based. 0 means no selection at all.
102 dict
->SetBoolean("selected",
103 (static_cast<size_t>(file_type_index
) == (i
+ 1)));
105 types_list
->Set(i
, dict
);
107 arg_value
.Set("typeList", types_list
);
109 arg_value
.SetBoolean("includeAllFiles", file_types
->include_all_files
);
112 // If the caller cannot handle Drive path, the file chooser dialog need to
113 // return resolved local native paths to the selected files.
114 arg_value
.SetBoolean("shouldReturnLocalPath",
115 !file_types
|| !file_types
->support_drive
);
117 std::string json_args
;
118 base::JSONWriter::Write(&arg_value
, &json_args
);
120 std::string url
= GetFileManagerMainPageUrl().spec() + '?' +
121 net::EscapeUrlEncodedData(json_args
,
122 false); // Space to %20 instead of +.
127 } // namespace file_manager