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