Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / fake_provided_file_system.cc
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_system_provider/fake_provided_file_system.h"
6
7 #include <string>
8
9 #include "base/files/file.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "extensions/browser/event_router.h"
12
13 namespace chromeos {
14 namespace file_system_provider {
15 namespace {
16
17 // Adds a fake entry to the entry list.
18 void AddDirectoryEntry(fileapi::AsyncFileUtil::EntryList* entry_list,
19                        const std::string& name,
20                        fileapi::DirectoryEntry::DirectoryEntryType type,
21                        int64 size,
22                        std::string last_modified_time_string) {
23   base::Time last_modified_time;
24   const bool result = base::Time::FromString(last_modified_time_string.c_str(),
25                                              &last_modified_time);
26   DCHECK(result);
27   entry_list->push_back(
28       fileapi::DirectoryEntry(name, type, size, last_modified_time));
29 }
30
31 }  // namespace
32
33 FakeProvidedFileSystem::FakeProvidedFileSystem(
34     const ProvidedFileSystemInfo& file_system_info)
35     : file_system_info_(file_system_info) {
36 }
37
38 FakeProvidedFileSystem::~FakeProvidedFileSystem() {}
39
40 void FakeProvidedFileSystem::RequestUnmount(
41     const fileapi::AsyncFileUtil::StatusCallback& callback) {
42   base::MessageLoopProxy::current()->PostTask(
43       FROM_HERE, base::Bind(callback, base::File::FILE_OK));
44 }
45
46 void FakeProvidedFileSystem::GetMetadata(
47     const base::FilePath& entry_path,
48     const fileapi::AsyncFileUtil::GetFileInfoCallback& callback) {
49   // Return fake metadata for the root directory only.
50   if (entry_path.AsUTF8Unsafe() != "/") {
51     base::MessageLoopProxy::current()->PostTask(
52         FROM_HERE,
53         base::Bind(
54             callback, base::File::FILE_ERROR_NOT_FOUND, base::File::Info()));
55     return;
56   }
57
58   base::File::Info file_info;
59   file_info.size = 0;
60   file_info.is_directory = true;
61   file_info.is_symbolic_link = false;
62   base::Time last_modified_time;
63   const bool result = base::Time::FromString("Thu Apr 24 00:46:52 UTC 2014",
64                                              &last_modified_time);
65   DCHECK(result);
66   file_info.last_modified = last_modified_time;
67
68   base::MessageLoopProxy::current()->PostTask(
69       FROM_HERE, base::Bind(callback, base::File::FILE_OK, file_info));
70 }
71
72 void FakeProvidedFileSystem::ReadDirectory(
73     const base::FilePath& directory_path,
74     const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) {
75   // Return fake contents for the root directory only.
76   if (directory_path.AsUTF8Unsafe() != "/") {
77     base::MessageLoopProxy::current()->PostTask(
78         FROM_HERE,
79         base::Bind(callback,
80                    base::File::FILE_ERROR_NOT_FOUND,
81                    fileapi::AsyncFileUtil::EntryList(),
82                    false /* has_more */));
83     return;
84   }
85
86   {
87     fileapi::AsyncFileUtil::EntryList entry_list;
88     AddDirectoryEntry(&entry_list,
89                       "hello.txt",
90                       fileapi::DirectoryEntry::FILE,
91                       1024 /* size */,
92                       "Thu Apr 24 00:46:52 UTC 2014");
93
94     AddDirectoryEntry(&entry_list,
95                       "world.txt",
96                       fileapi::DirectoryEntry::FILE,
97                       1024 /* size */,
98                       "Wed Apr 23 00:20:30 UTC 2014");
99
100     base::MessageLoopProxy::current()->PostTask(
101         FROM_HERE,
102         base::Bind(
103             callback, base::File::FILE_OK, entry_list, true /* has_more */));
104   }
105
106   {
107     fileapi::AsyncFileUtil::EntryList entry_list;
108     AddDirectoryEntry(&entry_list,
109                       "pictures",
110                       fileapi::DirectoryEntry::DIRECTORY,
111                       0 /* size */,
112                       "Tue May 22 00:40:50 UTC 2014");
113
114     base::MessageLoopProxy::current()->PostTask(
115         FROM_HERE,
116         base::Bind(
117             callback, base::File::FILE_OK, entry_list, false /* has_more */));
118   }
119 }
120
121 const ProvidedFileSystemInfo& FakeProvidedFileSystem::GetFileSystemInfo()
122     const {
123   return file_system_info_;
124 }
125
126 RequestManager* FakeProvidedFileSystem::GetRequestManager() {
127   NOTREACHED();
128   return NULL;
129 }
130
131 ProvidedFileSystemInterface* FakeProvidedFileSystem::Create(
132     extensions::EventRouter* event_router,
133     const ProvidedFileSystemInfo& file_system_info) {
134   return new FakeProvidedFileSystem(file_system_info);
135 }
136
137 }  // namespace file_system_provider
138 }  // namespace chromeos