1 // Copyright 2013 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.
5 #include "chrome/browser/chromeos/drive/debug_info_collector.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "google_apis/drive/task_util.h"
12 using content::BrowserThread
;
18 void IterateFileCacheInternal(
19 internal::ResourceMetadata
* metadata
,
20 const DebugInfoCollector::IterateFileCacheCallback
& iteration_callback
) {
21 scoped_ptr
<internal::ResourceMetadata::Iterator
> it
= metadata
->GetIterator();
22 for (; !it
->IsAtEnd(); it
->Advance()) {
23 if (it
->GetValue().file_specific_info().has_cache_state()) {
24 iteration_callback
.Run(it
->GetID(),
25 it
->GetValue().file_specific_info().cache_state());
28 DCHECK(!it
->HasError());
31 // Runs the callback with arguments.
32 void RunGetResourceEntryCallback(const GetResourceEntryCallback
& callback
,
33 scoped_ptr
<ResourceEntry
> entry
,
35 DCHECK(!callback
.is_null());
36 if (error
!= FILE_ERROR_OK
)
38 callback
.Run(error
, entry
.Pass());
41 // Runs the callback with arguments.
42 void RunReadDirectoryCallback(
43 const DebugInfoCollector::ReadDirectoryCallback
& callback
,
44 scoped_ptr
<ResourceEntryVector
> entries
,
46 DCHECK(!callback
.is_null());
47 if (error
!= FILE_ERROR_OK
)
49 callback
.Run(error
, entries
.Pass());
54 DebugInfoCollector::DebugInfoCollector(
55 internal::ResourceMetadata
* metadata
,
56 FileSystemInterface
* file_system
,
57 base::SequencedTaskRunner
* blocking_task_runner
)
58 : metadata_(metadata
),
59 file_system_(file_system
),
60 blocking_task_runner_(blocking_task_runner
) {
65 DebugInfoCollector::~DebugInfoCollector() {
68 void DebugInfoCollector::GetResourceEntry(
69 const base::FilePath
& file_path
,
70 const GetResourceEntryCallback
& callback
) {
71 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
72 DCHECK(!callback
.is_null());
74 scoped_ptr
<ResourceEntry
> entry(new ResourceEntry
);
75 ResourceEntry
* entry_ptr
= entry
.get();
76 base::PostTaskAndReplyWithResult(
77 blocking_task_runner_
.get(),
79 base::Bind(&internal::ResourceMetadata::GetResourceEntryByPath
,
80 base::Unretained(metadata_
),
83 base::Bind(&RunGetResourceEntryCallback
, callback
, base::Passed(&entry
)));
86 void DebugInfoCollector::ReadDirectory(
87 const base::FilePath
& file_path
,
88 const ReadDirectoryCallback
& callback
) {
89 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
90 DCHECK(!callback
.is_null());
92 scoped_ptr
<ResourceEntryVector
> entries(new ResourceEntryVector
);
93 ResourceEntryVector
* entries_ptr
= entries
.get();
94 base::PostTaskAndReplyWithResult(
95 blocking_task_runner_
.get(),
97 base::Bind(&internal::ResourceMetadata::ReadDirectoryByPath
,
98 base::Unretained(metadata_
),
101 base::Bind(&RunReadDirectoryCallback
, callback
, base::Passed(&entries
)));
104 void DebugInfoCollector::IterateFileCache(
105 const IterateFileCacheCallback
& iteration_callback
,
106 const base::Closure
& completion_callback
) {
107 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
108 DCHECK(!iteration_callback
.is_null());
109 DCHECK(!completion_callback
.is_null());
111 blocking_task_runner_
->PostTaskAndReply(
113 base::Bind(&IterateFileCacheInternal
,
115 google_apis::CreateRelayCallback(iteration_callback
)),
116 completion_callback
);
119 void DebugInfoCollector::GetMetadata(
120 const GetFilesystemMetadataCallback
& callback
) {
121 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
122 DCHECK(!callback
.is_null());
124 // Currently, this is just a proxy to the FileSystem.
125 // TODO(hidehiko): Move the implementation to here to simplify the
126 // FileSystem's implementation. crbug.com/237088
127 file_system_
->GetMetadata(callback
);