| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync_file_system/drive_file_sync_client.h" | 5 #include "chrome/browser/sync_file_system/drive_file_sync_client.h" |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 | 8 |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/google_apis/drive_uploader.h" | 12 #include "chrome/browser/google_apis/drive_uploader.h" |
| 13 #include "chrome/browser/google_apis/gdata_wapi_service.h" | 13 #include "chrome/browser/google_apis/gdata_wapi_service.h" |
| 14 #include "chrome/browser/google_apis/gdata_wapi_url_generator.h" | 14 #include "chrome/browser/google_apis/gdata_wapi_url_generator.h" |
| 15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "extensions/common/constants.h" | 16 #include "extensions/common/constants.h" |
| 17 #include "net/base/escape.h" | 17 #include "net/base/escape.h" |
| 18 #include "net/base/mime_util.h" | 18 #include "net/base/mime_util.h" |
| 19 | 19 |
| 20 namespace sync_file_system { | 20 namespace sync_file_system { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const char kRootResourceId[] = ""; | 24 const char kRootResourceId[] = ""; |
| 25 const char kSyncRootDirectoryName[] = "Chrome Syncable FileSystem"; | 25 const char kSyncRootDirectoryName[] = "Chrome Syncable FileSystem"; |
| 26 const char kResourceLinkPrefix[] = | |
| 27 "https://docs.google.com/feeds/default/private/full/"; | |
| 28 const char kMimeTypeOctetStream[] = "application/octet-stream"; | 26 const char kMimeTypeOctetStream[] = "application/octet-stream"; |
| 29 | 27 |
| 30 // This path is not actually used but is required by DriveUploaderInterface. | 28 // This path is not actually used but is required by DriveUploaderInterface. |
| 31 const FilePath::CharType kDummyDrivePath[] = | 29 const FilePath::CharType kDummyDrivePath[] = |
| 32 FILE_PATH_LITERAL("/dummy/drive/path"); | 30 FILE_PATH_LITERAL("/dummy/drive/path"); |
| 33 | 31 |
| 34 bool HasParentLinkTo(const ScopedVector<google_apis::Link>& links, | 32 bool HasParentLinkTo(const ScopedVector<google_apis::Link>& links, |
| 35 const std::string& parent_resource_id) { | 33 const GURL& parent_link) { |
| 36 bool should_not_have_parent = parent_resource_id.empty(); | 34 bool should_not_have_parent = parent_link.is_empty(); |
| 37 GURL parent_link(kResourceLinkPrefix + net::EscapePath(parent_resource_id)); | |
| 38 | 35 |
| 39 for (ScopedVector<google_apis::Link>::const_iterator itr = links.begin(); | 36 for (ScopedVector<google_apis::Link>::const_iterator itr = links.begin(); |
| 40 itr != links.end(); ++itr) { | 37 itr != links.end(); ++itr) { |
| 41 if ((*itr)->type() == google_apis::Link::LINK_PARENT) { | 38 if ((*itr)->type() == google_apis::Link::LINK_PARENT) { |
| 42 if (should_not_have_parent) | 39 if (should_not_have_parent) |
| 43 return false; | 40 return false; |
| 44 if ((*itr)->href() == parent_link) | 41 if ((*itr)->href().GetOrigin() == parent_link.GetOrigin() && |
| 42 (*itr)->href().path() == parent_link.path()) |
| 45 return true; | 43 return true; |
| 46 } | 44 } |
| 47 } | 45 } |
| 48 | 46 |
| 49 return should_not_have_parent; | 47 return should_not_have_parent; |
| 50 } | 48 } |
| 51 | 49 |
| 52 google_apis::DocumentEntry* GetDocumentByTitleAndParent( | 50 google_apis::DocumentEntry* GetDocumentByTitleAndParent( |
| 53 const ScopedVector<google_apis::DocumentEntry>& entries, | 51 const ScopedVector<google_apis::DocumentEntry>& entries, |
| 54 const std::string& parent_resource_id, | 52 const GURL& parent_link, |
| 55 const string16& title) { | 53 const string16& title) { |
| 56 typedef ScopedVector<google_apis::DocumentEntry>::const_iterator iterator; | 54 typedef ScopedVector<google_apis::DocumentEntry>::const_iterator iterator; |
| 57 for (iterator itr = entries.begin(); itr != entries.end(); ++itr) { | 55 for (iterator itr = entries.begin(); itr != entries.end(); ++itr) { |
| 58 if ((*itr)->title() == title && | 56 if ((*itr)->title() == title && |
| 59 HasParentLinkTo((*itr)->links(), parent_resource_id)) { | 57 HasParentLinkTo((*itr)->links(), parent_link)) { |
| 60 return *itr; | 58 return *itr; |
| 61 } | 59 } |
| 62 } | 60 } |
| 63 return NULL; | 61 return NULL; |
| 64 } | 62 } |
| 65 | 63 |
| 66 } // namespace | 64 } // namespace |
| 67 | 65 |
| 68 DriveFileSyncClient::DriveFileSyncClient(Profile* profile) { | 66 DriveFileSyncClient::DriveFileSyncClient(Profile* profile) |
| 67 : url_generator_(GURL( |
| 68 google_apis::GDataWapiUrlGenerator::kBaseUrlForProduction)) { |
| 69 drive_service_.reset(new google_apis::GDataWapiService( | 69 drive_service_.reset(new google_apis::GDataWapiService( |
| 70 GURL(google_apis::GDataWapiUrlGenerator::kBaseUrlForProduction), | 70 GURL(google_apis::GDataWapiUrlGenerator::kBaseUrlForProduction), |
| 71 "" /* custom_user_agent */)); | 71 "" /* custom_user_agent */)); |
| 72 drive_service_->Initialize(profile); | 72 drive_service_->Initialize(profile); |
| 73 drive_service_->AddObserver(this); | 73 drive_service_->AddObserver(this); |
| 74 | 74 |
| 75 drive_uploader_.reset(new google_apis::DriveUploader(drive_service_.get())); | 75 drive_uploader_.reset(new google_apis::DriveUploader(drive_service_.get())); |
| 76 } | 76 } |
| 77 | 77 |
| 78 scoped_ptr<DriveFileSyncClient> DriveFileSyncClient::CreateForTesting( | 78 scoped_ptr<DriveFileSyncClient> DriveFileSyncClient::CreateForTesting( |
| 79 Profile* profile, | 79 Profile* profile, |
| 80 const GURL& base_url, |
| 80 scoped_ptr<google_apis::DriveServiceInterface> drive_service, | 81 scoped_ptr<google_apis::DriveServiceInterface> drive_service, |
| 81 scoped_ptr<google_apis::DriveUploaderInterface> drive_uploader) { | 82 scoped_ptr<google_apis::DriveUploaderInterface> drive_uploader) { |
| 82 return make_scoped_ptr(new DriveFileSyncClient( | 83 return make_scoped_ptr(new DriveFileSyncClient( |
| 83 profile, drive_service.Pass(), drive_uploader.Pass())); | 84 profile, base_url, drive_service.Pass(), drive_uploader.Pass())); |
| 84 } | 85 } |
| 85 | 86 |
| 86 DriveFileSyncClient::DriveFileSyncClient( | 87 DriveFileSyncClient::DriveFileSyncClient( |
| 87 Profile* profile, | 88 Profile* profile, |
| 89 const GURL& base_url, |
| 88 scoped_ptr<google_apis::DriveServiceInterface> drive_service, | 90 scoped_ptr<google_apis::DriveServiceInterface> drive_service, |
| 89 scoped_ptr<google_apis::DriveUploaderInterface> drive_uploader) { | 91 scoped_ptr<google_apis::DriveUploaderInterface> drive_uploader) |
| 92 : url_generator_(base_url) { |
| 90 drive_service_ = drive_service.Pass(); | 93 drive_service_ = drive_service.Pass(); |
| 91 drive_service_->Initialize(profile); | 94 drive_service_->Initialize(profile); |
| 92 drive_service_->AddObserver(this); | 95 drive_service_->AddObserver(this); |
| 93 | 96 |
| 94 drive_uploader_ = drive_uploader.Pass(); | 97 drive_uploader_ = drive_uploader.Pass(); |
| 95 } | 98 } |
| 96 | 99 |
| 97 DriveFileSyncClient::~DriveFileSyncClient() { | 100 DriveFileSyncClient::~DriveFileSyncClient() { |
| 98 DCHECK(CalledOnValidThread()); | 101 DCHECK(CalledOnValidThread()); |
| 99 drive_service_->RemoveObserver(this); | 102 drive_service_->RemoveObserver(this); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 google_apis::GDataErrorCode error, | 147 google_apis::GDataErrorCode error, |
| 145 scoped_ptr<google_apis::DocumentFeed> feed) { | 148 scoped_ptr<google_apis::DocumentFeed> feed) { |
| 146 DCHECK(CalledOnValidThread()); | 149 DCHECK(CalledOnValidThread()); |
| 147 DCHECK(IsStringASCII(directory_name)); | 150 DCHECK(IsStringASCII(directory_name)); |
| 148 | 151 |
| 149 if (error != google_apis::HTTP_SUCCESS) { | 152 if (error != google_apis::HTTP_SUCCESS) { |
| 150 callback.Run(error, std::string()); | 153 callback.Run(error, std::string()); |
| 151 return; | 154 return; |
| 152 } | 155 } |
| 153 | 156 |
| 157 GURL parent_link; |
| 158 if (!parent_resource_id.empty()) |
| 159 parent_link = ResourceIdToResourceLink(parent_resource_id); |
| 154 google_apis::DocumentEntry* entry = GetDocumentByTitleAndParent( | 160 google_apis::DocumentEntry* entry = GetDocumentByTitleAndParent( |
| 155 feed->entries(), parent_resource_id, ASCIIToUTF16(directory_name)); | 161 feed->entries(), parent_link, ASCIIToUTF16(directory_name)); |
| 156 if (!entry) { | 162 if (!entry) { |
| 157 if (parent_resource_id.empty()) { | 163 if (parent_resource_id.empty()) { |
| 158 // Use empty content URL for root directory. | 164 // Use empty content URL for root directory. |
| 159 drive_service_->AddNewDirectory( | 165 drive_service_->AddNewDirectory( |
| 160 GURL(), // parent_content_url | 166 GURL(), // parent_content_url |
| 161 FilePath().AppendASCII(directory_name).value(), | 167 FilePath().AppendASCII(directory_name).value(), |
| 162 base::Bind(&DriveFileSyncClient::DidCreateDirectory, | 168 base::Bind(&DriveFileSyncClient::DidCreateDirectory, |
| 163 AsWeakPtr(), callback)); | 169 AsWeakPtr(), callback)); |
| 164 return; | 170 return; |
| 165 } | 171 } |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 std::string DriveFileSyncClient::OriginToDirectoryTitle(const GURL& origin) { | 382 std::string DriveFileSyncClient::OriginToDirectoryTitle(const GURL& origin) { |
| 377 DCHECK(origin.SchemeIs(extensions::kExtensionScheme)); | 383 DCHECK(origin.SchemeIs(extensions::kExtensionScheme)); |
| 378 return origin.host(); | 384 return origin.host(); |
| 379 } | 385 } |
| 380 | 386 |
| 381 // static | 387 // static |
| 382 GURL DriveFileSyncClient::DirectoryTitleToOrigin(const std::string& title) { | 388 GURL DriveFileSyncClient::DirectoryTitleToOrigin(const std::string& title) { |
| 383 return extensions::Extension::GetBaseURLFromExtensionId(title); | 389 return extensions::Extension::GetBaseURLFromExtensionId(title); |
| 384 } | 390 } |
| 385 | 391 |
| 392 GURL DriveFileSyncClient::ResourceIdToResourceLink( |
| 393 const std::string& resource_id) const { |
| 394 return url_generator_.GenerateDocumentEntryUrl(resource_id); |
| 395 } |
| 396 |
| 386 void DriveFileSyncClient::OnReadyToPerformOperations() { | 397 void DriveFileSyncClient::OnReadyToPerformOperations() { |
| 387 DCHECK(CalledOnValidThread()); | 398 DCHECK(CalledOnValidThread()); |
| 388 FOR_EACH_OBSERVER(DriveFileSyncClientObserver, observers_, OnAuthenticated()); | 399 FOR_EACH_OBSERVER(DriveFileSyncClientObserver, observers_, OnAuthenticated()); |
| 389 } | 400 } |
| 390 | 401 |
| 391 void DriveFileSyncClient::DidGetDocumentFeedData( | 402 void DriveFileSyncClient::DidGetDocumentFeedData( |
| 392 const DocumentFeedCallback& callback, | 403 const DocumentFeedCallback& callback, |
| 393 google_apis::GDataErrorCode error, | 404 google_apis::GDataErrorCode error, |
| 394 scoped_ptr<base::Value> data) { | 405 scoped_ptr<base::Value> data) { |
| 395 DCHECK(CalledOnValidThread()); | 406 DCHECK(CalledOnValidThread()); |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 } | 641 } |
| 631 | 642 |
| 632 void DriveFileSyncClient::DidDeleteFile( | 643 void DriveFileSyncClient::DidDeleteFile( |
| 633 const GDataErrorCallback& callback, | 644 const GDataErrorCallback& callback, |
| 634 google_apis::GDataErrorCode error) { | 645 google_apis::GDataErrorCode error) { |
| 635 DCHECK(CalledOnValidThread()); | 646 DCHECK(CalledOnValidThread()); |
| 636 callback.Run(error); | 647 callback.Run(error); |
| 637 } | 648 } |
| 638 | 649 |
| 639 } // namespace sync_file_system | 650 } // namespace sync_file_system |
| OLD | NEW |