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" | |
17 #include "content/browser/browser_thread.h" | 18 #include "content/browser/browser_thread.h" |
18 #include "content/browser/user_metrics.h" | 19 #include "content/browser/user_metrics.h" |
19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
20 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
21 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
22 #include "webkit/fileapi/file_system_context.h" | 23 #include "webkit/fileapi/file_system_context.h" |
23 #include "webkit/fileapi/file_system_mount_point_provider.h" | 24 #include "webkit/fileapi/file_system_mount_point_provider.h" |
24 #include "webkit/fileapi/file_system_util.h" | 25 #include "webkit/fileapi/file_system_util.h" |
25 | 26 |
26 #define FILEBROWSER_URL(PATH) \ | 27 #define FILEBROWSER_URL(PATH) \ |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 } | 146 } |
146 | 147 |
147 // static | 148 // static |
148 GURL FileManagerUtil::GetFileBrowserUrlWithParams( | 149 GURL FileManagerUtil::GetFileBrowserUrlWithParams( |
149 SelectFileDialog::Type type, | 150 SelectFileDialog::Type type, |
150 const string16& title, | 151 const string16& title, |
151 const FilePath& default_virtual_path, | 152 const FilePath& default_virtual_path, |
152 const SelectFileDialog::FileTypeInfo* file_types, | 153 const SelectFileDialog::FileTypeInfo* file_types, |
153 int file_type_index, | 154 int file_type_index, |
154 const FilePath::StringType& default_extension) { | 155 const FilePath::StringType& default_extension) { |
155 std::string json = GetArgumentsJson(type, title, default_virtual_path, | 156 DictionaryValue arg_value; |
Vladislav Kaznacheev
2011/08/05 13:01:36
Any special reason to inline the helper function?
SeRya
2011/08/05 13:45:42
This function is not used anywhere else anymore an
| |
156 file_types, file_type_index, | 157 arg_value.SetString("type", GetDialogTypeAsString(type)); |
157 default_extension); | 158 arg_value.SetString("title", title); |
158 return GURL(FileManagerUtil::GetFileBrowserUrl().spec() + "?" + | 159 arg_value.SetString("defaultPath", default_virtual_path.value()); |
159 EscapeUrlEncodedData(json, false)); | 160 arg_value.SetString("defaultExtension", default_extension); |
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); | |
160 | 195 |
161 } | 196 } |
162 | 197 |
163 // static | 198 // static |
164 void FileManagerUtil::ShowFullTabUrl(Profile*, | 199 void FileManagerUtil::ShowFullTabUrl(Profile*, |
165 const FilePath& default_path) { | 200 const FilePath& dir) { |
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)); | |
170 Browser* browser = BrowserList::GetLastActive(); | 201 Browser* browser = BrowserList::GetLastActive(); |
171 if (!browser) | 202 if (!browser) |
172 return; | 203 return; |
173 | 204 |
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 | |
174 UserMetrics::RecordAction(UserMetricsAction("ShowFileBrowserFullTab")); | 215 UserMetrics::RecordAction(UserMetricsAction("ShowFileBrowserFullTab")); |
175 browser->ShowSingletonTab(GURL(url)); | 216 browser->ShowSingletonTab(GURL(url)); |
176 } | 217 } |
177 | 218 |
178 void FileManagerUtil::ViewItem(const FilePath& full_path, bool enqueue) { | 219 void FileManagerUtil::ViewItem(const FilePath& full_path, bool enqueue) { |
179 std::string ext = full_path.Extension(); | 220 std::string ext = full_path.Extension(); |
180 // For things supported natively by the browser, we should open it | 221 // For things supported natively by the browser, we should open it |
181 // in a tab. | 222 // in a tab. |
182 if (IsSupportedBrowserExtension(ext.data())) { | 223 if (IsSupportedBrowserExtension(ext.data())) { |
183 std::string path; | 224 std::string path; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 NewRunnableFunction( | 261 NewRunnableFunction( |
221 &platform_util::SimpleErrorBox, | 262 &platform_util::SimpleErrorBox, |
222 static_cast<gfx::NativeWindow>(NULL), | 263 static_cast<gfx::NativeWindow>(NULL), |
223 l10n_util::GetStringUTF16(IDS_FILEBROWSER_ERROR_TITLE), | 264 l10n_util::GetStringUTF16(IDS_FILEBROWSER_ERROR_TITLE), |
224 l10n_util::GetStringFUTF16(IDS_FILEBROWSER_ERROR_UNKNOWN_FILE_TYPE, | 265 l10n_util::GetStringFUTF16(IDS_FILEBROWSER_ERROR_UNKNOWN_FILE_TYPE, |
225 UTF8ToUTF16(full_path.BaseName().value())) | 266 UTF8ToUTF16(full_path.BaseName().value())) |
226 )); | 267 )); |
227 } | 268 } |
228 | 269 |
229 // static | 270 // 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 | |
277 std::string FileManagerUtil::GetDialogTypeAsString( | 271 std::string FileManagerUtil::GetDialogTypeAsString( |
278 SelectFileDialog::Type dialog_type) { | 272 SelectFileDialog::Type dialog_type) { |
279 std::string type_str; | 273 std::string type_str; |
280 switch (dialog_type) { | 274 switch (dialog_type) { |
281 case SelectFileDialog::SELECT_NONE: | 275 case SelectFileDialog::SELECT_NONE: |
282 type_str = "full-page"; | 276 type_str = "full-page"; |
283 break; | 277 break; |
284 | 278 |
285 case SelectFileDialog::SELECT_FOLDER: | 279 case SelectFileDialog::SELECT_FOLDER: |
286 type_str = "folder"; | 280 type_str = "folder"; |
(...skipping 10 matching lines...) Expand all Loading... | |
297 case SelectFileDialog::SELECT_OPEN_MULTI_FILE: | 291 case SelectFileDialog::SELECT_OPEN_MULTI_FILE: |
298 type_str = "open-multi-file"; | 292 type_str = "open-multi-file"; |
299 break; | 293 break; |
300 | 294 |
301 default: | 295 default: |
302 NOTREACHED(); | 296 NOTREACHED(); |
303 } | 297 } |
304 | 298 |
305 return type_str; | 299 return type_str; |
306 } | 300 } |
OLD | NEW |