Commit 9947decb authored by kinaba@chromium.org's avatar kinaba@chromium.org

Implement snapshotting for non-Drive non-local file systems.

This is for supporting uploading files from those file systems
(MTP, providedFS) to web pages, by a workaround before the
long term solution resolving the issue crbug/126902 comes.

BUG=383207
R=hashimoto@chromium.org

Review URL: https://codereview.chromium.org/339323002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278637 0039d316-1c4b-4281-b951-d872f2087c98
parent 018452d5
......@@ -17,6 +17,7 @@
#include "chrome/browser/chromeos/file_manager/fileapi_util.h"
#include "chrome/browser/chromeos/file_manager/filesystem_api_util.h"
#include "chrome/browser/chromeos/file_manager/path_util.h"
#include "chrome/browser/chromeos/file_manager/snapshot_manager.h"
#include "chrome/browser/chromeos/file_manager/volume_manager.h"
#include "chrome/browser/chromeos/fileapi/file_system_backend.h"
#include "chrome/browser/profiles/profile.h"
......@@ -72,9 +73,8 @@ void GetFileNativeLocalPathForOpening(Profile* profile,
return;
}
// TODO(kinaba) crbug.com/383207 implement this.
NOTREACHED();
callback.Run(base::FilePath());
VolumeManager::Get(profile)->snapshot_manager()->CreateManagedSnapshot(
path, callback);
}
// Gets a resolved local file path of a non native |path| for file saving.
......
// 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
// 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.
#ifndef CHROME_BROWSER_CHROMEOS_FILE_MANAGER_SNAPSHOT_MANAGER_H_
#define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_SNAPSHOT_MANAGER_H_
#include <vector>
#include "base/callback_forward.h"
#include "base/files/file.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
class Profile;
namespace base {
class FilePath;
} // namespace base
namespace webkit_blob {
class ShareableFileReference;
} // namespace webkit_blob
namespace file_manager {
// Utility class for creating a snapshot of a file system file on local disk.
// The class wraps the underlying implementation of fileapi's CreateSnapshotFile
// and prolongs the lifetime of snapshot files so that the client code that just
// accepts file paths works without problems.
class SnapshotManager {
public:
// The callback type for CreateManagedSnapshot.
typedef base::Callback<void(const base::FilePath&)> LocalPathCallback;
explicit SnapshotManager(Profile* profile);
~SnapshotManager();
// Creates a snapshot file copy of a file system file |absolute_file_path| and
// returns back to |callback|. Returns empty path for failure.
void CreateManagedSnapshot(const base::FilePath& absolute_file_path,
const LocalPathCallback& callback);
private:
// Part of CreateManagedSnapshot.
void 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);
Profile* profile_;
std::vector<scoped_refptr<webkit_blob::ShareableFileReference> > file_refs_;
// Note: This should remain the last member so it'll be destroyed and
// invalidate the weak pointers before any other members are destroyed.
base::WeakPtrFactory<SnapshotManager> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(SnapshotManager);
};
} // namespace file_manager
#endif // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_SNAPSHOT_MANAGER_H_
......@@ -18,6 +18,7 @@
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/chromeos/file_manager/mounted_disk_monitor.h"
#include "chrome/browser/chromeos/file_manager/path_util.h"
#include "chrome/browser/chromeos/file_manager/snapshot_manager.h"
#include "chrome/browser/chromeos/file_manager/volume_manager_factory.h"
#include "chrome/browser/chromeos/file_manager/volume_manager_observer.h"
#include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
......@@ -256,6 +257,7 @@ VolumeManager::VolumeManager(
mounted_disk_monitor_(
new MountedDiskMonitor(power_manager_client, disk_mount_manager)),
file_system_provider_service_(file_system_provider_service),
snapshot_manager_(new SnapshotManager(profile_)),
weak_ptr_factory_(this) {
DCHECK(disk_mount_manager);
}
......@@ -403,6 +405,7 @@ void VolumeManager::Initialize() {
void VolumeManager::Shutdown() {
weak_ptr_factory_.InvalidateWeakPtrs();
snapshot_manager_.reset();
pref_change_registrar_.RemoveAll();
disk_mount_manager_->RemoveObserver(this);
if (storage_monitor::StorageMonitor::GetInstance())
......
......@@ -38,6 +38,7 @@ class BrowserContext;
namespace file_manager {
class MountedDiskMonitor;
class SnapshotManager;
class VolumeManagerObserver;
// Identifiers for volume types managed by Chrome OS file manager.
......@@ -202,6 +203,8 @@ class VolumeManager : public KeyedService,
virtual void OnRemovableStorageDetached(
const storage_monitor::StorageInfo& info) OVERRIDE;
SnapshotManager* snapshot_manager() { return snapshot_manager_.get(); }
private:
void OnStorageMonitorInitialized();
void OnPrivetVolumesAvailable(
......@@ -222,6 +225,7 @@ class VolumeManager : public KeyedService,
chromeos::file_system_provider::Service*
file_system_provider_service_; // Not owned by this class.
std::map<std::string, VolumeInfo> mounted_volumes_;
scoped_ptr<SnapshotManager> snapshot_manager_;
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
......
......@@ -368,6 +368,8 @@
'browser/chromeos/file_manager/path_util.h',
'browser/chromeos/file_manager/select_file_dialog_util.cc',
'browser/chromeos/file_manager/select_file_dialog_util.h',
'browser/chromeos/file_manager/snapshot_manager.cc',
'browser/chromeos/file_manager/snapshot_manager.h',
'browser/chromeos/file_manager/url_util.cc',
'browser/chromeos/file_manager/url_util.h',
'browser/chromeos/file_manager/volume_manager.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment