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[] = | |
52 "/feeds/default/private/changes"; | |
53 | |
54 } // namespace | |
55 | |
56 namespace gdata_wapi_url_util { | |
57 | |
58 const char kBaseUrlForProduction[] = "https://docs.google.com/"; | |
59 const char kBaseUrlForTesting[] = "http://127.0.0.1/"; | |
60 | |
61 GURL AddStandardUrlParams(const GURL& url) { | |
62 GURL result = | |
63 chrome_common_net::AppendOrReplaceQueryParameter(url, "v", "3"); | |
64 result = | |
65 chrome_common_net::AppendOrReplaceQueryParameter(result, "alt", "json"); | |
66 return result; | |
67 } | |
68 | |
69 GURL AddMetadataUrlParams(const GURL& url) { | |
70 GURL result = AddStandardUrlParams(url); | |
71 result = chrome_common_net::AppendOrReplaceQueryParameter( | |
72 result, "include-installed-apps", "true"); | |
73 return result; | |
74 } | |
75 | |
76 GURL AddFeedUrlParams(const GURL& url, | |
77 int num_items_to_fetch, | |
78 int changestamp, | |
79 const std::string& search_string) { | |
80 GURL result = AddStandardUrlParams(url); | |
81 result = chrome_common_net::AppendOrReplaceQueryParameter( | |
82 result, | |
83 "showfolders", | |
84 "true"); | |
85 result = chrome_common_net::AppendOrReplaceQueryParameter( | |
86 result, | |
87 "max-results", | |
88 base::StringPrintf("%d", num_items_to_fetch)); | |
89 result = chrome_common_net::AppendOrReplaceQueryParameter( | |
90 result, "include-installed-apps", "true"); | |
91 | |
92 if (changestamp) { | |
93 result = chrome_common_net::AppendQueryParameter( | |
94 result, | |
95 "start-index", | |
96 base::StringPrintf("%d", changestamp)); | |
97 } | |
98 | |
99 if (!search_string.empty()) { | |
100 result = chrome_common_net::AppendOrReplaceQueryParameter( | |
101 result, "q", search_string); | |
102 } | |
103 return result; | |
104 } | |
105 | |
106 } // namespace gdata_wapi_url_util | |
107 | |
108 | |
109 // TODO(satorux): Move the following code to a separate file | |
110 // gdata_wapi_url_generator.cc | |
111 | |
112 GDataWapiUrlGenerator::GDataWapiUrlGenerator(const GURL& base_url) | |
113 : base_url_(GURL(base_url)) { | |
114 } | |
115 | |
116 GDataWapiUrlGenerator::~GDataWapiUrlGenerator() { | |
117 } | |
118 | |
119 GURL GDataWapiUrlGenerator::GenerateDocumentListUrl( | |
120 const GURL& override_url, | |
121 int start_changestamp, | |
122 const std::string& search_string, | |
123 bool shared_with_me, | |
124 const std::string& directory_resource_id) const { | |
125 DCHECK_LE(0, start_changestamp); | |
126 | |
127 int max_docs = search_string.empty() ? kMaxDocumentsPerFeed : | |
128 kMaxDocumentsPerSearchFeed; | |
129 GURL url; | |
130 if (!override_url.is_empty()) { | |
131 url = override_url; | |
132 } else if (shared_with_me) { | |
133 url = base_url_.Resolve(kGetDocumentListURLForSharedWithMe); | |
134 } else if (start_changestamp > 0) { | |
135 // The start changestamp shouldn't be used for a search. | |
136 DCHECK(search_string.empty()); | |
137 url = base_url_.Resolve(kGetChangesListURL); | |
138 } else if (!directory_resource_id.empty()) { | |
139 url = base_url_.Resolve( | |
140 base::StringPrintf(kGetDocumentListURLForDirectoryFormat, | |
141 net::EscapePath( | |
142 directory_resource_id).c_str())); | |
143 } else { | |
144 url = base_url_.Resolve(kGetDocumentListURLForAllDocuments); | |
145 } | |
146 return gdata_wapi_url_util::AddFeedUrlParams( | |
147 url, max_docs, start_changestamp, search_string); | |
148 } | |
149 | |
150 GURL GDataWapiUrlGenerator::GenerateDocumentEntryUrl( | |
151 const std::string& resource_id) const { | |
152 GURL result = base_url_.Resolve( | |
153 base::StringPrintf(kGetDocumentEntryURLFormat, | |
154 net::EscapePath(resource_id).c_str())); | |
155 return gdata_wapi_url_util::AddStandardUrlParams(result); | |
156 } | |
157 | |
158 GURL GDataWapiUrlGenerator::GenerateDocumentListRootUrl() const { | |
159 return gdata_wapi_url_util::AddStandardUrlParams( | |
160 base_url_.Resolve(kDocumentListRootURL)); | |
161 } | |
162 | |
163 GURL GDataWapiUrlGenerator::GenerateAccountMetadataUrl() const { | |
164 return gdata_wapi_url_util::AddMetadataUrlParams( | |
165 base_url_.Resolve(kAccountMetadataURL)); | |
166 } | |
167 | |
168 } // namespace google_apis | |
OLD | NEW |