Chromium Code Reviews| 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/gdata_wapi_parser.h" | 14 #include "chrome/browser/google_apis/gdata_wapi_parser.h" |
| 14 #include "chrome/browser/google_apis/test_util.h" | 15 #include "chrome/browser/google_apis/test_util.h" |
| 15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 16 #include "net/base/escape.h" | 17 #include "net/base/escape.h" |
| 17 | 18 |
| 18 using content::BrowserThread; | 19 using content::BrowserThread; |
| 19 | 20 |
| 20 namespace google_apis { | 21 namespace google_apis { |
| 21 | 22 |
| 22 FakeDriveService::FakeDriveService() | 23 FakeDriveService::FakeDriveService() |
| 23 : largest_changestamp_(0), | 24 : largest_changestamp_(0), |
| 25 default_max_results_(0), | |
| 24 resource_id_count_(0), | 26 resource_id_count_(0), |
| 25 resource_list_load_count_(0), | 27 resource_list_load_count_(0), |
| 26 account_metadata_load_count_(0), | 28 account_metadata_load_count_(0), |
| 27 offline_(false) { | 29 offline_(false) { |
| 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 29 } | 31 } |
| 30 | 32 |
| 31 FakeDriveService::~FakeDriveService() { | 33 FakeDriveService::~FakeDriveService() { |
| 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 33 } | 35 } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 if (offline_) { | 143 if (offline_) { |
| 142 scoped_ptr<ResourceList> null; | 144 scoped_ptr<ResourceList> null; |
| 143 MessageLoop::current()->PostTask( | 145 MessageLoop::current()->PostTask( |
| 144 FROM_HERE, | 146 FROM_HERE, |
| 145 base::Bind(callback, | 147 base::Bind(callback, |
| 146 GDATA_NO_CONNECTION, | 148 GDATA_NO_CONNECTION, |
| 147 base::Passed(&null))); | 149 base::Passed(&null))); |
| 148 return; | 150 return; |
| 149 } | 151 } |
| 150 | 152 |
| 153 int start_index = 0; | |
|
kinaba
2013/01/19 03:28:18
The usage of "start-index" url parameter looks dif
satorux1
2013/01/22 17:44:09
That's a great catch! I wasn't aware of this incom
| |
| 154 int max_results = default_max_results_; | |
| 155 std::vector<std::pair<std::string, std::string> > parameters; | |
| 156 if (base::SplitStringIntoKeyValuePairs( | |
| 157 feed_url.query(), '=', '&', ¶meters)) { | |
| 158 for (size_t i = 0; i < parameters.size(); ++i) { | |
| 159 if (parameters[i].first == "start-index") | |
| 160 base::StringToInt(parameters[i].second, &start_index); | |
| 161 if (parameters[i].first == "max-results") | |
| 162 base::StringToInt(parameters[i].second, &max_results); | |
| 163 } | |
| 164 } | |
| 165 | |
| 151 scoped_ptr<ResourceList> resource_list = | 166 scoped_ptr<ResourceList> resource_list = |
| 152 ResourceList::CreateFrom(*resource_list_value_); | 167 ResourceList::CreateFrom(*resource_list_value_); |
| 153 | 168 |
| 154 // Filter out entries per parameters like |directory_resource_id| and | 169 // Filter out entries per parameters like |directory_resource_id| and |
| 155 // |search_query|. | 170 // |search_query|. |
| 156 ScopedVector<ResourceEntry>* entries = resource_list->mutable_entries(); | 171 ScopedVector<ResourceEntry>* entries = resource_list->mutable_entries(); |
| 172 | |
| 173 int num_entries_matched = 0; | |
| 157 for (size_t i = 0; i < entries->size();) { | 174 for (size_t i = 0; i < entries->size();) { |
| 158 ResourceEntry* entry = (*entries)[i]; | 175 ResourceEntry* entry = (*entries)[i]; |
| 159 bool should_exclude = false; | 176 bool should_exclude = false; |
| 160 | 177 |
| 161 // If |directory_resource_id| is set, exclude the entry if it's not in | 178 // If |directory_resource_id| is set, exclude the entry if it's not in |
| 162 // the target directory. | 179 // the target directory. |
| 163 if (!directory_resource_id.empty()) { | 180 if (!directory_resource_id.empty()) { |
| 164 // Get the parent resource ID of the entry. If the parent link does | 181 // Get the parent resource ID of the entry. If the parent link does |
| 165 // not exist, the entry must be in the root directory. | 182 // not exist, the entry must be in the root directory. |
| 166 std::string parent_resource_id = "folder:root"; | 183 std::string parent_resource_id = "folder:root"; |
| 167 const google_apis::Link* parent_link = | 184 const google_apis::Link* parent_link = |
| 168 entry->GetLinkByType(Link::LINK_PARENT); | 185 entry->GetLinkByType(Link::LINK_PARENT); |
| 169 if (parent_link) { | 186 if (parent_link) { |
| 170 parent_resource_id = | 187 parent_resource_id = |
| 171 net::UnescapeURLComponent(parent_link->href().ExtractFileName(), | 188 net::UnescapeURLComponent(parent_link->href().ExtractFileName(), |
| 172 net::UnescapeRule::URL_SPECIAL_CHARS); | 189 net::UnescapeRule::URL_SPECIAL_CHARS); |
| 173 } | 190 } |
| 174 if (directory_resource_id != parent_resource_id) | 191 if (directory_resource_id != parent_resource_id) |
| 175 should_exclude = true; | 192 should_exclude = true; |
| 176 } | 193 } |
| 177 | 194 |
| 178 // If |search_query| is set, exclude the entry if it does not contain the | 195 // If |search_query| is set, exclude the entry if it does not contain the |
| 179 // search query in the title. | 196 // search query in the title. |
| 180 if (!search_query.empty()) { | 197 if (!search_query.empty()) { |
| 181 if (UTF16ToUTF8(entry->title()).find(search_query) == std::string::npos) | 198 if (UTF16ToUTF8(entry->title()).find(search_query) == std::string::npos) |
| 182 should_exclude = true; | 199 should_exclude = true; |
| 183 } | 200 } |
| 184 | 201 |
| 185 // If |start_changestamp| is non-zero, exclude the entry if the | 202 // If |start_changestamp| is set, exclude the entry if the |
| 186 // changestamp is older than |largest_changestamp|. | 203 // changestamp is older than |largest_changestamp|. |
| 187 // See https://developers.google.com/google-apps/documents-list/ | 204 // See https://developers.google.com/google-apps/documents-list/ |
| 188 // #retrieving_all_changes_since_a_given_changestamp | 205 // #retrieving_all_changes_since_a_given_changestamp |
| 189 if (start_changestamp > 0) { | 206 if (start_changestamp > 0 && entry->changestamp() < start_changestamp) |
| 190 if (entry->changestamp() < start_changestamp) { | 207 should_exclude = true; |
| 191 should_exclude = true; | 208 |
| 192 } | 209 // The entry matched the criteria for inclusion. |
| 193 } | 210 if (!should_exclude) |
| 211 ++num_entries_matched; | |
| 212 | |
| 213 // If |start_index| is set, exclude the entry if the entry is before the | |
| 214 // start index. <= instead of < as |num_entries_matched| was | |
| 215 // already incremented. | |
| 216 if (start_index > 0 && num_entries_matched <= start_index) | |
| 217 should_exclude = true; | |
| 194 | 218 |
| 195 if (should_exclude) | 219 if (should_exclude) |
| 196 entries->erase(entries->begin() + i); | 220 entries->erase(entries->begin() + i); |
| 197 else | 221 else |
| 198 ++i; | 222 ++i; |
| 199 } | 223 } |
| 200 | 224 |
| 225 // If |max_results| is set, trim the entries if the number exceeded the max | |
| 226 // results. | |
| 227 if (max_results > 0 && entries->size() > static_cast<size_t>(max_results)) { | |
| 228 entries->erase(entries->begin() + max_results, entries->end()); | |
| 229 // Adds the next URL. | |
| 230 const GURL next_url( | |
| 231 base::StringPrintf( | |
| 232 "http://localhost/?start-index=%d&max-results=%d", | |
| 233 start_index + max_results, | |
| 234 max_results)); | |
| 235 Link* link = new Link; | |
| 236 link->set_type(Link::LINK_NEXT); | |
| 237 link->set_href(next_url); | |
| 238 resource_list->mutable_links()->push_back(link); | |
| 239 } | |
| 240 | |
| 201 ++resource_list_load_count_; | 241 ++resource_list_load_count_; |
| 202 MessageLoop::current()->PostTask( | 242 MessageLoop::current()->PostTask( |
| 203 FROM_HERE, | 243 FROM_HERE, |
| 204 base::Bind(callback, | 244 base::Bind(callback, |
| 205 HTTP_SUCCESS, | 245 HTTP_SUCCESS, |
| 206 base::Passed(&resource_list))); | 246 base::Passed(&resource_list))); |
| 207 } | 247 } |
| 208 | 248 |
| 209 void FakeDriveService::GetResourceEntry( | 249 void FakeDriveService::GetResourceEntry( |
| 210 const std::string& resource_id, | 250 const std::string& resource_id, |
| (...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 790 return base::StringPrintf("resource_id_%d", resource_id_count_); | 830 return base::StringPrintf("resource_id_%d", resource_id_count_); |
| 791 } | 831 } |
| 792 | 832 |
| 793 void FakeDriveService::AddNewChangestamp(base::DictionaryValue* entry) { | 833 void FakeDriveService::AddNewChangestamp(base::DictionaryValue* entry) { |
| 794 ++largest_changestamp_; | 834 ++largest_changestamp_; |
| 795 entry->SetString("docs$changestamp.value", | 835 entry->SetString("docs$changestamp.value", |
| 796 base::Int64ToString(largest_changestamp_)); | 836 base::Int64ToString(largest_changestamp_)); |
| 797 } | 837 } |
| 798 | 838 |
| 799 } // namespace google_apis | 839 } // namespace google_apis |
| OLD | NEW |