filesystem_api_util.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright 2014 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/file_manager/filesystem_api_util.h"
6
7#include "base/callback.h"
8#include "base/files/file_path.h"
9#include "base/memory/scoped_ptr.h"
10#include "chrome/browser/chromeos/drive/file_errors.h"
11#include "chrome/browser/chromeos/drive/file_system_interface.h"
12#include "chrome/browser/chromeos/drive/file_system_util.h"
13#include "chrome/browser/chromeos/file_manager/app_id.h"
14#include "chrome/browser/chromeos/file_manager/fileapi_util.h"
15#include "chrome/browser/extensions/extension_util.h"
16#include "chrome/browser/profiles/profile.h"
17#include "content/public/browser/browser_thread.h"
18#include "content/public/browser/storage_partition.h"
19#include "webkit/browser/fileapi/file_system_context.h"
20
21namespace file_manager {
22namespace util {
23
24namespace {
25
26void GetMimeTypeAfterGetResourceEntry(
27    const base::Callback<void(bool, const std::string&)>& callback,
28    drive::FileError error,
29    scoped_ptr<drive::ResourceEntry> entry) {
30  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
31
32  if (error != drive::FILE_ERROR_OK || !entry->has_file_specific_info()) {
33    callback.Run(false, std::string());
34    return;
35  }
36  callback.Run(true, entry->file_specific_info().content_mime_type());
37}
38
39void CheckDirectoryAfterDriveCheck(const base::Callback<void(bool)>& callback,
40                                   drive::FileError error) {
41  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
42
43  return callback.Run(error == drive::FILE_ERROR_OK);
44}
45
46void CheckWritableAfterDriveCheck(const base::Callback<void(bool)>& callback,
47                                  drive::FileError error,
48                                  const base::FilePath& local_path) {
49  // This is called on the IO-allowed blocking pool. Call back to UI.
50  content::BrowserThread::PostTask(
51      content::BrowserThread::UI,
52      FROM_HERE,
53      base::Bind(callback, error == drive::FILE_ERROR_OK));
54}
55
56}  // namespace
57
58bool IsUnderNonNativeLocalPath(Profile* profile,
59                        const base::FilePath& path) {
60  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
61
62  GURL url;
63  if (!util::ConvertAbsoluteFilePathToFileSystemUrl(
64           profile, path, kFileManagerAppId, &url)) {
65    return false;
66  }
67
68  content::StoragePartition* partition =
69      content::BrowserContext::GetStoragePartitionForSite(
70          profile,
71          extensions::util::GetSiteForExtensionId(kFileManagerAppId, profile));
72  fileapi::FileSystemContext* context = partition->GetFileSystemContext();
73
74  fileapi::FileSystemURL filesystem_url = context->CrackURL(url);
75  if (!filesystem_url.is_valid())
76    return false;
77
78  switch (filesystem_url.type()) {
79    case fileapi::kFileSystemTypeNativeLocal:
80    case fileapi::kFileSystemTypeRestrictedNativeLocal:
81      return false;
82    default:
83      // The path indeed corresponds to a mount point not associated with a
84      // native local path.
85      return true;
86  }
87}
88
89void GetNonNativeLocalPathMimeType(
90    Profile* profile,
91    const base::FilePath& path,
92    const base::Callback<void(bool, const std::string&)>& callback) {
93  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
94  DCHECK(IsUnderNonNativeLocalPath(profile, path));
95
96  if (!drive::util::IsUnderDriveMountPoint(path)) {
97    // Non-drive mount point does not have mime types as metadata. Just return
98    // success with empty mime type value.
99    content::BrowserThread::PostTask(
100        content::BrowserThread::UI,
101        FROM_HERE,
102        base::Bind(callback, true /* success */, std::string()));
103    return;
104  }
105
106  drive::FileSystemInterface* file_system =
107      drive::util::GetFileSystemByProfile(profile);
108  if (!file_system) {
109    content::BrowserThread::PostTask(
110        content::BrowserThread::UI,
111        FROM_HERE,
112        base::Bind(callback, false, std::string()));
113    return;
114  }
115
116  file_system->GetResourceEntry(
117      drive::util::ExtractDrivePath(path),
118      base::Bind(&GetMimeTypeAfterGetResourceEntry, callback));
119}
120
121void IsNonNativeLocalPathDirectory(
122    Profile* profile,
123    const base::FilePath& path,
124    const base::Callback<void(bool)>& callback) {
125  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
126
127  // TODO(kinaba): support other types of volumes besides Drive.
128  drive::util::CheckDirectoryExists(
129      profile,
130      path,
131      base::Bind(&CheckDirectoryAfterDriveCheck, callback));
132}
133
134void PrepareNonNativeLocalPathWritableFile(
135    Profile* profile,
136    const base::FilePath& path,
137    const base::Callback<void(bool)>& callback) {
138  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
139
140  // TODO(kinaba): support other types of volumes besides Drive.
141  drive::util::PrepareWritableFileAndRun(
142      profile,
143      path,
144      base::Bind(&CheckWritableAfterDriveCheck, callback));
145}
146
147}  // namespace util
148}  // namespace file_manager
149