1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/file_manager/url_util.h"
#include <stddef.h>
#include "base/json/json_writer.h"
#include "base/values.h"
#include "chrome/browser/chromeos/file_manager/app_id.h"
#include "net/base/escape.h"
namespace file_manager {
namespace util {
namespace {
const char kAllowedPaths[] = "allowedPaths";
const char kNativePath[] = "nativePath";
const char kNativeOrDrivePath[] = "nativeOrDrivePath";
const char kAnyPath[] = "anyPath";
// Returns a file manager URL for the given |path|.
GURL GetFileManagerUrl(const char* path) {
return GURL(std::string("chrome-extension://") + kFileManagerAppId + path);
}
// Converts a numeric dialog type to a string.
std::string GetDialogTypeAsString(
ui::SelectFileDialog::Type dialog_type) {
std::string type_str;
switch (dialog_type) {
case ui::SelectFileDialog::SELECT_NONE:
type_str = "full-page";
break;
case ui::SelectFileDialog::SELECT_FOLDER:
type_str = "folder";
break;
case ui::SelectFileDialog::SELECT_UPLOAD_FOLDER:
type_str = "upload-folder";
break;
case ui::SelectFileDialog::SELECT_SAVEAS_FILE:
type_str = "saveas-file";
break;
case ui::SelectFileDialog::SELECT_OPEN_FILE:
type_str = "open-file";
break;
case ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE:
type_str = "open-multi-file";
break;
default:
NOTREACHED();
}
return type_str;
}
} // namespace
GURL GetFileManagerMainPageUrl() {
return GetFileManagerUrl("/main.html");
}
GURL GetFileManagerMainPageUrlWithParams(
ui::SelectFileDialog::Type type,
const base::string16& title,
const GURL& current_directory_url,
const GURL& selection_url,
const std::string& target_name,
const ui::SelectFileDialog::FileTypeInfo* file_types,
int file_type_index,
const base::FilePath::StringType& default_extension) {
base::DictionaryValue arg_value;
arg_value.SetString("type", GetDialogTypeAsString(type));
arg_value.SetString("title", title);
arg_value.SetString("currentDirectoryURL", current_directory_url.spec());
arg_value.SetString("selectionURL", selection_url.spec());
arg_value.SetString("targetName", target_name);
arg_value.SetString("defaultExtension", default_extension);
if (file_types) {
base::ListValue* types_list = new base::ListValue();
for (size_t i = 0; i < file_types->extensions.size(); ++i) {
base::ListValue* extensions_list = new base::ListValue();
for (size_t j = 0; j < file_types->extensions[i].size(); ++j) {
extensions_list->Append(
new base::StringValue(file_types->extensions[i][j]));
}
base::DictionaryValue* dict = new base::DictionaryValue();
dict->Set("extensions", extensions_list);
if (i < file_types->extension_description_overrides.size()) {
base::string16 desc = file_types->extension_description_overrides[i];
dict->SetString("description", desc);
}
// file_type_index is 1-based. 0 means no selection at all.
dict->SetBoolean("selected",
(static_cast<size_t>(file_type_index) == (i + 1)));
types_list->Set(i, dict);
}
arg_value.Set("typeList", types_list);
arg_value.SetBoolean("includeAllFiles", file_types->include_all_files);
}
// If the caller cannot handle Drive path, the file chooser dialog need to
// return resolved local native paths to the selected files.
if (file_types) {
switch (file_types->allowed_paths) {
case ui::SelectFileDialog::FileTypeInfo::NATIVE_PATH:
arg_value.SetString(kAllowedPaths, kNativePath);
break;
case ui::SelectFileDialog::FileTypeInfo::NATIVE_OR_DRIVE_PATH:
arg_value.SetString(kAllowedPaths, kNativeOrDrivePath);
break;
case ui::SelectFileDialog::FileTypeInfo::ANY_PATH:
arg_value.SetString(kAllowedPaths, kAnyPath);
break;
}
} else {
arg_value.SetString(kAllowedPaths, kNativePath);
}
std::string json_args;
base::JSONWriter::Write(arg_value, &json_args);
std::string url = GetFileManagerMainPageUrl().spec() + '?' +
net::EscapeUrlEncodedData(json_args,
false); // Space to %20 instead of +.
return GURL(url);
}
} // namespace util
} // namespace file_manager
|