summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/file_write_helper.cc
blob: 8b890b6ae56ba31db1f2296cf9d23abff6c14ea9 (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
97
98
99
100
101
102
103
104
105
// 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/chromeos/drive/file_write_helper.h"

#include "base/threading/sequenced_worker_pool.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace drive {

namespace {

// Emits debug log when DriveFileSystem::CloseFile() is complete.
void EmitDebugLogForCloseFile(const FilePath& file_path,
                              DriveFileError file_error) {
  if (file_error != DRIVE_FILE_OK) {
    LOG(WARNING) << "CloseFile failed: " << file_path.AsUTF8Unsafe() << ": "
                 << file_error;
  }
}

}  // namespace

FileWriteHelper::FileWriteHelper(DriveFileSystemInterface* file_system)
    : file_system_(file_system),
      ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
  // Must be created in DriveSystemService.
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}

FileWriteHelper::~FileWriteHelper() {
  // Must be destroyed in DriveSystemService.
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}

void FileWriteHelper::PrepareWritableFileAndRun(
    const FilePath& file_path,
    const OpenFileCallback& callback) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(!callback.is_null());

  file_system_->CreateFile(
      file_path,
      false,  // it is not an error, even if the path already exists.
      base::Bind(&FileWriteHelper::PrepareWritableFileAndRunAfterCreateFile,
                 weak_ptr_factory_.GetWeakPtr(),
                 file_path,
                 callback));
}

void FileWriteHelper::PrepareWritableFileAndRunAfterCreateFile(
    const FilePath& file_path,
    const OpenFileCallback& callback,
    DriveFileError error) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(!callback.is_null());

  if (error != DRIVE_FILE_OK) {
    content::BrowserThread::GetBlockingPool()->PostTask(
        FROM_HERE,
        base::Bind(callback, error, FilePath()));
    return;
  }
  file_system_->OpenFile(
      file_path,
      base::Bind(&FileWriteHelper::PrepareWritableFileAndRunAfterOpenFile,
                 weak_ptr_factory_.GetWeakPtr(),
                 file_path,
                 callback));
}

void FileWriteHelper::PrepareWritableFileAndRunAfterOpenFile(
    const FilePath& file_path,
    const OpenFileCallback& callback,
    DriveFileError error,
    const FilePath& local_cache_path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(!callback.is_null());

  if (error != DRIVE_FILE_OK) {
    content::BrowserThread::GetBlockingPool()->PostTask(
        FROM_HERE,
        base::Bind(callback, error, FilePath()));
    return;
  }

  content::BrowserThread::GetBlockingPool()->PostTaskAndReply(
      FROM_HERE,
      base::Bind(callback, DRIVE_FILE_OK, local_cache_path),
      base::Bind(&FileWriteHelper::PrepareWritableFileAndRunAfterCallback,
                 weak_ptr_factory_.GetWeakPtr(),
                 file_path));
}

void FileWriteHelper::PrepareWritableFileAndRunAfterCallback(
    const FilePath& file_path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  file_system_->CloseFile(file_path,
                          base::Bind(&EmitDebugLogForCloseFile, file_path));
}

}  // namespace drive