15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/drive/file_system_util.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind_helpers.h" 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/file_util.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h" 14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/i18n/icu_string_conversions.h" 1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/json/json_file_value_serializer.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h" 17558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "base/memory/scoped_ptr.h" 18b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "base/message_loop/message_loop_proxy.h" 193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/prefs/pref_service.h" 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h" 215e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string_util.h" 225e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/stringprintf.h" 23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/threading/sequenced_worker_pool.h" 247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/browser_process.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chromeos/drive/drive.pb.h" 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/chromeos/drive/drive_integration_service.h" 27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/drive/file_system_interface.h" 28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/chromeos/drive/job_list.h" 293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/browser/chromeos/drive/write_on_cache_file.h" 303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/browser/chromeos/profiles/profile_util.h" 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/profiles/profile_manager.h" 33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/chrome_constants.h" 34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/chrome_paths_internal.h" 353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/common/pref_names.h" 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h" 37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chromeos/chromeos_constants.h" 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h" 39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "google_apis/drive/gdata_wapi_parser.h" 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/escape.h" 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/file_system_url.h" 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread; 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace drive { 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace util { 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kDriveMountPointPath[] = "/special/drive"; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const base::FilePath::CharType kDriveMyDriveMountPointPath[] = 53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) FILE_PATH_LITERAL("/special/drive/root"); 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const base::FilePath::CharType kDriveMyDriveRootPath[] = 56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) FILE_PATH_LITERAL("drive/root"); 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)const base::FilePath::CharType kFileCacheVersionDir[] = 59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) FILE_PATH_LITERAL("v1"); 60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kSlash[] = "/"; 62a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)const char kDot = '.'; 63a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)const char kEscapedChars[] = "_"; 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const base::FilePath& GetDriveMyDriveMountPointPath() { 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CR_DEFINE_STATIC_LOCAL(base::FilePath, drive_mydrive_mount_path, 67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) (kDriveMyDriveMountPointPath)); 68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return drive_mydrive_mount_path; 69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string ReadStringFromGDocFile(const base::FilePath& file_path, 7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const std::string& key) { 7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const int64 kMaxGDocSize = 4096; 7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int64 file_size = 0; 75a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (!base::GetFileSize(file_path, &file_size) || 7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) file_size > kMaxGDocSize) { 77a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) LOG(WARNING) << "File too large to be a GDoc file " << file_path.value(); 7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return std::string(); 7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) JSONFileValueSerializer reader(file_path); 8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::string error_message; 8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) scoped_ptr<base::Value> root_value(reader.Deserialize(NULL, &error_message)); 8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (!root_value) { 85a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) LOG(WARNING) << "Failed to parse " << file_path.value() << " as JSON." 86a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) << " error = " << error_message; 8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return std::string(); 8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::DictionaryValue* dictionary_value = NULL; 9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::string result; 9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (!root_value->GetAsDictionary(&dictionary_value) || 9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) !dictionary_value->GetString(key, &result)) { 94a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) LOG(WARNING) << "No value for the given key is stored in " 95a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) << file_path.value() << ". key = " << key; 9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return std::string(); 9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return result; 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 10258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Returns DriveIntegrationService instance, if Drive is enabled. 10358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Otherwise, NULL. 10458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)DriveIntegrationService* GetIntegrationServiceByProfile(Profile* profile) { 105d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) DriveIntegrationService* service = 106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) DriveIntegrationServiceFactory::FindForProfile(profile); 107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (!service || !service->IsMounted()) 108d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return NULL; 109d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) return service; 11058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 11158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath& GetDriveGrandRootPath() { 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CR_DEFINE_STATIC_LOCAL(base::FilePath, grand_root_path, 116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) (util::kDriveGrandRootDirName)); 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return grand_root_path; 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath& GetDriveMyDriveRootPath() { 1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CR_DEFINE_STATIC_LOCAL(base::FilePath, drive_root_path, 122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) (util::kDriveMyDriveRootPath)); 1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return drive_root_path; 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath& GetDriveMountPointPath() { 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CR_DEFINE_STATIC_LOCAL(base::FilePath, drive_mount_path, 1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) (base::FilePath::FromUTF8Unsafe(kDriveMountPointPath))); 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return drive_mount_path; 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 13258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)FileSystemInterface* GetFileSystemByProfile(Profile* profile) { 13358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 13458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 13558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DriveIntegrationService* integration_service = 13658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) GetIntegrationServiceByProfile(profile); 13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return integration_service ? integration_service->file_system() : NULL; 13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 13958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 1407dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochFileSystemInterface* GetFileSystemByProfileId(void* profile_id) { 1417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 1437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // |profile_id| needs to be checked with ProfileManager::IsValidProfile 1447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // before using it. 1457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch Profile* profile = reinterpret_cast<Profile*>(profile_id); 1467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!g_browser_process->profile_manager()->IsValidProfile(profile)) 1477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch return NULL; 14858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return GetFileSystemByProfile(profile); 14958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 15058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 15158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)DriveAppRegistry* GetDriveAppRegistryByProfile(Profile* profile) { 15258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 15358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 15458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DriveIntegrationService* integration_service = 15558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) GetIntegrationServiceByProfile(profile); 15658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return integration_service ? 15758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) integration_service->drive_app_registry() : 15858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) NULL; 15958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 16058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 16158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)DriveServiceInterface* GetDriveServiceByProfile(Profile* profile) { 16258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 16358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 16458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DriveIntegrationService* integration_service = 16558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) GetIntegrationServiceByProfile(profile); 16658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return integration_service ? integration_service->drive_service() : NULL; 1677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch} 1687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool IsSpecialResourceId(const std::string& resource_id) { 170f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return resource_id == kDriveGrandRootLocalId || 171f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) resource_id == kDriveOtherDirLocalId; 172c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 173c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)ResourceEntry CreateMyDriveRootEntry(const std::string& root_resource_id) { 175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ResourceEntry mydrive_root; 176c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) mydrive_root.mutable_file_info()->set_is_directory(true); 177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) mydrive_root.set_resource_id(root_resource_id); 178f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) mydrive_root.set_parent_local_id(util::kDriveGrandRootLocalId); 179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) mydrive_root.set_title(util::kDriveMyDriveRootDirName); 180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return mydrive_root; 181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const std::string& GetDriveMountPointPathAsString() { 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CR_DEFINE_STATIC_LOCAL(std::string, drive_mount_path_string, 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (kDriveMountPointPath)); 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return drive_mount_path_string; 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)GURL FilePathToDriveURL(const base::FilePath& path) { 190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::string url(base::StringPrintf("%s:%s", 191c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) chrome::kDriveScheme, 192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) path.AsUTF8Unsafe().c_str())); 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return GURL(url); 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::FilePath DriveURLToFilePath(const GURL& url) { 197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!url.is_valid() || url.scheme() != chrome::kDriveScheme) 198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return base::FilePath(); 199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::string path_string = net::UnescapeURLComponent( 2001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) url.GetContent(), net::UnescapeRule::NORMAL); 201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return base::FilePath::FromUTF8Unsafe(path_string); 202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MaybeSetDriveURL(Profile* profile, const base::FilePath& path, GURL* url) { 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!IsUnderDriveMountPoint(path)) 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 21058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) FileSystemInterface* file_system = GetFileSystemByProfile(profile); 211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!file_system) 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) *url = FilePathToDriveURL(util::ExtractDrivePath(path)); 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool IsUnderDriveMountPoint(const base::FilePath& path) { 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return GetDriveMountPointPath() == path || 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetDriveMountPointPath().IsParent(path); 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool NeedsNamespaceMigration(const base::FilePath& path) { 223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Before migration, "My Drive" which was represented as "drive. 224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // The user might use some path pointing a directory in "My Drive". 225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // e.g. "drive/downloads_dir" 226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // We changed the path for the "My Drive" to "drive/root", hence the user pref 227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // pointing to the old path needs update to the new path. 228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // e.g. "drive/root/downloads_dir" 229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // If |path| already points to some directory in "drive/root", there's no need 230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // to update it. 231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return IsUnderDriveMountPoint(path) && 232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) !(GetDriveMyDriveMountPointPath() == path || 233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) GetDriveMyDriveMountPointPath().IsParent(path)); 2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::FilePath ConvertToMyDriveNamespace(const base::FilePath& path) { 237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(NeedsNamespaceMigration(path)); 2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Need to migrate "/special/drive(.*)" to "/special/drive/root(.*)". 2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Append the relative path from "/special/drive". 2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath new_path(GetDriveMyDriveMountPointPath()); 2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GetDriveMountPointPath().AppendRelativePath(path, &new_path); 2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DVLOG(1) << "Migrate download.default_directory setting from " 2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) << path.AsUTF8Unsafe() << " to " << new_path.AsUTF8Unsafe(); 2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!NeedsNamespaceMigration(new_path)); 2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return new_path; 2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::FilePath ExtractDrivePath(const base::FilePath& path) { 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!IsUnderDriveMountPoint(path)) 2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return base::FilePath(); 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) base::FilePath drive_path = GetDriveGrandRootPath(); 254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) GetDriveMountPointPath().AppendRelativePath(path, &drive_path); 255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return drive_path; 2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::FilePath ExtractDrivePathFromFileSystemUrl( 259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const fileapi::FileSystemURL& url) { 260c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!url.is_valid() || url.type() != fileapi::kFileSystemTypeDrive) 261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return base::FilePath(); 262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return ExtractDrivePath(url.path()); 263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)base::FilePath GetCacheRootPath(Profile* profile) { 266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath cache_base_path; 267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) chrome::GetUserCacheDirectory(profile->GetPath(), &cache_base_path); 268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath cache_root_path = 269a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) cache_base_path.Append(chromeos::kDriveCacheDirname); 270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return cache_root_path.Append(kFileCacheVersionDir); 271c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string EscapeCacheFileName(const std::string& filename) { 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This is based on net/base/escape.cc: net::(anonymous namespace)::Escape 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string escaped; 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (size_t i = 0; i < filename.size(); ++i) { 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char c = filename[i]; 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (c == '%' || c == '.' || c == '/') { 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::StringAppendF(&escaped, "%%%02X", c); 2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) escaped.push_back(c); 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return escaped; 2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string UnescapeCacheFileName(const std::string& filename) { 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string unescaped; 2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (size_t i = 0; i < filename.size(); ++i) { 2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char c = filename[i]; 2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (c == '%' && i + 2 < filename.length()) { 2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) c = (HexDigitToInt(filename[i + 1]) << 4) + 2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HexDigitToInt(filename[i + 2]); 2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) i += 2; 2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unescaped.push_back(c); 2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return unescaped; 2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 301868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::string NormalizeFileName(const std::string& input) { 302868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) DCHECK(IsStringUTF8(input)); 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 304868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::string output; 305868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!base::ConvertToUtf8AndNormalize(input, base::kCodepageUTF8, &output)) 306868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) output = input; 307a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::ReplaceChars(output, kSlash, std::string(kEscapedChars), &output); 308a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (!output.empty() && output.find_first_not_of(kDot, 0) == std::string::npos) 309a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) output = kEscapedChars; 310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return output; 3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PrepareWritableFileAndRun(Profile* profile, 3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& path, 3153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const PrepareWritableFileCallback& callback) { 3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!callback.is_null()); 3183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 31958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) FileSystemInterface* file_system = GetFileSystemByProfile(profile); 3203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (!file_system || !IsUnderDriveMountPoint(path)) { 3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::BrowserThread::GetBlockingPool()->PostTask( 3223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) FROM_HERE, base::Bind(callback, FILE_ERROR_FAILED, base::FilePath())); 3233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return; 3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 3253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 3263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) WriteOnCacheFile(file_system, 3273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) ExtractDrivePath(path), 3283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) std::string(), // mime_type 3293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) callback); 3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void EnsureDirectoryExists(Profile* profile, 3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& directory, 3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const FileOperationCallback& callback) { 335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!callback.is_null()); 3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (IsUnderDriveMountPoint(directory)) { 33858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) FileSystemInterface* file_system = GetFileSystemByProfile(profile); 3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(file_system); 3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_system->CreateDirectory( 3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ExtractDrivePath(directory), 3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) true /* is_exclusive */, 3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) true /* is_recursive */, 3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) callback); 3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::MessageLoopProxy::current()->PostTask( 347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) FROM_HERE, base::Bind(callback, FILE_ERROR_OK)); 3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void EmptyFileOperationCallback(FileError error) { 3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 35490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CreateGDocFile(const base::FilePath& file_path, 35590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const GURL& url, 35690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const std::string& resource_id) { 35790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::string content = base::StringPrintf( 35890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) "{\"url\": \"%s\", \"resource_id\": \"%s\"}", 35990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) url.spec().c_str(), resource_id.c_str()); 36090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return file_util::WriteFile(file_path, content.data(), content.size()) == 36190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) static_cast<int>(content.size()); 36290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 36390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 36490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool HasGDocFileExtension(const base::FilePath& file_path) { 36590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return google_apis::ResourceEntry::ClassifyEntryKindByFileExtension( 36690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) file_path) & 36790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) google_apis::ResourceEntry::KIND_OF_HOSTED_DOCUMENT; 36890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 36990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 37090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)GURL ReadUrlFromGDocFile(const base::FilePath& file_path) { 37190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return GURL(ReadStringFromGDocFile(file_path, "url")); 37290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 37390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 37490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string ReadResourceIdFromGDocFile(const base::FilePath& file_path) { 37590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return ReadStringFromGDocFile(file_path, "resource_id"); 37690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 37790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool IsDriveEnabledForProfile(Profile* profile) { 3793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 3803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 3813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (!chromeos::IsProfileAssociatedWithGaiaAccount(profile)) 3823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return false; 3833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 3843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Disable Drive if preference is set. This can happen with commandline flag 3853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // --disable-drive or enterprise policy, or with user settings. 3863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (profile->GetPrefs()->GetBoolean(prefs::kDisableDrive)) 3873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return false; 3883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 3893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return true; 3903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 3913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 392a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)ConnectionStatusType GetDriveConnectionStatus(Profile* profile) { 393a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) drive::DriveServiceInterface* const drive_service = 394a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) drive::util::GetDriveServiceByProfile(profile); 395a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 396a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (!drive_service) 397a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) return DRIVE_DISCONNECTED_NOSERVICE; 398a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (net::NetworkChangeNotifier::IsOffline()) 399a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) return DRIVE_DISCONNECTED_NONETWORK; 400a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (!drive_service->CanSendRequest()) 401a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) return DRIVE_DISCONNECTED_NOTREADY; 402a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 403a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const bool is_connection_cellular = 404a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) net::NetworkChangeNotifier::IsConnectionCellular( 405a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) net::NetworkChangeNotifier::GetConnectionType()); 406a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const bool disable_sync_over_celluar = 407a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) profile->GetPrefs()->GetBoolean(prefs::kDisableDriveOverCellular); 408a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 409a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (is_connection_cellular && disable_sync_over_celluar) 410a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) return DRIVE_CONNECTED_METERED; 411a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) return DRIVE_CONNECTED; 412a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)} 413a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace util 4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace drive 416