OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 #include "chrome/browser/extensions/file_manager_util.h" | 4 #include "chrome/browser/extensions/file_manager_util.h" |
5 | 5 |
6 #include "base/json/json_writer.h" | 6 #include "base/json/json_writer.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/chromeos/media/media_player.h" | 12 #include "chrome/browser/chromeos/media/media_player.h" |
13 #include "chrome/browser/platform_util.h" | 13 #include "chrome/browser/platform_util.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_list.h" | 16 #include "chrome/browser/ui/browser_list.h" |
17 #include "chrome/common/url_constants.h" | |
18 #include "content/browser/browser_thread.h" | 17 #include "content/browser/browser_thread.h" |
19 #include "content/browser/user_metrics.h" | 18 #include "content/browser/user_metrics.h" |
20 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
21 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
22 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
23 #include "webkit/fileapi/file_system_context.h" | 22 #include "webkit/fileapi/file_system_context.h" |
24 #include "webkit/fileapi/file_system_mount_point_provider.h" | 23 #include "webkit/fileapi/file_system_mount_point_provider.h" |
25 #include "webkit/fileapi/file_system_util.h" | 24 #include "webkit/fileapi/file_system_util.h" |
26 | 25 |
27 #define FILEBROWSER_URL(PATH) \ | 26 #define FILEBROWSER_URL(PATH) \ |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 } | 145 } |
147 | 146 |
148 // static | 147 // static |
149 GURL FileManagerUtil::GetFileBrowserUrlWithParams( | 148 GURL FileManagerUtil::GetFileBrowserUrlWithParams( |
150 SelectFileDialog::Type type, | 149 SelectFileDialog::Type type, |
151 const string16& title, | 150 const string16& title, |
152 const FilePath& default_virtual_path, | 151 const FilePath& default_virtual_path, |
153 const SelectFileDialog::FileTypeInfo* file_types, | 152 const SelectFileDialog::FileTypeInfo* file_types, |
154 int file_type_index, | 153 int file_type_index, |
155 const FilePath::StringType& default_extension) { | 154 const FilePath::StringType& default_extension) { |
156 DictionaryValue arg_value; | 155 std::string json = GetArgumentsJson(type, title, default_virtual_path, |
157 arg_value.SetString("type", GetDialogTypeAsString(type)); | 156 file_types, file_type_index, |
158 arg_value.SetString("title", title); | 157 default_extension); |
159 arg_value.SetString("defaultPath", default_virtual_path.value()); | 158 return GURL(FileManagerUtil::GetFileBrowserUrl().spec() + "?" + |
160 arg_value.SetString("defaultExtension", default_extension); | 159 EscapeUrlEncodedData(json, false)); |
161 | |
162 if (file_types) { | |
163 ListValue* types_list = new ListValue(); | |
164 for (size_t i = 0; i < file_types->extensions.size(); ++i) { | |
165 ListValue* extensions_list = new ListValue(); | |
166 for (size_t j = 0; j < file_types->extensions[i].size(); ++j) { | |
167 extensions_list->Set( | |
168 i, Value::CreateStringValue(file_types->extensions[i][j])); | |
169 } | |
170 | |
171 DictionaryValue* dict = new DictionaryValue(); | |
172 dict->Set("extensions", extensions_list); | |
173 | |
174 if (i < file_types->extension_description_overrides.size()) { | |
175 string16 desc = file_types->extension_description_overrides[i]; | |
176 dict->SetString("description", desc); | |
177 } | |
178 | |
179 dict->SetBoolean("selected", | |
180 (static_cast<size_t>(file_type_index) == i)); | |
181 | |
182 types_list->Set(i, dict); | |
183 } | |
184 arg_value.Set("typeList", types_list); | |
185 } | |
186 | |
187 std::string json_args; | |
188 base::JSONWriter::Write(&arg_value, false, &json_args); | |
189 | |
190 // kChromeUIFileManagerURL could not be used since query parameters are not | |
191 // supported for it. | |
192 std::string url = FileManagerUtil::GetFileBrowserUrl().spec() + | |
193 '?' + EscapeUrlEncodedData(json_args, false); | |
194 return GURL(url); | |
195 | 160 |
196 } | 161 } |
197 | 162 |
198 // static | 163 // static |
199 void FileManagerUtil::ShowFullTabUrl(Profile*, | 164 void FileManagerUtil::ShowFullTabUrl(Profile*, |
200 const FilePath& dir) { | 165 const FilePath& default_path) { |
| 166 std::string json = GetArgumentsJson(SelectFileDialog::SELECT_NONE, string16(), |
| 167 default_path, NULL, 0, FilePath::StringType()); |
| 168 GURL url(std::string(kBaseFileBrowserUrl) + "?" + |
| 169 EscapeUrlEncodedData(json, false)); |
201 Browser* browser = BrowserList::GetLastActive(); | 170 Browser* browser = BrowserList::GetLastActive(); |
202 if (!browser) | 171 if (!browser) |
203 return; | 172 return; |
204 | 173 |
205 FilePath virtual_path; | |
206 if (!FileManagerUtil::ConvertFileToRelativeFileSystemPath(browser->profile(), | |
207 dir, | |
208 &virtual_path)) { | |
209 return; | |
210 } | |
211 | |
212 std::string url = chrome::kChromeUIFileManagerURL; | |
213 url += '#' + EscapeUrlEncodedData(virtual_path.value(), false); | |
214 | |
215 UserMetrics::RecordAction(UserMetricsAction("ShowFileBrowserFullTab")); | 174 UserMetrics::RecordAction(UserMetricsAction("ShowFileBrowserFullTab")); |
216 browser->ShowSingletonTab(GURL(url)); | 175 browser->ShowSingletonTab(GURL(url)); |
217 } | 176 } |
218 | 177 |
219 void FileManagerUtil::ViewItem(const FilePath& full_path, bool enqueue) { | 178 void FileManagerUtil::ViewItem(const FilePath& full_path, bool enqueue) { |
220 std::string ext = full_path.Extension(); | 179 std::string ext = full_path.Extension(); |
221 // For things supported natively by the browser, we should open it | 180 // For things supported natively by the browser, we should open it |
222 // in a tab. | 181 // in a tab. |
223 if (IsSupportedBrowserExtension(ext.data())) { | 182 if (IsSupportedBrowserExtension(ext.data())) { |
224 std::string path; | 183 std::string path; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 NewRunnableFunction( | 220 NewRunnableFunction( |
262 &platform_util::SimpleErrorBox, | 221 &platform_util::SimpleErrorBox, |
263 static_cast<gfx::NativeWindow>(NULL), | 222 static_cast<gfx::NativeWindow>(NULL), |
264 l10n_util::GetStringUTF16(IDS_FILEBROWSER_ERROR_TITLE), | 223 l10n_util::GetStringUTF16(IDS_FILEBROWSER_ERROR_TITLE), |
265 l10n_util::GetStringFUTF16(IDS_FILEBROWSER_ERROR_UNKNOWN_FILE_TYPE, | 224 l10n_util::GetStringFUTF16(IDS_FILEBROWSER_ERROR_UNKNOWN_FILE_TYPE, |
266 UTF8ToUTF16(full_path.BaseName().value())) | 225 UTF8ToUTF16(full_path.BaseName().value())) |
267 )); | 226 )); |
268 } | 227 } |
269 | 228 |
270 // static | 229 // static |
| 230 std::string FileManagerUtil::GetArgumentsJson( |
| 231 SelectFileDialog::Type type, |
| 232 const string16& title, |
| 233 const FilePath& default_virtual_path, |
| 234 const SelectFileDialog::FileTypeInfo* file_types, |
| 235 int file_type_index, |
| 236 const FilePath::StringType& default_extension) { |
| 237 DictionaryValue arg_value; |
| 238 arg_value.SetString("type", GetDialogTypeAsString(type)); |
| 239 arg_value.SetString("title", title); |
| 240 // TODO(zelidrag): Convert local system path into virtual path for File API. |
| 241 arg_value.SetString("defaultPath", default_virtual_path.value()); |
| 242 arg_value.SetString("defaultExtension", default_extension); |
| 243 |
| 244 |
| 245 if (file_types) { |
| 246 ListValue* types_list = new ListValue(); |
| 247 for (size_t i = 0; i < file_types->extensions.size(); ++i) { |
| 248 ListValue* extensions_list = new ListValue(); |
| 249 for (size_t j = 0; j < file_types->extensions[i].size(); ++j) { |
| 250 extensions_list->Set( |
| 251 i, Value::CreateStringValue(file_types->extensions[i][j])); |
| 252 } |
| 253 |
| 254 DictionaryValue* dict = new DictionaryValue(); |
| 255 dict->Set("extensions", extensions_list); |
| 256 |
| 257 if (i < file_types->extension_description_overrides.size()) { |
| 258 string16 desc = file_types->extension_description_overrides[i]; |
| 259 dict->SetString("description", desc); |
| 260 } |
| 261 |
| 262 dict->SetBoolean("selected", |
| 263 (static_cast<size_t>(file_type_index) == i)); |
| 264 |
| 265 types_list->Set(i, dict); |
| 266 } |
| 267 arg_value.Set("typeList", types_list); |
| 268 } |
| 269 |
| 270 std::string rv; |
| 271 base::JSONWriter::Write(&arg_value, false, &rv); |
| 272 |
| 273 return rv; |
| 274 } |
| 275 |
| 276 // static |
271 std::string FileManagerUtil::GetDialogTypeAsString( | 277 std::string FileManagerUtil::GetDialogTypeAsString( |
272 SelectFileDialog::Type dialog_type) { | 278 SelectFileDialog::Type dialog_type) { |
273 std::string type_str; | 279 std::string type_str; |
274 switch (dialog_type) { | 280 switch (dialog_type) { |
275 case SelectFileDialog::SELECT_NONE: | 281 case SelectFileDialog::SELECT_NONE: |
276 type_str = "full-page"; | 282 type_str = "full-page"; |
277 break; | 283 break; |
278 | 284 |
279 case SelectFileDialog::SELECT_FOLDER: | 285 case SelectFileDialog::SELECT_FOLDER: |
280 type_str = "folder"; | 286 type_str = "folder"; |
(...skipping 10 matching lines...) Expand all Loading... |
291 case SelectFileDialog::SELECT_OPEN_MULTI_FILE: | 297 case SelectFileDialog::SELECT_OPEN_MULTI_FILE: |
292 type_str = "open-multi-file"; | 298 type_str = "open-multi-file"; |
293 break; | 299 break; |
294 | 300 |
295 default: | 301 default: |
296 NOTREACHED(); | 302 NOTREACHED(); |
297 } | 303 } |
298 | 304 |
299 return type_str; | 305 return type_str; |
300 } | 306 } |
OLD | NEW |