Chromium Code Reviews| Index: chrome/browser/google_apis/fake_drive_service.cc |
| diff --git a/chrome/browser/google_apis/fake_drive_service.cc b/chrome/browser/google_apis/fake_drive_service.cc |
| index 4be7bfd2935d316c427fa373c5ba3e98c5ee5d11..52b38b7a83f910127ae5016315d1f2468dcdc0f6 100644 |
| --- a/chrome/browser/google_apis/fake_drive_service.cc |
| +++ b/chrome/browser/google_apis/fake_drive_service.cc |
| @@ -23,6 +23,11 @@ using content::BrowserThread; |
| namespace google_apis { |
| namespace { |
| + |
| +// Rel property of upload link in the entries dictionary value. |
| +const char kUploadUrlRel[] = |
| + "http://schemas.google.com/g/2005#resumable-create-media"; |
| + |
| // Returns true if a resource entry matches with the search query. |
| // Supports queries consist of following format. |
| // - Phrases quoted by double/single quotes |
| @@ -774,12 +779,146 @@ void FakeDriveService::InitiateUpload( |
| const InitiateUploadCallback& callback) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| DCHECK(!callback.is_null()); |
| + |
| + if (offline_) { |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback, GDATA_NO_CONNECTION, GURL())); |
| + return; |
| + } |
| + |
| + DictionaryValue* entry = FindEntryByUploadUrl(params.upload_location); |
| + if (!entry) { |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback, HTTP_NOT_FOUND, GURL())); |
| + return; |
| + } |
| + |
| + // If the title is empty, the passed upload location is the location of the |
| + // (existing) file that will be uploaded. |
| + if (params.title.empty()) { |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback, HTTP_SUCCESS, params.upload_location)); |
| + return; |
| + } |
| + |
| + // If the title was set, the upload_location is the location of the parent |
| + // directory of the file that will be uploaded. The file does not yet exist |
| + // and it must be created. Its title will be the passed title param. |
| + std::string parent_resource_id; |
| + entry->GetString("gd$resourceId.$t", &parent_resource_id); |
| + |
| + std::string resource_id = GetNewResourceId(); |
| + GURL upload_url = GURL("https://xxx/upload/" + resource_id); |
| + |
| + scoped_ptr<base::DictionaryValue> new_entry(new base::DictionaryValue); |
| + // Set the resource ID and the title |
| + new_entry->SetString("gd$resourceId.$t", resource_id); |
| + new_entry->SetString("title.$t", params.title); |
| + new_entry->SetString("docs$filename", params.title); |
| + new_entry->SetString("docs$size", "0"); |
| + new_entry->SetString("docs$md5Checksum.$t", |
| + "3b4385ebefec6e743574c76bbd0575de"); |
| + |
| + // Add "category" which sets the resource type to file. |
| + base::ListValue* categories = new base::ListValue; |
| + base::DictionaryValue* category = new base::DictionaryValue; |
| + category->SetString("label", "test/foo"); |
| + category->SetString("scheme", "http://schemas.google.com/g/2005#kind"); |
| + category->SetString("term", "http://schemas.google.com/docs/2007#file"); |
| + categories->Append(category); |
| + new_entry->Set("category", categories); |
| + |
| + // Add "content" which sets the content URL. |
| + base::DictionaryValue* content = new base::DictionaryValue; |
| + content->SetString("src", "https://xxx/content/" + resource_id); |
| + content->SetString("type", "test/foo"); |
| + new_entry->Set("content", content); |
| + |
| + // Add "link" which sets the parent URL, the edit URL and the upload URL. |
| + base::ListValue* links = new base::ListValue; |
| + if (parent_resource_id != GetRootResourceId()) { |
| + base::DictionaryValue* parent_link = new base::DictionaryValue; |
| + parent_link->SetString("href", GetFakeLinkUrl(parent_resource_id).spec()); |
| + parent_link->SetString("rel", |
| + "http://schemas.google.com/docs/2007#parent"); |
| + links->Append(parent_link); |
| + } |
| + |
| + base::DictionaryValue* edit_link = new base::DictionaryValue; |
| + edit_link->SetString("href", "https://xxx/edit/" + resource_id); |
| + edit_link->SetString("rel", "edit"); |
| + links->Append(edit_link); |
| + |
| + base::DictionaryValue* upload_link = new base::DictionaryValue; |
| + upload_link->SetString("href", upload_url.spec()); |
| + upload_link->SetString("rel", kUploadUrlRel); |
| + links->Append(upload_link); |
| + new_entry->Set("link", links); |
| + |
| + AddNewChangestamp(new_entry.get()); |
| + |
| + base::DictionaryValue* resource_list_dict = NULL; |
| + base::ListValue* entries = NULL; |
| + if (!resource_list_value_->GetAsDictionary(&resource_list_dict)) { |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback, HTTP_NOT_FOUND, GURL())); |
| + return; |
| + } |
| + |
| + // If there are no entries, prepare an empty entry to add. |
| + if (!resource_list_dict->HasKey("entry")) |
| + resource_list_dict->Set("entry", new ListValue); |
| + |
| + if (resource_list_dict->GetList("entry", &entries)) |
| + entries->Append(new_entry.release()); |
| + |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback, HTTP_SUCCESS, upload_url)); |
| } |
| void FakeDriveService::ResumeUpload(const ResumeUploadParams& params, |
| const ResumeUploadCallback& callback) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| DCHECK(!callback.is_null()); |
| + |
| + scoped_ptr<ResourceEntry> result_entry; |
| + GDataErrorCode return_code = HTTP_SUCCESS; |
| + |
| + if (offline_) |
| + return_code = GDATA_NO_CONNECTION; |
|
satorux1
2013/02/06 07:47:55
I'd suggest to return here
if (offline_) {
Mess
tonibarzic
2013/02/07 08:31:36
Done.
|
| + |
| + DictionaryValue* entry = NULL; |
| + if (return_code == HTTP_SUCCESS) { |
| + entry = FindEntryByUploadUrl(params.upload_location); |
| + if (!entry) |
| + return_code = HTTP_NOT_FOUND; |
|
satorux1
2013/02/06 07:47:55
like wise. I'd suggest to return when failed.
tonibarzic
2013/02/07 08:31:36
Done.
|
| + } |
| + |
| + if (return_code == HTTP_SUCCESS) { |
| + entry->SetString("docs$size", base::Int64ToString(params.end_position)); |
| + |
| + if (params.content_length != params.end_position) |
| + return_code = HTTP_RESUME_INCOMPLETE; |
| + } |
| + |
| + if (return_code == HTTP_SUCCESS) { |
| + result_entry = ResourceEntry::CreateFrom(*entry).Pass(); |
| + if (params.upload_mode == UPLOAD_NEW_FILE) |
| + return_code = HTTP_CREATED; |
| + } |
| + |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback, |
| + ResumeUploadResponse(return_code, |
| + params.start_position, |
| + params.end_position), |
| + base::Passed(&result_entry))); |
| } |
| void FakeDriveService::AuthorizeApp(const GURL& edit_url, |
| @@ -835,6 +974,42 @@ base::DictionaryValue* FakeDriveService::FindEntryByContentUrl( |
| return NULL; |
| } |
| +base::DictionaryValue* FakeDriveService::FindEntryByUploadUrl( |
| + const GURL& upload_url) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + base::DictionaryValue* resource_list_dict = NULL; |
| + base::ListValue* entries = NULL; |
| + // Go through entries and return the one that matches |upload_url|. |
| + if (resource_list_value_->GetAsDictionary(&resource_list_dict) && |
| + resource_list_dict->GetList("entry", &entries)) { |
| + for (size_t i = 0; i < entries->GetSize(); ++i) { |
| + base::DictionaryValue* entry = NULL; |
| + base::ListValue* links = NULL; |
| + if (entries->GetDictionary(i, &entry) && |
| + entry->GetList("link", &links) && |
| + links) { |
| + for (size_t link_index = 0; |
| + link_index < links->GetSize(); |
| + ++link_index) { |
| + base::DictionaryValue* link = NULL; |
| + std::string rel; |
| + std::string found_upload_url; |
| + if (links->GetDictionary(link_index, &link) && |
| + link && link->GetString("rel", &rel) && |
| + rel == kUploadUrlRel && |
| + link->GetString("href", &found_upload_url) && |
| + GURL(found_upload_url) == upload_url) { |
| + return entry; |
| + } |
| + } |
| + } |
| + } |
| + } |
| + |
| + return NULL; |
| +} |
| + |
| std::string FakeDriveService::GetNewResourceId() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |