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

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_util.cc

Issue 10920091: Move Drive API files to google_apis directory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reflect comments Created 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/gdata/gdata_util.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #include "base/string_number_conversions.h"
14 #include "base/string_util.h"
15 #include "base/stringprintf.h"
16 #include "base/time.h"
17 #include "base/tracked_objects.h"
18 #include "chrome/browser/prefs/pref_service.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h"
22 #include "content/public/browser/browser_thread.h"
23
24 #if defined(OS_CHROMEOS)
25 #include "chrome/browser/chromeos/login/user_manager.h"
26 #endif // OS_CHROMEOS
27
28 using content::BrowserThread;
29
30 namespace gdata {
31 namespace util {
32
33 namespace {
34
35 const char kGDataSpecialRootPath[] = "/special";
36
37 const char kGDataMountPointPath[] = "/special/drive";
38
39 const int kReadOnlyFilePermissions = base::PLATFORM_FILE_OPEN |
40 base::PLATFORM_FILE_READ |
41 base::PLATFORM_FILE_EXCLUSIVE_READ |
42 base::PLATFORM_FILE_ASYNC;
43
44 bool ParseTimezone(const base::StringPiece& timezone,
45 bool ahead,
46 int* out_offset_to_utc_in_minutes) {
47 DCHECK(out_offset_to_utc_in_minutes);
48
49 std::vector<base::StringPiece> parts;
50 int num_of_token = Tokenize(timezone, ":", &parts);
51
52 int hour = 0;
53 if (!base::StringToInt(parts[0], &hour))
54 return false;
55
56 int minute = 0;
57 if (num_of_token > 1 && !base::StringToInt(parts[1], &minute))
58 return false;
59
60 *out_offset_to_utc_in_minutes = (hour * 60 + minute) * (ahead ? +1 : -1);
61 return true;
62 }
63
64 } // namespace
65
66 bool IsGDataAvailable(Profile* profile) {
67 #if defined(OS_CHROMEOS)
68 if (!chromeos::UserManager::Get()->IsUserLoggedIn() ||
69 chromeos::UserManager::Get()->IsLoggedInAsGuest() ||
70 chromeos::UserManager::Get()->IsLoggedInAsDemoUser())
71 return false;
72
73 // Do not allow GData for incognito windows / guest mode.
74 if (profile->IsOffTheRecord())
75 return false;
76
77 // Disable gdata if preference is set. This can happen with commandline flag
78 // --disable-gdata or enterprise policy, or probably with user settings too
79 // in the future.
80 if (profile->GetPrefs()->GetBoolean(prefs::kDisableGData))
81 return false;
82
83 return true;
84 #else
85 // TODO(nhiroki): Check if GData is available or not in a platform
86 // independent way (http://crbug.com/147529).
87 return false;
88 #endif // OS_CHROMEOS
89 }
90
91 bool IsDriveV2ApiEnabled() {
92 static bool enabled = CommandLine::ForCurrentProcess()->HasSwitch(
93 switches::kEnableDriveV2Api);
94 return enabled;
95 }
96
97 base::PlatformFileError DriveFileErrorToPlatformError(
98 gdata::DriveFileError error) {
99 switch (error) {
100 case gdata::DRIVE_FILE_OK:
101 return base::PLATFORM_FILE_OK;
102
103 case gdata::DRIVE_FILE_ERROR_FAILED:
104 return base::PLATFORM_FILE_ERROR_FAILED;
105
106 case gdata::DRIVE_FILE_ERROR_IN_USE:
107 return base::PLATFORM_FILE_ERROR_IN_USE;
108
109 case gdata::DRIVE_FILE_ERROR_EXISTS:
110 return base::PLATFORM_FILE_ERROR_EXISTS;
111
112 case gdata::DRIVE_FILE_ERROR_NOT_FOUND:
113 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
114
115 case gdata::DRIVE_FILE_ERROR_ACCESS_DENIED:
116 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED;
117
118 case gdata::DRIVE_FILE_ERROR_TOO_MANY_OPENED:
119 return base::PLATFORM_FILE_ERROR_TOO_MANY_OPENED;
120
121 case gdata::DRIVE_FILE_ERROR_NO_MEMORY:
122 return base::PLATFORM_FILE_ERROR_NO_MEMORY;
123
124 case gdata::DRIVE_FILE_ERROR_NO_SPACE:
125 return base::PLATFORM_FILE_ERROR_NO_SPACE;
126
127 case gdata::DRIVE_FILE_ERROR_NOT_A_DIRECTORY:
128 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY;
129
130 case gdata::DRIVE_FILE_ERROR_INVALID_OPERATION:
131 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
132
133 case gdata::DRIVE_FILE_ERROR_SECURITY:
134 return base::PLATFORM_FILE_ERROR_SECURITY;
135
136 case gdata::DRIVE_FILE_ERROR_ABORT:
137 return base::PLATFORM_FILE_ERROR_ABORT;
138
139 case gdata::DRIVE_FILE_ERROR_NOT_A_FILE:
140 return base::PLATFORM_FILE_ERROR_NOT_A_FILE;
141
142 case gdata::DRIVE_FILE_ERROR_NOT_EMPTY:
143 return base::PLATFORM_FILE_ERROR_NOT_EMPTY;
144
145 case gdata::DRIVE_FILE_ERROR_INVALID_URL:
146 return base::PLATFORM_FILE_ERROR_INVALID_URL;
147
148 case gdata::DRIVE_FILE_ERROR_NO_CONNECTION:
149 return base::PLATFORM_FILE_ERROR_FAILED;
150 }
151
152 NOTREACHED();
153 return base::PLATFORM_FILE_ERROR_FAILED;
154 }
155
156 bool GetTimeFromString(const base::StringPiece& raw_value,
157 base::Time* parsed_time) {
158 base::StringPiece date;
159 base::StringPiece time_and_tz;
160 base::StringPiece time;
161 base::Time::Exploded exploded = {0};
162 bool has_timezone = false;
163 int offset_to_utc_in_minutes = 0;
164
165 // Splits the string into "date" part and "time" part.
166 {
167 std::vector<base::StringPiece> parts;
168 if (Tokenize(raw_value, "T", &parts) != 2)
169 return false;
170 date = parts[0];
171 time_and_tz = parts[1];
172 }
173
174 // Parses timezone suffix on the time part if available.
175 {
176 std::vector<base::StringPiece> parts;
177 if (time_and_tz[time_and_tz.size() - 1] == 'Z') {
178 // Timezone is 'Z' (UTC)
179 has_timezone = true;
180 offset_to_utc_in_minutes = 0;
181 time = time_and_tz;
182 time.remove_suffix(1);
183 } else if (Tokenize(time_and_tz, "+", &parts) == 2) {
184 // Timezone is "+hh:mm" format
185 if (!ParseTimezone(parts[1], true, &offset_to_utc_in_minutes))
186 return false;
187 has_timezone = true;
188 time = parts[0];
189 } else if (Tokenize(time_and_tz, "-", &parts) == 2) {
190 // Timezone is "-hh:mm" format
191 if (!ParseTimezone(parts[1], false, &offset_to_utc_in_minutes))
192 return false;
193 has_timezone = true;
194 time = parts[0];
195 } else {
196 // No timezone (uses local timezone)
197 time = time_and_tz;
198 }
199 }
200
201 // Parses the date part.
202 {
203 std::vector<base::StringPiece> parts;
204 if (Tokenize(date, "-", &parts) != 3)
205 return false;
206
207 if (!base::StringToInt(parts[0], &exploded.year) ||
208 !base::StringToInt(parts[1], &exploded.month) ||
209 !base::StringToInt(parts[2], &exploded.day_of_month)) {
210 return false;
211 }
212 }
213
214 // Parses the time part.
215 {
216 std::vector<base::StringPiece> parts;
217 int num_of_token = Tokenize(time, ":", &parts);
218 if (num_of_token != 3)
219 return false;
220
221 if (!base::StringToInt(parts[0], &exploded.hour) ||
222 !base::StringToInt(parts[1], &exploded.minute)) {
223 return false;
224 }
225
226 std::vector<base::StringPiece> seconds_parts;
227 int num_of_seconds_token = Tokenize(parts[2], ".", &seconds_parts);
228 if (num_of_seconds_token >= 3)
229 return false;
230
231 if (!base::StringToInt(seconds_parts[0], &exploded.second))
232 return false;
233
234 // Only accept milli-seconds (3-digits).
235 if (num_of_seconds_token > 1 &&
236 seconds_parts[1].length() == 3 &&
237 !base::StringToInt(seconds_parts[1], &exploded.millisecond)) {
238 return false;
239 }
240 }
241
242 exploded.day_of_week = 0;
243 if (!exploded.HasValidValues())
244 return false;
245
246 if (has_timezone) {
247 *parsed_time = base::Time::FromUTCExploded(exploded);
248 if (offset_to_utc_in_minutes != 0)
249 *parsed_time -= base::TimeDelta::FromMinutes(offset_to_utc_in_minutes);
250 } else {
251 *parsed_time = base::Time::FromLocalExploded(exploded);
252 }
253
254 return true;
255 }
256
257 std::string FormatTimeAsString(const base::Time& time) {
258 base::Time::Exploded exploded;
259 time.UTCExplode(&exploded);
260 return base::StringPrintf(
261 "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
262 exploded.year, exploded.month, exploded.day_of_month,
263 exploded.hour, exploded.minute, exploded.second, exploded.millisecond);
264 }
265
266 std::string FormatTimeAsStringLocaltime(const base::Time& time) {
267 base::Time::Exploded exploded;
268 time.LocalExplode(&exploded);
269
270 return base::StringPrintf(
271 "%04d-%02d-%02dT%02d:%02d:%02d.%03d",
272 exploded.year, exploded.month, exploded.day_of_month,
273 exploded.hour, exploded.minute, exploded.second, exploded.millisecond);
274 }
275
276 void PostBlockingPoolSequencedTask(
277 const tracked_objects::Location& from_here,
278 base::SequencedTaskRunner* blocking_task_runner,
279 const base::Closure& task) {
280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
281
282 const bool posted = blocking_task_runner->PostTask(from_here, task);
283 DCHECK(posted);
284 }
285
286 void PostBlockingPoolSequencedTaskAndReply(
287 const tracked_objects::Location& from_here,
288 base::SequencedTaskRunner* blocking_task_runner,
289 const base::Closure& request_task,
290 const base::Closure& reply_task) {
291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
292
293 const bool posted = blocking_task_runner->PostTaskAndReply(
294 from_here, request_task, reply_task);
295 DCHECK(posted);
296 }
297
298 } // namespace util
299 } // namespace gdata
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/gdata/gdata_util.h ('k') | chrome/browser/chromeos/gdata/gdata_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698