Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_manager / url_util.cc
blob1f3e9d8674c8ad3bfb735bfd6bd0d21a03496391
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 {
13 namespace util {
14 namespace {
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) {
24 std::string type_str;
25 switch (dialog_type) {
26 case ui::SelectFileDialog::SELECT_NONE:
27 type_str = "full-page";
28 break;
30 case ui::SelectFileDialog::SELECT_FOLDER:
31 type_str = "folder";
32 break;
34 case ui::SelectFileDialog::SELECT_UPLOAD_FOLDER:
35 type_str = "upload-folder";
36 break;
38 case ui::SelectFileDialog::SELECT_SAVEAS_FILE:
39 type_str = "saveas-file";
40 break;
42 case ui::SelectFileDialog::SELECT_OPEN_FILE:
43 type_str = "open-file";
44 break;
46 case ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE:
47 type_str = "open-multi-file";
48 break;
50 default:
51 NOTREACHED();
54 return type_str;
57 } // namespace
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,
70 int file_type_index,
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);
80 if (file_types) {
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 +.
119 return GURL(url);
122 } // namespace util
123 } // namespace file_manager