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/google_apis/fake_drive_service.h" | 5 #include "chrome/browser/google_apis/fake_drive_service.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "chrome/browser/google_apis/gdata_wapi_parser.h" |
| 10 #include "chrome/browser/google_apis/test_util.h" |
8 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
9 | 12 |
10 using content::BrowserThread; | 13 using content::BrowserThread; |
11 | 14 |
12 namespace google_apis { | 15 namespace google_apis { |
13 | 16 |
14 FakeDriveService::FakeDriveService() { | 17 FakeDriveService::FakeDriveService() { |
15 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 18 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
16 } | 19 } |
17 | 20 |
18 FakeDriveService::~FakeDriveService() { | 21 FakeDriveService::~FakeDriveService() { |
19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
20 } | 23 } |
21 | 24 |
| 25 bool FakeDriveService::LoadResourceListForWapi( |
| 26 const std::string& relative_path) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 28 scoped_ptr<Value> raw_value = test_util::LoadJSONFile(relative_path); |
| 29 base::DictionaryValue* as_dict = NULL; |
| 30 base::Value* feed = NULL; |
| 31 base::DictionaryValue* feed_as_dict = NULL; |
| 32 |
| 33 // Extract the "feed" from the raw value and take the ownership. |
| 34 // Note that Remove() transfers the ownership to |feed|. |
| 35 if (raw_value->GetAsDictionary(&as_dict) && |
| 36 as_dict->Remove("feed", &feed) && |
| 37 feed->GetAsDictionary(&feed_as_dict)) { |
| 38 resource_list_value_.reset(feed_as_dict); |
| 39 } |
| 40 |
| 41 return resource_list_value_; |
| 42 } |
| 43 |
| 44 bool FakeDriveService::LoadAccountMetadataForWapi( |
| 45 const std::string& relative_path) { |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 47 account_metadata_value_ = test_util::LoadJSONFile(relative_path); |
| 48 return account_metadata_value_; |
| 49 } |
| 50 |
22 void FakeDriveService::Initialize(Profile* profile) { | 51 void FakeDriveService::Initialize(Profile* profile) { |
23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
24 } | 53 } |
25 | 54 |
26 void FakeDriveService::AddObserver(DriveServiceObserver* observer) { | 55 void FakeDriveService::AddObserver(DriveServiceObserver* observer) { |
27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
28 } | 57 } |
29 | 58 |
30 void FakeDriveService::RemoveObserver(DriveServiceObserver* observer) { | 59 void FakeDriveService::RemoveObserver(DriveServiceObserver* observer) { |
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 29 matching lines...) Expand all Loading... |
61 } | 90 } |
62 void FakeDriveService::GetResourceList( | 91 void FakeDriveService::GetResourceList( |
63 const GURL& feed_url, | 92 const GURL& feed_url, |
64 int64 start_changestamp, | 93 int64 start_changestamp, |
65 const std::string& search_query, | 94 const std::string& search_query, |
66 bool shared_with_me, | 95 bool shared_with_me, |
67 const std::string& directory_resource_id, | 96 const std::string& directory_resource_id, |
68 const GetResourceListCallback& callback) { | 97 const GetResourceListCallback& callback) { |
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
70 DCHECK(!callback.is_null()); | 99 DCHECK(!callback.is_null()); |
| 100 |
| 101 scoped_ptr<ResourceList> resource_list = |
| 102 ResourceList::CreateFrom(*resource_list_value_); |
| 103 MessageLoop::current()->PostTask( |
| 104 FROM_HERE, |
| 105 base::Bind(callback, |
| 106 HTTP_SUCCESS, |
| 107 base::Passed(&resource_list))); |
71 } | 108 } |
72 | 109 |
73 void FakeDriveService::GetResourceEntry( | 110 void FakeDriveService::GetResourceEntry( |
74 const std::string& resource_id, | 111 const std::string& resource_id, |
75 const GetResourceEntryCallback& callback) { | 112 const GetResourceEntryCallback& callback) { |
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
77 DCHECK(!callback.is_null()); | 114 DCHECK(!callback.is_null()); |
| 115 |
| 116 base::DictionaryValue* resource_list_dict = NULL; |
| 117 base::ListValue* entries = NULL; |
| 118 // Go through entries and return the one that matches |resource_id|. |
| 119 if (resource_list_value_->GetAsDictionary(&resource_list_dict) && |
| 120 resource_list_dict->GetList("entry", &entries)) { |
| 121 for (size_t i = 0; i < entries->GetSize(); ++i) { |
| 122 base::DictionaryValue* entry = NULL; |
| 123 base::DictionaryValue* resource_id_dict = NULL; |
| 124 std::string current_resource_id; |
| 125 if (entries->GetDictionary(i, &entry) && |
| 126 entry->GetDictionary("gd$resourceId", &resource_id_dict) && |
| 127 resource_id_dict->GetString("$t", ¤t_resource_id) && |
| 128 resource_id == current_resource_id) { |
| 129 scoped_ptr<ResourceEntry> resource_entry = |
| 130 ResourceEntry::CreateFrom(*entry); |
| 131 MessageLoop::current()->PostTask( |
| 132 FROM_HERE, |
| 133 base::Bind(callback, HTTP_SUCCESS, base::Passed(&resource_entry))); |
| 134 return; |
| 135 } |
| 136 } |
| 137 } |
| 138 |
| 139 scoped_ptr<ResourceEntry> null; |
| 140 MessageLoop::current()->PostTask( |
| 141 FROM_HERE, |
| 142 base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null))); |
78 } | 143 } |
79 | 144 |
80 void FakeDriveService::GetAccountMetadata( | 145 void FakeDriveService::GetAccountMetadata( |
81 const GetAccountMetadataCallback& callback) { | 146 const GetAccountMetadataCallback& callback) { |
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
83 DCHECK(!callback.is_null()); | 148 DCHECK(!callback.is_null()); |
| 149 |
| 150 scoped_ptr<AccountMetadataFeed> account_metadata = |
| 151 AccountMetadataFeed::CreateFrom(*account_metadata_value_); |
| 152 MessageLoop::current()->PostTask( |
| 153 FROM_HERE, |
| 154 base::Bind(callback, |
| 155 HTTP_SUCCESS, |
| 156 base::Passed(&account_metadata))); |
84 } | 157 } |
85 | 158 |
86 void FakeDriveService::GetApplicationInfo( | 159 void FakeDriveService::GetApplicationInfo( |
87 const GetDataCallback& callback) { | 160 const GetDataCallback& callback) { |
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
89 DCHECK(!callback.is_null()); | 162 DCHECK(!callback.is_null()); |
90 } | 163 } |
91 | 164 |
92 void FakeDriveService::DeleteResource( | 165 void FakeDriveService::DeleteResource( |
93 const GURL& edit_url, | 166 const GURL& edit_url, |
94 const EntryActionCallback& callback) { | 167 const EntryActionCallback& callback) { |
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
96 DCHECK(!callback.is_null()); | 169 DCHECK(!callback.is_null()); |
| 170 |
| 171 base::DictionaryValue* resource_list_dict = NULL; |
| 172 base::ListValue* entries = NULL; |
| 173 // Go through entries and remove the one that matches |edit_url|. |
| 174 if (resource_list_value_->GetAsDictionary(&resource_list_dict) && |
| 175 resource_list_dict->GetList("entry", &entries)) { |
| 176 for (size_t i = 0; i < entries->GetSize(); ++i) { |
| 177 base::DictionaryValue* entry = NULL; |
| 178 base::ListValue* links = NULL; |
| 179 if (entries->GetDictionary(i, &entry) && |
| 180 entry->GetList("link", &links)) { |
| 181 for (size_t j = 0; j < links->GetSize(); ++j) { |
| 182 base::DictionaryValue* link = NULL; |
| 183 std::string rel; |
| 184 std::string href; |
| 185 if (links->GetDictionary(j, &link) && |
| 186 link->GetString("rel", &rel) && |
| 187 link->GetString("href", &href) && |
| 188 rel == "edit" && |
| 189 GURL(href) == edit_url) { |
| 190 entries->Remove(i, NULL); |
| 191 MessageLoop::current()->PostTask( |
| 192 FROM_HERE, base::Bind(callback, HTTP_SUCCESS)); |
| 193 return; |
| 194 } |
| 195 } |
| 196 } |
| 197 } |
| 198 } |
| 199 |
| 200 MessageLoop::current()->PostTask( |
| 201 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND)); |
97 } | 202 } |
98 | 203 |
99 void FakeDriveService::DownloadHostedDocument( | 204 void FakeDriveService::DownloadHostedDocument( |
100 const FilePath& virtual_path, | 205 const FilePath& virtual_path, |
101 const FilePath& local_cache_path, | 206 const FilePath& local_cache_path, |
102 const GURL& content_url, | 207 const GURL& content_url, |
103 DocumentExportFormat format, | 208 DocumentExportFormat format, |
104 const DownloadActionCallback& callback) { | 209 const DownloadActionCallback& callback) { |
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
106 DCHECK(!callback.is_null()); | 211 DCHECK(!callback.is_null()); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 } | 275 } |
171 | 276 |
172 void FakeDriveService::AuthorizeApp(const GURL& edit_url, | 277 void FakeDriveService::AuthorizeApp(const GURL& edit_url, |
173 const std::string& app_id, | 278 const std::string& app_id, |
174 const AuthorizeAppCallback& callback) { | 279 const AuthorizeAppCallback& callback) { |
175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
176 DCHECK(!callback.is_null()); | 281 DCHECK(!callback.is_null()); |
177 } | 282 } |
178 | 283 |
179 } // namespace google_apis | 284 } // namespace google_apis |
OLD | NEW |