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/chromeos/gdata/drive_webapps_registry.h" | 5 #include "chrome/browser/chromeos/gdata/drive_webapps_registry.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/chromeos/gdata/drive_api_parser.h" |
9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
10 | 11 |
11 using content::BrowserThread; | 12 using content::BrowserThread; |
12 | 13 |
13 namespace gdata { | 14 namespace gdata { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // WebApp store URL prefix. | 18 // WebApp store URL prefix. |
18 const char kStoreProductUrl[] = "https://chrome.google.com/webstore/"; | 19 const char kStoreProductUrl[] = "https://chrome.google.com/webstore/"; |
19 | 20 |
20 // Extracts Web store id from its web store URL. | 21 // Extracts Web store id from its web store URL. |
21 std::string GetWebStoreIdFromUrl(const GURL& url) { | 22 std::string GetWebStoreIdFromUrl(const GURL& url) { |
22 if (!StartsWithASCII(url.spec(), kStoreProductUrl, false)) { | 23 if (!StartsWithASCII(url.spec(), kStoreProductUrl, false)) { |
23 LOG(WARNING) << "Unrecognized product URL " << url.spec(); | 24 LOG(WARNING) << "Unrecognized product URL " << url.spec(); |
24 return std::string(); | 25 return std::string(); |
25 } | 26 } |
26 | 27 |
27 FilePath path(url.path()); | 28 FilePath path(url.path()); |
28 std::vector<FilePath::StringType> components; | 29 std::vector<FilePath::StringType> components; |
29 path.GetComponents(&components); | 30 path.GetComponents(&components); |
30 DCHECK_LE(2U, components.size()); // Coming from kStoreProductUrl | 31 DCHECK_LE(2U, components.size()); // Coming from kStoreProductUrl |
31 | 32 |
32 // Return the last part of the path | 33 // Return the last part of the path |
33 return components[components.size() - 1]; | 34 return components[components.size() - 1]; |
34 } | 35 } |
35 | 36 |
| 37 // TODO(kochi): This is duplicate from gdata_wapi_parser.cc. |
| 38 bool SortBySize(const InstalledApp::IconList::value_type& a, |
| 39 const InstalledApp::IconList::value_type& b) { |
| 40 return a.first < b.first; |
| 41 } |
| 42 |
36 } // namespace | 43 } // namespace |
37 | 44 |
38 // DriveWebAppInfo struct implementation. | 45 // DriveWebAppInfo struct implementation. |
39 | 46 |
40 DriveWebAppInfo::DriveWebAppInfo(const std::string& app_id, | 47 DriveWebAppInfo::DriveWebAppInfo(const std::string& app_id, |
41 const InstalledApp::IconList& app_icons, | 48 const InstalledApp::IconList& app_icons, |
42 const InstalledApp::IconList& document_icons, | 49 const InstalledApp::IconList& document_icons, |
43 const std::string& web_store_id, | 50 const std::string& web_store_id, |
44 const string16& app_name, | 51 const string16& app_name, |
45 const string16& object_type, | 52 const string16& object_type, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 // web_store_id. | 126 // web_store_id. |
120 for (WebAppFileSelectorMap::iterator iter = webapp_extension_map_.begin(); | 127 for (WebAppFileSelectorMap::iterator iter = webapp_extension_map_.begin(); |
121 iter != webapp_extension_map_.end(); ++iter) { | 128 iter != webapp_extension_map_.end(); ++iter) { |
122 std::string id = GetWebStoreIdFromUrl(iter->second->product_link); | 129 std::string id = GetWebStoreIdFromUrl(iter->second->product_link); |
123 if (id == web_store_id) | 130 if (id == web_store_id) |
124 extensions.insert(iter->first); | 131 extensions.insert(iter->first); |
125 } | 132 } |
126 return extensions; | 133 return extensions; |
127 } | 134 } |
128 | 135 |
129 void DriveWebAppsRegistry::UpdateFromFeed(AccountMetadataFeed* metadata) { | 136 void DriveWebAppsRegistry::UpdateFromFeed( |
| 137 const AccountMetadataFeed& metadata) { |
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
131 | 139 |
132 url_to_name_map_.clear(); | 140 url_to_name_map_.clear(); |
133 STLDeleteValues(&webapp_extension_map_); | 141 STLDeleteValues(&webapp_extension_map_); |
134 STLDeleteValues(&webapp_mimetypes_map_); | 142 STLDeleteValues(&webapp_mimetypes_map_); |
135 for (ScopedVector<InstalledApp>::const_iterator it = | 143 for (ScopedVector<InstalledApp>::const_iterator it = |
136 metadata->installed_apps().begin(); | 144 metadata.installed_apps().begin(); |
137 it != metadata->installed_apps().end(); ++it) { | 145 it != metadata.installed_apps().end(); ++it) { |
138 const InstalledApp* app = *it; | 146 const InstalledApp& app = **it; |
139 GURL product_url = app->GetProductUrl(); | 147 GURL product_url = app.GetProductUrl(); |
140 if (product_url.is_empty()) | 148 if (product_url.is_empty()) |
141 continue; | 149 continue; |
142 | 150 |
143 InstalledApp::IconList app_icons = | 151 InstalledApp::IconList app_icons = |
144 app->GetIconsForCategory(AppIcon::APPLICATION); | 152 app.GetIconsForCategory(AppIcon::APPLICATION); |
145 InstalledApp::IconList document_icons = | 153 InstalledApp::IconList document_icons = |
146 app->GetIconsForCategory(AppIcon::DOCUMENT); | 154 app.GetIconsForCategory(AppIcon::DOCUMENT); |
147 | 155 |
148 url_to_name_map_.insert( | 156 url_to_name_map_.insert( |
149 std::make_pair(product_url, app->app_name())); | 157 std::make_pair(product_url, app.app_name())); |
150 AddAppSelectorList(product_url, | 158 AddAppSelectorList(product_url, |
151 app_icons, | 159 app_icons, |
152 document_icons, | 160 document_icons, |
153 app->object_type(), | 161 app.object_type(), |
154 app->app_id(), | 162 app.app_id(), |
155 true, // primary | 163 true, // primary |
156 app->primary_mimetypes(), | 164 app.primary_mimetypes(), |
157 &webapp_mimetypes_map_); | 165 &webapp_mimetypes_map_); |
158 AddAppSelectorList(product_url, | 166 AddAppSelectorList(product_url, |
159 app_icons, | 167 app_icons, |
160 document_icons, | 168 document_icons, |
161 app->object_type(), | 169 app.object_type(), |
162 app->app_id(), | 170 app.app_id(), |
163 false, // primary | 171 false, // primary |
164 app->secondary_mimetypes(), | 172 app.secondary_mimetypes(), |
165 &webapp_mimetypes_map_); | 173 &webapp_mimetypes_map_); |
166 AddAppSelectorList(product_url, | 174 AddAppSelectorList(product_url, |
167 app_icons, | 175 app_icons, |
168 document_icons, | 176 document_icons, |
169 app->object_type(), | 177 app.object_type(), |
170 app->app_id(), | 178 app.app_id(), |
171 true, // primary | 179 true, // primary |
172 app->primary_extensions(), | 180 app.primary_extensions(), |
173 &webapp_extension_map_); | 181 &webapp_extension_map_); |
174 AddAppSelectorList(product_url, | 182 AddAppSelectorList(product_url, |
175 app_icons, | 183 app_icons, |
176 document_icons, | 184 document_icons, |
177 app->object_type(), | 185 app.object_type(), |
178 app->app_id(), | 186 app.app_id(), |
179 false, // primary | 187 false, // primary |
180 app->secondary_extensions(), | 188 app.secondary_extensions(), |
181 &webapp_extension_map_); | 189 &webapp_extension_map_); |
182 } | 190 } |
183 } | 191 } |
| 192 |
| 193 void DriveWebAppsRegistry::UpdateFromApplicationList(const AppList& applist) { |
| 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 195 |
| 196 url_to_name_map_.clear(); |
| 197 STLDeleteValues(&webapp_extension_map_); |
| 198 STLDeleteValues(&webapp_mimetypes_map_); |
| 199 for (size_t i = 0; i < applist.items().size(); ++i) { |
| 200 const AppResource& app = *applist.items()[i]; |
| 201 if (app.product_url().is_empty()) |
| 202 continue; |
| 203 |
| 204 InstalledApp::IconList app_icons; |
| 205 InstalledApp::IconList document_icons; |
| 206 for (size_t j = 0; j < app.icons().size(); ++j) { |
| 207 const DriveAppIcon& icon = *app.icons()[j]; |
| 208 if (icon.icon_url().is_empty()) |
| 209 continue; |
| 210 if (icon.category() == DriveAppIcon::APPLICATION) |
| 211 app_icons.push_back(std::make_pair(icon.icon_side_length(), |
| 212 icon.icon_url())); |
| 213 if (icon.category() == DriveAppIcon::DOCUMENT) |
| 214 document_icons.push_back(std::make_pair(icon.icon_side_length(), |
| 215 icon.icon_url())); |
| 216 } |
| 217 std::sort(app_icons.begin(), app_icons.end(), SortBySize); |
| 218 std::sort(document_icons.begin(), document_icons.end(), SortBySize); |
| 219 |
| 220 url_to_name_map_.insert( |
| 221 std::make_pair(app.product_url(), UTF8ToUTF16(app.name()))); |
| 222 AddAppSelectorList(app.product_url(), |
| 223 app_icons, |
| 224 document_icons, |
| 225 UTF8ToUTF16(app.object_type()), |
| 226 app.application_id(), |
| 227 true, // primary |
| 228 app.primary_mimetypes(), |
| 229 &webapp_mimetypes_map_); |
| 230 AddAppSelectorList(app.product_url(), |
| 231 app_icons, |
| 232 document_icons, |
| 233 UTF8ToUTF16(app.object_type()), |
| 234 app.application_id(), |
| 235 false, // primary |
| 236 app.secondary_mimetypes(), |
| 237 &webapp_mimetypes_map_); |
| 238 AddAppSelectorList(app.product_url(), |
| 239 app_icons, |
| 240 document_icons, |
| 241 UTF8ToUTF16(app.object_type()), |
| 242 app.application_id(), |
| 243 true, // primary |
| 244 app.primary_file_extensions(), |
| 245 &webapp_extension_map_); |
| 246 AddAppSelectorList(app.product_url(), |
| 247 app_icons, |
| 248 document_icons, |
| 249 UTF8ToUTF16(app.object_type()), |
| 250 app.application_id(), |
| 251 false, // primary |
| 252 app.secondary_file_extensions(), |
| 253 &webapp_extension_map_); |
| 254 } |
| 255 } |
184 | 256 |
185 // static. | 257 // static. |
186 void DriveWebAppsRegistry::AddAppSelectorList( | 258 void DriveWebAppsRegistry::AddAppSelectorList( |
187 const GURL& product_link, | 259 const GURL& product_link, |
188 const InstalledApp::IconList& app_icons, | 260 const InstalledApp::IconList& app_icons, |
189 const InstalledApp::IconList& document_icons, | 261 const InstalledApp::IconList& document_icons, |
190 const string16& object_type, | 262 const string16& object_type, |
191 const std::string& app_id, | 263 const std::string& app_id, |
192 bool is_primary_selector, | 264 bool is_primary_selector, |
193 const ScopedVector<std::string>& selectors, | 265 const ScopedVector<std::string>& selectors, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 web_app->app_icons, | 304 web_app->app_icons, |
233 web_app->document_icons, | 305 web_app->document_icons, |
234 web_store_id, | 306 web_store_id, |
235 product_iter->second, // app name. | 307 product_iter->second, // app name. |
236 web_app->object_type, | 308 web_app->object_type, |
237 web_app->is_primary_selector))); | 309 web_app->is_primary_selector))); |
238 } | 310 } |
239 } | 311 } |
240 | 312 |
241 } // namespace gdata | 313 } // namespace gdata |
OLD | NEW |