Home | History | Annotate | Download | only in file_manager
      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.
      4 
      5 #include "chrome/browser/chromeos/file_manager/url_util.h"
      6 
      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"
     11 
     12 namespace file_manager {
     13 namespace util {
     14 namespace {
     15 
     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);
     19 }
     20 
     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;
     29 
     30     case ui::SelectFileDialog::SELECT_FOLDER:
     31       type_str = "folder";
     32       break;
     33 
     34     case ui::SelectFileDialog::SELECT_UPLOAD_FOLDER:
     35       type_str = "upload-folder";
     36       break;
     37 
     38     case ui::SelectFileDialog::SELECT_SAVEAS_FILE:
     39       type_str = "saveas-file";
     40       break;
     41 
     42     case ui::SelectFileDialog::SELECT_OPEN_FILE:
     43       type_str = "open-file";
     44       break;
     45 
     46     case ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE:
     47       type_str = "open-multi-file";
     48       break;
     49 
     50     default:
     51       NOTREACHED();
     52   }
     53 
     54   return type_str;
     55 }
     56 
     57 }  // namespace
     58 
     59 GURL GetFileManagerBaseUrl() {
     60   return GetFileManagerUrl("/");
     61 }
     62 
     63 GURL GetFileManagerMainPageUrl() {
     64   return GetFileManagerUrl("/main.html");
     65 }
     66 
     67 GURL GetFileManagerMainPageUrlWithParams(
     68     ui::SelectFileDialog::Type type,
     69     const base::string16& title,
     70     const base::FilePath& default_virtual_path,
     71     const ui::SelectFileDialog::FileTypeInfo* file_types,
     72     int file_type_index,
     73     const base::FilePath::StringType& default_extension) {
     74   base::DictionaryValue arg_value;
     75   arg_value.SetString("type", GetDialogTypeAsString(type));
     76   arg_value.SetString("title", title);
     77   arg_value.SetString("defaultPath", default_virtual_path.value());
     78   arg_value.SetString("defaultExtension", default_extension);
     79 
     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]));
     87       }
     88 
     89       base::DictionaryValue* dict = new base::DictionaryValue();
     90       dict->Set("extensions", extensions_list);
     91 
     92       if (i < file_types->extension_description_overrides.size()) {
     93         base::string16 desc = file_types->extension_description_overrides[i];
     94         dict->SetString("description", desc);
     95       }
     96 
     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)));
    100 
    101       types_list->Set(i, dict);
    102     }
    103     arg_value.Set("typeList", types_list);
    104 
    105     arg_value.SetBoolean("includeAllFiles", file_types->include_all_files);
    106   }
    107 
    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);
    112 
    113   std::string json_args;
    114   base::JSONWriter::Write(&arg_value, &json_args);
    115 
    116   std::string url = GetFileManagerMainPageUrl().spec() + '?' +
    117       net::EscapeUrlEncodedData(json_args,
    118                                 false);  // Space to %20 instead of +.
    119   return GURL(url);
    120 }
    121 
    122 }  // namespace util
    123 }  // namespace file_manager
    124