OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/google_apis/gdata_wapi_url_util.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/stringprintf.h" | |
9 #include "chrome/common/net/url_util.h" | |
10 #include "googleurl/src/gurl.h" | |
11 #include "net/base/escape.h" | |
12 | |
13 namespace google_apis { | |
14 namespace { | |
15 | |
16 // URL requesting documents list that belong to the authenticated user only | |
17 // (handled with '/-/mine' part). | |
18 const char kGetDocumentListURLForAllDocuments[] = | |
19 "/feeds/default/private/full/-/mine"; | |
20 | |
21 // URL requesting documents list in a particular directory specified by "%s" | |
22 // that belong to the authenticated user only (handled with '/-/mine' part). | |
23 const char kGetDocumentListURLForDirectoryFormat[] = | |
24 "/feeds/default/private/full/%s/contents/-/mine"; | |
25 | |
26 // URL requesting single document entry whose resource id is specified by "%s". | |
27 const char kGetDocumentEntryURLFormat[] = "/feeds/default/private/full/%s"; | |
28 | |
29 // Root document list url. | |
30 const char kDocumentListRootURL[] = "/feeds/default/private/full"; | |
31 | |
32 // Metadata feed with things like user quota. | |
33 const char kAccountMetadataURL[] = "/feeds/metadata/default"; | |
34 | |
35 #ifndef NDEBUG | |
36 // Use smaller 'page' size while debugging to ensure we hit feed reload | |
37 // almost always. Be careful not to use something too small on account that | |
38 // have many items because server side 503 error might kick in. | |
39 const int kMaxDocumentsPerFeed = 500; | |
40 const int kMaxDocumentsPerSearchFeed = 50; | |
41 #else | |
42 const int kMaxDocumentsPerFeed = 500; | |
43 const int kMaxDocumentsPerSearchFeed = 50; | |
44 #endif | |
45 | |
46 // URL requesting documents list that shared to the authenticated user only | |
47 const char kGetDocumentListURLForSharedWithMe[] = | |
48 "/feeds/default/private/full/-/shared-with-me"; | |
49 | |
50 // URL requesting documents list of changes to documents collections. | |
51 const char kGetChangesListURL[] = "/feeds/default/private/changes"; | |
52 | |
53 } // namespace | |
54 | |
55 namespace gdata_wapi_url_util { | |
56 | |
57 const char kBaseUrlForProduction[] = "https://docs.google.com/"; | |
58 const char kBaseUrlForTesting[] = "http://127.0.0.1/"; | |
59 | |
60 GURL AddStandardUrlParams(const GURL& url) { | |
61 GURL result = | |
62 chrome_common_net::AppendOrReplaceQueryParameter(url, "v", "3"); | |
63 result = | |
64 chrome_common_net::AppendOrReplaceQueryParameter(result, "alt", "json"); | |
65 return result; | |
66 } | |
67 | |
68 GURL AddMetadataUrlParams(const GURL& url) { | |
69 GURL result = AddStandardUrlParams(url); | |
70 result = chrome_common_net::AppendOrReplaceQueryParameter( | |
71 result, "include-installed-apps", "true"); | |
72 return result; | |
73 } | |
74 | |
75 GURL AddFeedUrlParams(const GURL& url, | |
76 int num_items_to_fetch, | |
77 int changestamp, | |
78 const std::string& search_string) { | |
79 GURL result = AddStandardUrlParams(url); | |
80 result = chrome_common_net::AppendOrReplaceQueryParameter( | |
81 result, | |
82 "showfolders", | |
83 "true"); | |
84 result = chrome_common_net::AppendOrReplaceQueryParameter( | |
85 result, | |
86 "max-results", | |
87 base::StringPrintf("%d", num_items_to_fetch)); | |
88 result = chrome_common_net::AppendOrReplaceQueryParameter( | |
89 result, "include-installed-apps", "true"); | |
90 | |
91 if (changestamp) { | |
92 result = chrome_common_net::AppendQueryParameter( | |
93 result, | |
94 "start-index", | |
95 base::StringPrintf("%d", changestamp)); | |
96 } | |
97 | |
98 if (!search_string.empty()) { | |
99 result = chrome_common_net::AppendOrReplaceQueryParameter( | |
100 result, "q", search_string); | |
101 } | |
102 return result; | |
103 } | |
104 | |
105 } // namespace gdata_wapi_url_util | |
106 | |
107 | |
108 // TODO(satorux): Move the following code to a separate file | |
109 // gdata_wapi_url_generator.cc | |
110 | |
111 GDataWapiUrlGenerator::GDataWapiUrlGenerator(const GURL& base_url) | |
112 : base_url_(GURL(base_url)) { | |
113 } | |
114 | |
115 GDataWapiUrlGenerator::~GDataWapiUrlGenerator() { | |
116 } | |
117 | |
118 GURL GDataWapiUrlGenerator::GenerateDocumentListUrl( | |
119 const GURL& override_url, | |
120 int start_changestamp, | |
121 const std::string& search_string, | |
122 bool shared_with_me, | |
123 const std::string& directory_resource_id) const { | |
124 DCHECK_LE(0, start_changestamp); | |
125 | |
126 int max_docs = search_string.empty() ? kMaxDocumentsPerFeed : | |
127 kMaxDocumentsPerSearchFeed; | |
128 GURL url; | |
129 if (!override_url.is_empty()) { | |
130 url = override_url; | |
131 } else if (shared_with_me) { | |
132 url = base_url_.Resolve(kGetDocumentListURLForSharedWithMe); | |
133 } else if (start_changestamp > 0) { | |
134 // The start changestamp shouldn't be used for a search. | |
135 DCHECK(search_string.empty()); | |
136 url = base_url_.Resolve(kGetChangesListURL); | |
137 } else if (!directory_resource_id.empty()) { | |
138 url = base_url_.Resolve( | |
139 base::StringPrintf(kGetDocumentListURLForDirectoryFormat, | |
140 net::EscapePath( | |
141 directory_resource_id).c_str())); | |
142 } else { | |
143 url = base_url_.Resolve(kGetDocumentListURLForAllDocuments); | |
144 } | |
145 return gdata_wapi_url_util::AddFeedUrlParams( | |
146 url, max_docs, start_changestamp, search_string); | |
147 } | |
148 | |
149 GURL GDataWapiUrlGenerator::GenerateDocumentEntryUrl( | |
150 const std::string& resource_id) const { | |
151 GURL result = base_url_.Resolve( | |
152 base::StringPrintf(kGetDocumentEntryURLFormat, | |
153 net::EscapePath(resource_id).c_str())); | |
154 return gdata_wapi_url_util::AddStandardUrlParams(result); | |
155 } | |
156 | |
157 GURL GDataWapiUrlGenerator::GenerateDocumentListRootUrl() const { | |
158 return gdata_wapi_url_util::AddStandardUrlParams( | |
159 base_url_.Resolve(kDocumentListRootURL)); | |
160 } | |
161 | |
162 GURL GDataWapiUrlGenerator::GenerateAccountMetadataUrl() const { | |
163 return gdata_wapi_url_util::AddMetadataUrlParams( | |
164 base_url_.Resolve(kAccountMetadataURL)); | |
165 } | |
166 | |
167 } // namespace google_apis | |
OLD | NEW |