summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/file_manager/snapshot_manager.cc
blob: dae641bbbc00e86abb7422d7b04d0075d42c1c62 (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
88
89
90
91
92
93
94
95
96
// Copyright 2014 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/chromeos/file_manager/snapshot_manager.h"

#include "base/bind.h"
#include "chrome/browser/chromeos/file_manager/app_id.h"
#include "chrome/browser/chromeos/file_manager/fileapi_util.h"
#include "content/public/browser/browser_thread.h"
#include "google_apis/drive/task_util.h"
#include "webkit/browser/fileapi/file_system_context.h"
#include "webkit/common/blob/shareable_file_reference.h"

namespace file_manager {
namespace {

// Part of CreateManagedSnapshot. Runs CreateSnapshotFile method of fileapi.
void CreateSnapshotFileOnIOThread(
    scoped_refptr<fileapi::FileSystemContext> context,
    const fileapi::FileSystemURL& url,
    const fileapi::FileSystemOperation::SnapshotFileCallback& callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
  context->operation_runner()->CreateSnapshotFile(url, callback);
}

// Utility for destructing the bound |file_refs| on IO thread. This is meant
// to be used together with base::Bind. After this function finishes, the
// Bind callback should destruct the bound argument.
void FreeReferenceOnIOThread(const std::vector<
    scoped_refptr<webkit_blob::ShareableFileReference> >& file_refs) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
}

}  // namespace

SnapshotManager::SnapshotManager(Profile* profile)
    : profile_(profile), weak_ptr_factory_(this) {
}

SnapshotManager::~SnapshotManager() {
  if (!file_refs_.empty()) {
    bool posted = content::BrowserThread::PostTask(
        content::BrowserThread::IO,
        FROM_HERE,
        base::Bind(&FreeReferenceOnIOThread, file_refs_));
    DCHECK(posted);
  }
}

void SnapshotManager::CreateManagedSnapshot(
    const base::FilePath& absolute_file_path,
    const LocalPathCallback& callback) {
  fileapi::FileSystemContext* context =
      util::GetFileSystemContextForExtensionId(profile_, kFileManagerAppId);
  DCHECK(context);

  GURL url;
  if (!util::ConvertAbsoluteFilePathToFileSystemUrl(
          profile_, absolute_file_path, kFileManagerAppId, &url)) {
    callback.Run(base::FilePath());
    return;
  }

  // TODO(kinaba): crbug.com/386519 evict old |file_refs_| before creating a new
  // one if necessary.
  content::BrowserThread::PostTask(
      content::BrowserThread::IO,
      FROM_HERE,
      base::Bind(&CreateSnapshotFileOnIOThread,
                 make_scoped_refptr(context),
                 context->CrackURL(url),
                 google_apis::CreateRelayCallback(
                     base::Bind(&SnapshotManager::OnCreateSnapshotFile,
                                weak_ptr_factory_.GetWeakPtr(),
                                callback))));
}

void SnapshotManager::OnCreateSnapshotFile(
    const LocalPathCallback& callback,
    base::File::Error result,
    const base::File::Info& file_info,
    const base::FilePath& platform_path,
    const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (result != base::File::FILE_OK) {
    callback.Run(base::FilePath());
    return;
  }

  file_refs_.push_back(file_ref);
  callback.Run(platform_path);
}

}  // namespace file_manager