summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/file_system/create_file_operation.cc
blob: 36da187d463591abed73cd0c6654ce955ce88cfc (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2013 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_system/create_file_operation.h"

#include <string>

#include "base/file_util.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/file_cache.h"
#include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/chromeos/drive/job_scheduler.h"
#include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
#include "chrome/browser/chromeos/drive/resource_metadata.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/mime_util.h"

using content::BrowserThread;

namespace drive {
namespace file_system {

namespace {

const char kMimeTypeOctetStream[] = "application/octet-stream";

// Part of CreateFileOperation::CreateFile(), runs on |blocking_task_runner_|
// of the operation, before server-side file creation.
FileError CheckPreConditionForCreateFile(internal::ResourceMetadata* metadata,
                                         const base::FilePath& file_path,
                                         bool is_exclusive,
                                         std::string* parent_resource_id,
                                         std::string* mime_type) {
  DCHECK(metadata);
  DCHECK(parent_resource_id);
  DCHECK(mime_type);

  ResourceEntry entry;
  FileError error = metadata->GetResourceEntryByPath(file_path, &entry);
  if (error == FILE_ERROR_OK) {
    // Error if an exclusive mode is requested, or the entry is not a file.
    return (is_exclusive ||
            entry.file_info().is_directory() ||
            entry.file_specific_info().is_hosted_document()) ?
        FILE_ERROR_EXISTS : FILE_ERROR_OK;
  }

  // If the file is not found, an actual request to create a new file will be
  // sent to the server.
  if (error == FILE_ERROR_NOT_FOUND) {
    // If parent path is not a directory, it is an error.
    ResourceEntry parent;
    if (metadata->GetResourceEntryByPath(
            file_path.DirName(), &parent) != FILE_ERROR_OK ||
        !parent.file_info().is_directory())
      return FILE_ERROR_NOT_A_DIRECTORY;

    // In the request, parent_resource_id and mime_type are needed.
    // Here, populate them.
    *parent_resource_id = parent.resource_id();

    // If mime_type is not set or "application/octet-stream", guess from the
    // |file_path|. If it is still unsure, use octet-stream by default.
    if ((mime_type->empty() || *mime_type == kMimeTypeOctetStream) &&
        !net::GetMimeTypeFromFile(file_path, mime_type)) {
      *mime_type = kMimeTypeOctetStream;
    }
  }

  return error;
}

// Part of CreateFileOperation::CreateFile(), runs on |blocking_task_runner_|
// of the operation, after server side file creation.
FileError UpdateLocalStateForCreateFile(
    internal::ResourceMetadata* metadata,
    internal::FileCache* cache,
    scoped_ptr<google_apis::ResourceEntry> resource_entry,
    base::FilePath* file_path) {
  DCHECK(metadata);
  DCHECK(cache);
  DCHECK(resource_entry);
  DCHECK(file_path);

  // Add the entry to the local resource metadata.
  ResourceEntry entry;
  std::string parent_resource_id;
  if (!ConvertToResourceEntry(*resource_entry, &entry, &parent_resource_id) ||
      parent_resource_id.empty())
    return FILE_ERROR_NOT_A_FILE;

  std::string parent_local_id;
  FileError error = metadata->GetIdByResourceId(parent_resource_id,
                                                &parent_local_id);
  if (error != FILE_ERROR_OK)
    return error;
  entry.set_parent_local_id(parent_local_id);

  std::string local_id;
  error = metadata->AddEntry(entry, &local_id);

  // Depending on timing, the metadata may have inserted via change list
  // already. So, FILE_ERROR_EXISTS is not an error.
  if (error == FILE_ERROR_EXISTS)
    error = metadata->GetIdByResourceId(entry.resource_id(), &local_id);

  if (error == FILE_ERROR_OK) {
    // At this point, upload to the server is fully succeeded.
    // Populate the |file_path| which will be used to notify the observer.
    *file_path = metadata->GetFilePath(local_id);

    // Also store an empty file to the cache.
    // Here, failure is not a fatal error, so ignore the returned code.
    FileError cache_store_error = FILE_ERROR_FAILED;
    base::FilePath empty_file;
    if (base::CreateTemporaryFile(&empty_file)) {
      cache_store_error =  cache->Store(
          local_id,
          entry.file_specific_info().md5(),
          empty_file,
          internal::FileCache::FILE_OPERATION_MOVE);
    }
    DLOG_IF(WARNING, cache_store_error != FILE_ERROR_OK)
        << "Failed to store a cache file: "
        << FileErrorToString(cache_store_error)
        << ", local_id: " << local_id;
  }

  return error;
}

}  // namespace

CreateFileOperation::CreateFileOperation(
    base::SequencedTaskRunner* blocking_task_runner,
    OperationObserver* observer,
    JobScheduler* scheduler,
    internal::ResourceMetadata* metadata,
    internal::FileCache* cache)
    : blocking_task_runner_(blocking_task_runner),
      observer_(observer),
      scheduler_(scheduler),
      metadata_(metadata),
      cache_(cache),
      weak_ptr_factory_(this) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}

CreateFileOperation::~CreateFileOperation() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}

void CreateFileOperation::CreateFile(const base::FilePath& file_path,
                                     bool is_exclusive,
                                     const std::string& mime_type,
                                     const FileOperationCallback& callback) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(!callback.is_null());

  std::string* parent_resource_id = new std::string;
  std::string* determined_mime_type = new std::string(mime_type);
  base::PostTaskAndReplyWithResult(
      blocking_task_runner_.get(),
      FROM_HERE,
      base::Bind(&CheckPreConditionForCreateFile,
                 metadata_,
                 file_path,
                 is_exclusive,
                 parent_resource_id,
                 determined_mime_type),
      base::Bind(&CreateFileOperation::CreateFileAfterCheckPreCondition,
                 weak_ptr_factory_.GetWeakPtr(),
                 file_path,
                 callback,
                 base::Owned(parent_resource_id),
                 base::Owned(determined_mime_type)));
}

