summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/download_file_picker_chromeos.cc
blob: b51e84baa0dcec880b5e6a74095cf5133ad2e0e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/download/download_file_picker_chromeos.h"

#include "base/bind.h"
#include "base/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/chromeos/gdata/gdata_download_observer.h"
#include "chrome/browser/chromeos/gdata/gdata_file_system.h"
#include "chrome/browser/chromeos/gdata/gdata_system_service.h"
#include "chrome/browser/chromeos/gdata/gdata_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/download_manager.h"

using content::BrowserThread;
using content::DownloadManager;

namespace {

// Call FileSelected on |download_manager|.
void GDataTempFileSelected(DownloadManager* download_manager,
                           FilePath* file_path,
                           int32 download_id) {
  download_manager->FileSelected(*file_path, download_id);
}

}  // namespace

DownloadFilePickerChromeOS::DownloadFilePickerChromeOS(
    content::DownloadManager* download_manager,
    content::WebContents* web_contents,
    const FilePath& suggested_path,
    int32 download_id)
    : DownloadFilePicker(download_manager,
                         web_contents,
                         suggested_path,
                         download_id) {
}

DownloadFilePickerChromeOS::~DownloadFilePickerChromeOS() {
}

void DownloadFilePickerChromeOS::FileSelected(const FilePath& path,
                                              int index,
                                              void* params) {
  RecordFileSelected(path);

  if (download_manager_) {
    gdata::GDataSystemService* system_service =
        gdata::GDataSystemServiceFactory::GetForProfile(
            ProfileManager::GetDefaultProfile());
    if (system_service && gdata::util::IsUnderGDataMountPoint(path)) {
      // If we're trying to download a file into gdata, save path in external
      // data.
      content::DownloadItem* download =
          download_manager_->GetActiveDownloadItem(download_id_);
      if (download) {
        gdata::GDataDownloadObserver::SetGDataPath(download, path);
        download->SetDisplayName(path.BaseName());
        download->SetIsTemporary(true);

        const FilePath gdata_tmp_download_dir =
            system_service->file_system()->GetGDataTempDownloadFolderPath();

        // Swap the gdata path with a local path. Local path must be created
        // on the IO thread pool.
        FilePath* gdata_tmp_download_path(new FilePath());
        BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE,
            base::Bind(&gdata::GDataDownloadObserver::GetGDataTempDownloadPath,
                       gdata_tmp_download_dir,
                       gdata_tmp_download_path),
            base::Bind(&GDataTempFileSelected,
                       download_manager_,
                       base::Owned(gdata_tmp_download_path),
                       download_id_));
      }
    } else {
      download_manager_->FileSelected(path, download_id_);
    }
  }
  delete this;
}