OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 #include "chrome/browser/extensions/file_manager_util.h" |
| 5 |
| 6 #include "base/json/json_writer.h" |
| 7 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/metrics/user_metrics.h" |
| 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/browser_list.h" |
| 13 #include "third_party/libjingle/source/talk/base/urlencode.h" |
| 14 |
| 15 // This is the "well known" url for the file manager extension from |
| 16 // browser/resources/file_manager. In the future we may provide a way to swap |
| 17 // out this file manager for an aftermarket part, but not yet. |
| 18 const char kBaseFileBrowserUrl[] = |
| 19 "chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/main.html"; |
| 20 |
| 21 // static |
| 22 GURL FileManagerUtil::GetFileBrowserUrl() { |
| 23 return GURL(kBaseFileBrowserUrl); |
| 24 } |
| 25 |
| 26 // static |
| 27 GURL FileManagerUtil::GetFileBrowserUrlWithParams( |
| 28 SelectFileDialog::Type type, |
| 29 const string16& title, |
| 30 const FilePath& default_path, |
| 31 const SelectFileDialog::FileTypeInfo* file_types, |
| 32 int file_type_index, |
| 33 const FilePath::StringType& default_extension) { |
| 34 std::string json = GetArgumentsJson(type, title, default_path, file_types, |
| 35 file_type_index, default_extension); |
| 36 return GURL(FileManagerUtil::GetFileBrowserUrl().spec() + "?" + |
| 37 UrlEncodeStringWithoutEncodingSpaceAsPlus(json)); |
| 38 |
| 39 } |
| 40 // static |
| 41 void FileManagerUtil::ShowFullTabUrl(Profile* profile, |
| 42 const FilePath& default_path) { |
| 43 std::string json = GetArgumentsJson(SelectFileDialog::SELECT_NONE, string16(), |
| 44 default_path, NULL, 0, FilePath::StringType()); |
| 45 GURL url(std::string(kBaseFileBrowserUrl) + "?" + |
| 46 UrlEncodeStringWithoutEncodingSpaceAsPlus(json)); |
| 47 Browser* browser = BrowserList::GetLastActive(); |
| 48 if (!browser) |
| 49 return; |
| 50 |
| 51 UserMetrics::RecordAction(UserMetricsAction("ShowFileBrowserFullTab"), |
| 52 profile); |
| 53 browser->ShowSingletonTab(GURL(url)); |
| 54 } |
| 55 |
| 56 // static |
| 57 std::string FileManagerUtil::GetArgumentsJson( |
| 58 SelectFileDialog::Type type, |
| 59 const string16& title, |
| 60 const FilePath& default_path, |
| 61 const SelectFileDialog::FileTypeInfo* file_types, |
| 62 int file_type_index, |
| 63 const FilePath::StringType& default_extension) { |
| 64 DictionaryValue arg_value; |
| 65 arg_value.SetString("type", GetDialogTypeAsString(type)); |
| 66 arg_value.SetString("title", title); |
| 67 // TODO(zelidrag): Convert local system path into virtual path for File API. |
| 68 arg_value.SetString("defaultPath", default_path.value()); |
| 69 arg_value.SetString("defaultExtension", default_extension); |
| 70 |
| 71 ListValue* types_list = new ListValue(); |
| 72 |
| 73 if (file_types) { |
| 74 for (size_t i = 0; i < file_types->extensions.size(); ++i) { |
| 75 ListValue* extensions_list = new ListValue(); |
| 76 for (size_t j = 0; j < file_types->extensions[i].size(); ++j) { |
| 77 extensions_list->Set( |
| 78 i, Value::CreateStringValue(file_types->extensions[i][j])); |
| 79 } |
| 80 |
| 81 DictionaryValue* dict = new DictionaryValue(); |
| 82 dict->Set("extensions", extensions_list); |
| 83 |
| 84 if (i < file_types->extension_description_overrides.size()) { |
| 85 string16 desc = file_types->extension_description_overrides[i]; |
| 86 dict->SetString("description", desc); |
| 87 } |
| 88 |
| 89 dict->SetBoolean("selected", |
| 90 (static_cast<size_t>(file_type_index) == i)); |
| 91 |
| 92 types_list->Set(i, dict); |
| 93 } |
| 94 } |
| 95 |
| 96 std::string rv; |
| 97 base::JSONWriter::Write(&arg_value, false, &rv); |
| 98 |
| 99 return rv; |
| 100 } |
| 101 |
| 102 // static |
| 103 std::string FileManagerUtil::GetDialogTypeAsString( |
| 104 SelectFileDialog::Type dialog_type) { |
| 105 std::string type_str; |
| 106 switch (dialog_type) { |
| 107 case SelectFileDialog::SELECT_NONE: |
| 108 type_str = "none"; |
| 109 break; |
| 110 |
| 111 case SelectFileDialog::SELECT_FOLDER: |
| 112 type_str = "folder"; |
| 113 break; |
| 114 |
| 115 case SelectFileDialog::SELECT_SAVEAS_FILE: |
| 116 type_str = "saveas-file"; |
| 117 break; |
| 118 |
| 119 case SelectFileDialog::SELECT_OPEN_FILE: |
| 120 type_str = "open-file"; |
| 121 break; |
| 122 |
| 123 case SelectFileDialog::SELECT_OPEN_MULTI_FILE: |
| 124 type_str = "open-multi-file"; |
| 125 break; |
| 126 |
| 127 default: |
| 128 NOTREACHED(); |
| 129 } |
| 130 |
| 131 return type_str; |
| 132 } |
OLD | NEW |