Home | History | Annotate | Download | only in file_manager
      1 // Copyright 2013 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/extensions/file_manager/private_api_dialog.h"
      6 
      7 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h"
      8 #include "chrome/browser/ui/views/select_file_dialog_extension.h"
      9 #include "chrome/common/extensions/api/file_browser_private.h"
     10 #include "content/public/browser/browser_thread.h"
     11 #include "ui/shell_dialogs/selected_file_info.h"
     12 
     13 using content::BrowserThread;
     14 
     15 namespace extensions {
     16 
     17 namespace {
     18 
     19 // Computes the routing ID for SelectFileDialogExtension from the |function|.
     20 SelectFileDialogExtension::RoutingID GetFileDialogRoutingID(
     21     ChromeAsyncExtensionFunction* function) {
     22   return SelectFileDialogExtension::GetRoutingIDFromWebContents(
     23       function->GetAssociatedWebContents());
     24 }
     25 
     26 }  // namespace
     27 
     28 bool FileBrowserPrivateCancelDialogFunction::RunAsync() {
     29   SelectFileDialogExtension::OnFileSelectionCanceled(
     30       GetFileDialogRoutingID(this));
     31   SendResponse(true);
     32   return true;
     33 }
     34 
     35 bool FileBrowserPrivateSelectFileFunction::RunAsync() {
     36   using extensions::api::file_browser_private::SelectFile::Params;
     37   const scoped_ptr<Params> params(Params::Create(*args_));
     38   EXTENSION_FUNCTION_VALIDATE(params);
     39 
     40   std::vector<GURL> file_paths;
     41   file_paths.push_back(GURL(params->selected_path));
     42 
     43   file_manager::util::GetSelectedFileInfoLocalPathOption option =
     44       file_manager::util::NO_LOCAL_PATH_RESOLUTION;
     45   if (params->should_return_local_path) {
     46     option = params->for_opening ?
     47         file_manager::util::NEED_LOCAL_PATH_FOR_OPENING :
     48         file_manager::util::NEED_LOCAL_PATH_FOR_SAVING;
     49   }
     50 
     51   file_manager::util::GetSelectedFileInfo(
     52       render_view_host(),
     53       GetProfile(),
     54       file_paths,
     55       option,
     56       base::Bind(
     57           &FileBrowserPrivateSelectFileFunction::GetSelectedFileInfoResponse,
     58           this,
     59           params->index));
     60   return true;
     61 }
     62 
     63 void FileBrowserPrivateSelectFileFunction::GetSelectedFileInfoResponse(
     64     int index,
     65     const std::vector<ui::SelectedFileInfo>& files) {
     66   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     67   if (files.size() != 1) {
     68     SendResponse(false);
     69     return;
     70   }
     71   SelectFileDialogExtension::OnFileSelected(GetFileDialogRoutingID(this),
     72                                             files[0], index);
     73   SendResponse(true);
     74 }
     75 
     76 bool FileBrowserPrivateSelectFilesFunction::RunAsync() {
     77   using extensions::api::file_browser_private::SelectFiles::Params;
     78   const scoped_ptr<Params> params(Params::Create(*args_));
     79   EXTENSION_FUNCTION_VALIDATE(params);
     80 
     81   std::vector<GURL> file_urls;
     82   for (size_t i = 0; i < params->selected_paths.size(); ++i)
     83     file_urls.push_back(GURL(params->selected_paths[i]));
     84 
     85   file_manager::util::GetSelectedFileInfo(
     86       render_view_host(),
     87       GetProfile(),
     88       file_urls,
     89       params->should_return_local_path ?
     90           file_manager::util::NEED_LOCAL_PATH_FOR_OPENING :
     91           file_manager::util::NO_LOCAL_PATH_RESOLUTION,
     92       base::Bind(
     93           &FileBrowserPrivateSelectFilesFunction::GetSelectedFileInfoResponse,
     94           this));
     95   return true;
     96 }
     97 
     98 void FileBrowserPrivateSelectFilesFunction::GetSelectedFileInfoResponse(
     99     const std::vector<ui::SelectedFileInfo>& files) {
    100   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    101   SelectFileDialogExtension::OnMultiFilesSelected(GetFileDialogRoutingID(this),
    102                                                   files);
    103   SendResponse(true);
    104 }
    105 
    106 }  // namespace extensions
    107