Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/googledrivefs/googledrivefs.cc

Issue 2156503002: [NaCl SDK] Expose Google Drive to nacl_io. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "nacl_io/googledrivefs/googledrivefs.h"
6
7 #include "nacl_io/filesystem.h"
8 #include "nacl_io/statuscode.h"
9 #include "nacl_io/googledrivefs/googledrivefs_node.h"
10 #include "nacl_io/googledrivefs/googledrivefs_util.h"
11
12 namespace nacl_io {
13
14 GoogleDriveFs::GoogleDriveFs() {}
15
16 Error GoogleDriveFs::Init(const FsInitArgs& args) {
17 Error error = Filesystem::Init(args);
18 if (error) {
19 return error;
20 }
21
22 if (!ppapi()) {
23 return ENOSYS;
24 }
25
26 StringMap_t::const_iterator token_it = args.string_map.find("token");
27
28 if (token_it == args.string_map.end()) {
29 return EINVAL;
30 }
31
32 token_ = token_it->second;
33
34 return 0;
35 }
36
37 Error GoogleDriveFs::OpenWithMode(const Path& path,
38 int open_flags,
39 mode_t mode,
40 ScopedNode* out_node) {
41 ScopedNode node(new GoogleDriveFsNode(this, path));
42 Error error = node->Init(open_flags);
43 if (error) {
44 return error;
45 }
46
47 *out_node = node;
48
49 return 0;
50 }
51
52 Error GoogleDriveFs::Mkdir(const Path& path, int permissions) {
53 if (path.IsRoot()) {
54 return EEXIST;
55 }
56
57 std::string parent_dir_id;
58 Error error = RequestParentDirId(path, this, &parent_dir_id);
59 if (error) {
60 return error;
61 }
62
63 std::string item_id;
64 bool is_dir_type;
65 error = RequestItemIdAndItemType(parent_dir_id, path.Basename(), this,
66 &item_id, &is_dir_type);
67
68 // mkdir does not create a directory when a directory or a file with
69 // path.Basename() already exists.
70 if (!error) {
71 return EEXIST;
72 } else if (error != ENOENT) {
73 return error;
74 }
75
76 RequestUrlParams p;
77
78 p.url = DRIVE_URL;
79 p.method = "POST";
80
81 AddHeaders("Content-type", "application/json", &p.headers);
82 AddHeaders("Authorization", "Bearer " + token_, &p.headers);
83
84 AddBody("{", &p.body);
85 AddBody(" \"name\": \"" + path.Basename() + "\",", &p.body);
86 AddBody(
87 std::string(" \"mimeType\": \"") + FOLDER_MIME_TYPE + std::string("\","),
88 &p.body);
89 AddBody(" \"parents\": [", &p.body);
90 AddBody(" \"" + parent_dir_id + "\"", &p.body);
91 AddBody(" ]", &p.body);
92 AddBody("}", &p.body);
93
94 ScopedResource url_response_info_resource(ppapi());
95 error = LoadUrl(ppapi(), p, &url_response_info_resource);
96 if (error) {
97 return error;
98 }
99
100 if (ReadStatusCode(ppapi(), url_response_info_resource.pp_resource()) !=
101 STATUSCODE_OK) {
102 return EPERM;
103 }
104
105 return 0;
106 }
107
108 Error GoogleDriveFs::Rmdir(const Path& path) {
109 if (path.IsRoot()) {
110 return EEXIST;
111 }
112
113 std::string parent_dir_id;
114 Error error = RequestParentDirId(path, this, &parent_dir_id);
115 if (error) {
116 return error;
117 }
118
119 std::string item_id;
120 bool is_dir_type;
121 error = RequestItemIdAndItemType(parent_dir_id, path.Basename(), this,
122 &item_id, &is_dir_type);
123 if (error) {
124 return error;
125 }
126
127 if (!is_dir_type) {
128 return ENOTDIR;
129 }
130
131 bool is_empty_dir;
132 error = IsEmptyDir(item_id, &is_empty_dir);
133 if (error) {
134 return error;
135 }
136
137 if (!is_empty_dir) {
138 return ENOTEMPTY;
139 }
140
141 RequestUrlParams p;
142
143 p.url = DRIVE_URL;
144 AddUrlPath(item_id, &p.url);
145
146 p.method = "DELETE";
147
148 AddHeaders("Content-type", "application/json", &p.headers);
149 AddHeaders("Authorization", "Bearer " + token_, &p.headers);
150
151 ScopedResource url_response_info_resource(ppapi());
152 error = LoadUrl(ppapi(), p, &url_response_info_resource);
153 if (error) {
154 return error;
155 }
156
157 if (ReadStatusCode(ppapi(), url_response_info_resource.pp_resource()) !=
158 STATUSCODE_NO_CONTENT) {
159 return EPERM;
160 }
161
162 return 0;
163 }
164
165 Error GoogleDriveFs::Rename(const Path& path, const Path& newPath) {
166 // TODO: support rename
167 LOG_ERROR("rename not supported.");
168 return EPERM;
169 }
170
171 Error GoogleDriveFs::Unlink(const Path& path) {
172 // TODO: support unlink
173 LOG_ERROR("unlink not supported.");
174 return EPERM;
175 }
176
177 Error GoogleDriveFs::Remove(const Path& path) {
178 // TODO: support remove
179 LOG_ERROR("remove not supported.");
180 return EPERM;
181 }
182
183 Error GoogleDriveFs::IsEmptyDir(const std::string& dir_id,
184 bool* out_is_empty_dir) {
185 RequestUrlParams p;
186
187 p.url = DRIVE_URL;
188 AddUrlFirstQueryParameter("q", ParentEqualClause(dir_id), &p.url);
189
190 p.method = "GET";
191
192 AddHeaders("Content-type", "application/json", &p.headers);
193 AddHeaders("Authorization", "Bearer " + token_, &p.headers);
194
195 ScopedResource url_response_info_resource(ppapi());
196 Error error = LoadUrl(ppapi(), p, &url_response_info_resource);
197 if (error) {
198 return error;
199 }
200
201 if (ReadStatusCode(ppapi(), url_response_info_resource.pp_resource()) !=
202 STATUSCODE_OK) {
203 return EPERM;
204 }
205
206 std::string output;
207 error = ReadResponseBody(ppapi(), url_response_info_resource.pp_resource(),
208 UINT_MAX, &output);
209 if (error) {
210 return error;
211 }
212
213 std::string name_value;
214 size_t name_index;
215 error =
216 GetValueStringAndValuePos(output, "name", 0, &name_value, &name_index);
217 if (error && error != EINVAL) {
218 return error;
219 }
220
221 *out_is_empty_dir = (error == EINVAL);
222
223 return 0;
224 }
225
226 std::string GoogleDriveFs::token() {
227 return token_;
228 }
229
230 } // namespace nacl_io
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698