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/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_split.h" |
11 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/google_apis/drive_api_parser.h" | 14 #include "chrome/browser/google_apis/drive_api_parser.h" |
14 #include "chrome/browser/google_apis/gdata_wapi_parser.h" | 15 #include "chrome/browser/google_apis/gdata_wapi_parser.h" |
15 #include "chrome/browser/google_apis/test_util.h" | 16 #include "chrome/browser/google_apis/test_util.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
18 | 19 |
19 using content::BrowserThread; | 20 using content::BrowserThread; |
20 | 21 |
21 namespace google_apis { | 22 namespace google_apis { |
22 | 23 |
23 FakeDriveService::FakeDriveService() | 24 FakeDriveService::FakeDriveService() |
24 : largest_changestamp_(0), | 25 : largest_changestamp_(0), |
| 26 default_max_results_(0), |
25 resource_id_count_(0), | 27 resource_id_count_(0), |
26 resource_list_load_count_(0), | 28 resource_list_load_count_(0), |
27 account_metadata_load_count_(0), | 29 account_metadata_load_count_(0), |
28 offline_(false) { | 30 offline_(false) { |
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
30 } | 32 } |
31 | 33 |
32 FakeDriveService::~FakeDriveService() { | 34 FakeDriveService::~FakeDriveService() { |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
34 } | 36 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 if (offline_) { | 149 if (offline_) { |
148 scoped_ptr<ResourceList> null; | 150 scoped_ptr<ResourceList> null; |
149 MessageLoop::current()->PostTask( | 151 MessageLoop::current()->PostTask( |
150 FROM_HERE, | 152 FROM_HERE, |
151 base::Bind(callback, | 153 base::Bind(callback, |
152 GDATA_NO_CONNECTION, | 154 GDATA_NO_CONNECTION, |
153 base::Passed(&null))); | 155 base::Passed(&null))); |
154 return; | 156 return; |
155 } | 157 } |
156 | 158 |
| 159 // "start-offset" is a parameter only used in the FakeDriveService to |
| 160 // implement pagenation. |
| 161 int start_offset = 0; |
| 162 int max_results = default_max_results_; |
| 163 std::vector<std::pair<std::string, std::string> > parameters; |
| 164 if (base::SplitStringIntoKeyValuePairs( |
| 165 feed_url.query(), '=', '&', ¶meters)) { |
| 166 for (size_t i = 0; i < parameters.size(); ++i) { |
| 167 if (parameters[i].first == "start-offset") |
| 168 base::StringToInt(parameters[i].second, &start_offset); |
| 169 if (parameters[i].first == "max-results") |
| 170 base::StringToInt(parameters[i].second, &max_results); |
| 171 } |
| 172 } |
| 173 |
157 scoped_ptr<ResourceList> resource_list = | 174 scoped_ptr<ResourceList> resource_list = |
158 ResourceList::CreateFrom(*resource_list_value_); | 175 ResourceList::CreateFrom(*resource_list_value_); |
159 | 176 |
160 // Filter out entries per parameters like |directory_resource_id| and | 177 // Filter out entries per parameters like |directory_resource_id| and |
161 // |search_query|. | 178 // |search_query|. |
162 ScopedVector<ResourceEntry>* entries = resource_list->mutable_entries(); | 179 ScopedVector<ResourceEntry>* entries = resource_list->mutable_entries(); |
| 180 |
| 181 int num_entries_matched = 0; |
163 for (size_t i = 0; i < entries->size();) { | 182 for (size_t i = 0; i < entries->size();) { |
164 ResourceEntry* entry = (*entries)[i]; | 183 ResourceEntry* entry = (*entries)[i]; |
165 bool should_exclude = false; | 184 bool should_exclude = false; |
166 | 185 |
167 // If |directory_resource_id| is set, exclude the entry if it's not in | 186 // If |directory_resource_id| is set, exclude the entry if it's not in |
168 // the target directory. | 187 // the target directory. |
169 if (!directory_resource_id.empty()) { | 188 if (!directory_resource_id.empty()) { |
170 // Get the parent resource ID of the entry. If the parent link does | 189 // Get the parent resource ID of the entry. If the parent link does |
171 // not exist, the entry must be in the root directory. | 190 // not exist, the entry must be in the root directory. |
172 std::string parent_resource_id = "folder:root"; | 191 std::string parent_resource_id = "folder:root"; |
173 const google_apis::Link* parent_link = | 192 const google_apis::Link* parent_link = |
174 entry->GetLinkByType(Link::LINK_PARENT); | 193 entry->GetLinkByType(Link::LINK_PARENT); |
175 if (parent_link) { | 194 if (parent_link) { |
176 parent_resource_id = | 195 parent_resource_id = |
177 net::UnescapeURLComponent(parent_link->href().ExtractFileName(), | 196 net::UnescapeURLComponent(parent_link->href().ExtractFileName(), |
178 net::UnescapeRule::URL_SPECIAL_CHARS); | 197 net::UnescapeRule::URL_SPECIAL_CHARS); |
179 } | 198 } |
180 if (directory_resource_id != parent_resource_id) | 199 if (directory_resource_id != parent_resource_id) |
181 should_exclude = true; | 200 should_exclude = true; |
182 } | 201 } |
183 | 202 |
184 // If |search_query| is set, exclude the entry if it does not contain the | 203 // If |search_query| is set, exclude the entry if it does not contain the |
185 // search query in the title. | 204 // search query in the title. |
186 if (!search_query.empty()) { | 205 if (!search_query.empty()) { |
187 if (UTF16ToUTF8(entry->title()).find(search_query) == std::string::npos) | 206 if (UTF16ToUTF8(entry->title()).find(search_query) == std::string::npos) |
188 should_exclude = true; | 207 should_exclude = true; |
189 } | 208 } |
190 | 209 |
191 // If |start_changestamp| is non-zero, exclude the entry if the | 210 // If |start_changestamp| is set, exclude the entry if the |
192 // changestamp is older than |largest_changestamp|. | 211 // changestamp is older than |largest_changestamp|. |
193 // See https://developers.google.com/google-apps/documents-list/ | 212 // See https://developers.google.com/google-apps/documents-list/ |
194 // #retrieving_all_changes_since_a_given_changestamp | 213 // #retrieving_all_changes_since_a_given_changestamp |
195 if (start_changestamp > 0) { | 214 if (start_changestamp > 0 && entry->changestamp() < start_changestamp) |
196 if (entry->changestamp() < start_changestamp) { | 215 should_exclude = true; |
197 should_exclude = true; | 216 |
198 } | 217 // The entry matched the criteria for inclusion. |
199 } | 218 if (!should_exclude) |
| 219 ++num_entries_matched; |
| 220 |
| 221 // If |start_offset| is set, exclude the entry if the entry is before the |
| 222 // start index. <= instead of < as |num_entries_matched| was |
| 223 // already incremented. |
| 224 if (start_offset > 0 && num_entries_matched <= start_offset) |
| 225 should_exclude = true; |
200 | 226 |
201 if (should_exclude) | 227 if (should_exclude) |
202 entries->erase(entries->begin() + i); | 228 entries->erase(entries->begin() + i); |
203 else | 229 else |
204 ++i; | 230 ++i; |
205 } | 231 } |
206 | 232 |
| 233 // If |max_results| is set, trim the entries if the number exceeded the max |
| 234 // results. |
| 235 if (max_results > 0 && entries->size() > static_cast<size_t>(max_results)) { |
| 236 entries->erase(entries->begin() + max_results, entries->end()); |
| 237 // Adds the next URL. |
| 238 const GURL next_url( |
| 239 base::StringPrintf( |
| 240 "http://localhost/?start-offset=%d&max-results=%d", |
| 241 start_offset + max_results, |
| 242 max_results)); |
| 243 Link* link = new Link; |
| 244 link->set_type(Link::LINK_NEXT); |
| 245 link->set_href(next_url); |
| 246 resource_list->mutable_links()->push_back(link); |
| 247 } |
| 248 |
207 ++resource_list_load_count_; | 249 ++resource_list_load_count_; |
208 MessageLoop::current()->PostTask( | 250 MessageLoop::current()->PostTask( |
209 FROM_HERE, | 251 FROM_HERE, |
210 base::Bind(callback, | 252 base::Bind(callback, |
211 HTTP_SUCCESS, | 253 HTTP_SUCCESS, |
212 base::Passed(&resource_list))); | 254 base::Passed(&resource_list))); |
213 } | 255 } |
214 | 256 |
215 void FakeDriveService::GetResourceEntry( | 257 void FakeDriveService::GetResourceEntry( |
216 const std::string& resource_id, | 258 const std::string& resource_id, |
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 return base::StringPrintf("resource_id_%d", resource_id_count_); | 839 return base::StringPrintf("resource_id_%d", resource_id_count_); |
798 } | 840 } |
799 | 841 |
800 void FakeDriveService::AddNewChangestamp(base::DictionaryValue* entry) { | 842 void FakeDriveService::AddNewChangestamp(base::DictionaryValue* entry) { |
801 ++largest_changestamp_; | 843 ++largest_changestamp_; |
802 entry->SetString("docs$changestamp.value", | 844 entry->SetString("docs$changestamp.value", |
803 base::Int64ToString(largest_changestamp_)); | 845 base::Int64ToString(largest_changestamp_)); |
804 } | 846 } |
805 | 847 |
806 } // namespace google_apis | 848 } // namespace google_apis |
OLD | NEW |