void CreateFileOperation::CreateFileAfterCheckPreCondition(
    const base::FilePath& file_path,
    const FileOperationCallback& callback,
    std::string* parent_resource_id,
    std::string* mime_type,
    FileError error) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(!callback.is_null());
  DCHECK(parent_resource_id);
  DCHECK(mime_type);

  // If the file is found, or an error other than "not found" is found,
  // runs callback and quit the operation.
  if (error != FILE_ERROR_NOT_FOUND) {
    callback.Run(error);
    return;
  }

  scheduler_->CreateFile(
      *parent_resource_id,
      file_path,
      file_path.BaseName().value(),
      *mime_type,
      ClientContext(USER_INITIATED),
      base::Bind(&CreateFileOperation::CreateFileAfterUpload,
                 weak_ptr_factory_.GetWeakPtr(),
                 callback));
}

void CreateFileOperation::CreateFileAfterUpload(
    const FileOperationCallback& callback,
    google_apis::GDataErrorCode gdata_error,
    scoped_ptr<google_apis::ResourceEntry> resource_entry) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(!callback.is_null());

  FileError error = GDataToFileError(gdata_error);
  if (error != FILE_ERROR_OK) {
    callback.Run(error);
    return;
  }
  DCHECK(resource_entry);

  base::FilePath* file_path = new base::FilePath;
  base::PostTaskAndReplyWithResult(
      blocking_task_runner_.get(),
      FROM_HERE,
      base::Bind(&UpdateLocalStateForCreateFile,
                 metadata_,
                 cache_,
                 base::Passed(&resource_entry),
                 file_path),
      base::Bind(&CreateFileOperation::CreateFileAfterUpdateLocalState,
                 weak_ptr_factory_.GetWeakPtr(),
                 callback,
                 base::Owned(file_path)));
}

void CreateFileOperation::CreateFileAfterUpdateLocalState(
    const FileOperationCallback& callback,
    base::FilePath* file_path,
    FileError error) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(!callback.is_null());
  DCHECK(file_path);

  // Notify observer if the file creation process is successfully done.
  if (error == FILE_ERROR_OK)
    observer_->OnDirectoryChangedByOperation(file_path->DirName());

  callback.Run(error);
}

}  // namespace file_system
}  // namespace drive