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 | 73 |
74 drive_uploader_.reset(new google_apis::DriveUploader(drive_service_.get())); | 74 drive_uploader_.reset(new google_apis::DriveUploader(drive_service_.get())); |
75 } | 75 } |
76 | 76 |
77 scoped_ptr<DriveFileSyncClient> DriveFileSyncClient::CreateForTesting( | 77 scoped_ptr<DriveFileSyncClient> DriveFileSyncClient::CreateForTesting( |
78 Profile* profile, | 78 Profile* profile, |
| 79 const GURL& base_url, |
79 scoped_ptr<google_apis::DriveServiceInterface> drive_service, | 80 scoped_ptr<google_apis::DriveServiceInterface> drive_service, |
80 scoped_ptr<google_apis::DriveUploaderInterface> drive_uploader) { | 81 scoped_ptr<google_apis::DriveUploaderInterface> drive_uploader) { |
81 return make_scoped_ptr(new DriveFileSyncClient( | 82 return make_scoped_ptr(new DriveFileSyncClient( |
82 profile, drive_service.Pass(), drive_uploader.Pass())); | 83 profile, base_url, drive_service.Pass(), drive_uploader.Pass())); |
83 } | 84 } |
84 | 85 |
85 DriveFileSyncClient::DriveFileSyncClient( | 86 DriveFileSyncClient::DriveFileSyncClient( |
86 Profile* profile, | 87 Profile* profile, |
| 88 const GURL& base_url, |
87 scoped_ptr<google_apis::DriveServiceInterface> drive_service, | 89 scoped_ptr<google_apis::DriveServiceInterface> drive_service, |
88 scoped_ptr<google_apis::DriveUploaderInterface> drive_uploader) { | 90 scoped_ptr<google_apis::DriveUploaderInterface> drive_uploader) |
| 91 : url_generator_(base_url) { |
89 drive_service_ = drive_service.Pass(); | 92 drive_service_ = drive_service.Pass(); |
90 drive_service_->Initialize(profile); | 93 drive_service_->Initialize(profile); |
91 | 94 |
92 drive_uploader_ = drive_uploader.Pass(); | 95 drive_uploader_ = drive_uploader.Pass(); |
93 } | 96 } |
94 | 97 |
95 DriveFileSyncClient::~DriveFileSyncClient() { | 98 DriveFileSyncClient::~DriveFileSyncClient() { |
96 DCHECK(CalledOnValidThread()); | 99 DCHECK(CalledOnValidThread()); |
97 drive_service_->CancelAll(); | 100 drive_service_->CancelAll(); |
98 } | 101 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 google_apis::GDataErrorCode error, | 133 google_apis::GDataErrorCode error, |
131 scoped_ptr<google_apis::DocumentFeed> feed) { | 134 scoped_ptr<google_apis::DocumentFeed> feed) { |
132 DCHECK(CalledOnValidThread()); | 135 DCHECK(CalledOnValidThread()); |
133 DCHECK(IsStringASCII(directory_name)); | 136 DCHECK(IsStringASCII(directory_name)); |
134 | 137 |
135 if (error != google_apis::HTTP_SUCCESS) { | 138 if (error != google_apis::HTTP_SUCCESS) { |
136 callback.Run(error, std::string()); | 139 callback.Run(error, std::string()); |
137 return; | 140 return; |
138 } | 141 } |
139 | 142 |
| 143 GURL parent_link; |
| 144 if (!parent_resource_id.empty()) |
| 145 parent_link = ResourceIdToResourceLink(parent_resource_id); |
140 google_apis::DocumentEntry* entry = GetDocumentByTitleAndParent( | 146 google_apis::DocumentEntry* entry = GetDocumentByTitleAndParent( |
141 feed->entries(), parent_resource_id, ASCIIToUTF16(directory_name)); | 147 feed->entries(), parent_link, ASCIIToUTF16(directory_name)); |
142 if (!entry) { | 148 if (!entry) { |
143 if (parent_resource_id.empty()) { | 149 if (parent_resource_id.empty()) { |
144 // Use empty content URL for root directory. | 150 // Use empty content URL for root directory. |
145 drive_service_->AddNewDirectory( | 151 drive_service_->AddNewDirectory( |
146 GURL(), // parent_content_url | 152 GURL(), // parent_content_url |
147 FilePath().AppendASCII(directory_name).value(), | 153 FilePath().AppendASCII(directory_name).value(), |
148 base::Bind(&DriveFileSyncClient::DidCreateDirectory, | 154 base::Bind(&DriveFileSyncClient::DidCreateDirectory, |
149 AsWeakPtr(), callback)); | 155 AsWeakPtr(), callback)); |
150 return; | 156 return; |
151 } | 157 } |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 std::string DriveFileSyncClient::OriginToDirectoryTitle(const GURL& origin) { | 368 std::string DriveFileSyncClient::OriginToDirectoryTitle(const GURL& origin) { |
363 DCHECK(origin.SchemeIs(extensions::kExtensionScheme)); | 369 DCHECK(origin.SchemeIs(extensions::kExtensionScheme)); |
364 return origin.host(); | 370 return origin.host(); |
365 } | 371 } |
366 | 372 |
367 // static | 373 // static |
368 GURL DriveFileSyncClient::DirectoryTitleToOrigin(const std::string& title) { | 374 GURL DriveFileSyncClient::DirectoryTitleToOrigin(const std::string& title) { |
369 return extensions::Extension::GetBaseURLFromExtensionId(title); | 375 return extensions::Extension::GetBaseURLFromExtensionId(title); |
370 } | 376 } |
371 | 377 |
| 378 GURL DriveFileSyncClient::ResourceIdToResourceLink( |
| 379 const std::string& resource_id) const { |
| 380 return url_generator_.GenerateDocumentEntryUrl(resource_id); |
| 381 } |
| 382 |
372 void DriveFileSyncClient::DidGetDocumentFeedData( | 383 void DriveFileSyncClient::DidGetDocumentFeedData( |
373 const DocumentFeedCallback& callback, | 384 const DocumentFeedCallback& callback, |
374 google_apis::GDataErrorCode error, | 385 google_apis::GDataErrorCode error, |
375 scoped_ptr<base::Value> data) { | 386 scoped_ptr<base::Value> data) { |
376 DCHECK(CalledOnValidThread()); | 387 DCHECK(CalledOnValidThread()); |
377 | 388 |
378 if (error != google_apis::HTTP_SUCCESS) { | 389 if (error != google_apis::HTTP_SUCCESS) { |
379 callback.Run(error, scoped_ptr<google_apis::DocumentFeed>()); | 390 callback.Run(error, scoped_ptr<google_apis::DocumentFeed>()); |
380 return; | 391 return; |
381 } | 392 } |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 } | 622 } |
612 | 623 |
613 void DriveFileSyncClient::DidDeleteFile( | 624 void DriveFileSyncClient::DidDeleteFile( |
614 const GDataErrorCallback& callback, | 625 const GDataErrorCallback& callback, |
615 google_apis::GDataErrorCode error) { | 626 google_apis::GDataErrorCode error) { |
616 DCHECK(CalledOnValidThread()); | 627 DCHECK(CalledOnValidThread()); |
617 callback.Run(error); | 628 callback.Run(error); |
618 } | 629 } |
619 | 630 |
620 } // namespace sync_file_system | 631 } // namespace sync_file_system |
OLD | NEW |