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/drive/drive_app_registry.h" | 5 #include "chrome/browser/chromeos/drive/drive_app_registry.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 base::FilePath path(url.path()); | 35 base::FilePath path(url.path()); |
36 std::vector<base::FilePath::StringType> components; | 36 std::vector<base::FilePath::StringType> components; |
37 path.GetComponents(&components); | 37 path.GetComponents(&components); |
38 DCHECK_LE(2U, components.size()); // Coming from kStoreProductUrl | 38 DCHECK_LE(2U, components.size()); // Coming from kStoreProductUrl |
39 | 39 |
40 // Return the last part of the path | 40 // Return the last part of the path |
41 return components[components.size() - 1]; | 41 return components[components.size() - 1]; |
42 } | 42 } |
43 | 43 |
44 bool SortBySize(const google_apis::InstalledApp::IconList::value_type& a, | |
45 const google_apis::InstalledApp::IconList::value_type& b) { | |
46 return a.first < b.first; | |
47 } | |
48 | |
49 } // namespace | 44 } // namespace |
50 | 45 |
51 // DriveAppInfo struct implementation. | |
52 | |
53 DriveAppInfo::DriveAppInfo() { | 46 DriveAppInfo::DriveAppInfo() { |
54 } | 47 } |
55 | 48 |
56 DriveAppInfo::DriveAppInfo( | 49 DriveAppInfo::DriveAppInfo( |
57 const std::string& app_id, | 50 const std::string& app_id, |
58 const google_apis::InstalledApp::IconList& app_icons, | 51 const google_apis::InstalledApp::IconList& app_icons, |
59 const google_apis::InstalledApp::IconList& document_icons, | 52 const google_apis::InstalledApp::IconList& document_icons, |
60 const std::string& web_store_id, | 53 const std::string& web_store_id, |
61 const std::string& app_name, | 54 const std::string& app_name, |
62 const std::string& object_type, | 55 const std::string& object_type, |
63 bool is_primary_selector) | 56 bool is_primary_selector) |
64 : app_id(app_id), | 57 : app_id(app_id), |
65 app_icons(app_icons), | 58 app_icons(app_icons), |
66 document_icons(document_icons), | 59 document_icons(document_icons), |
67 web_store_id(web_store_id), | 60 web_store_id(web_store_id), |
68 app_name(app_name), | 61 app_name(app_name), |
69 object_type(object_type), | 62 object_type(object_type), |
70 is_primary_selector(is_primary_selector) { | 63 is_primary_selector(is_primary_selector) { |
71 } | 64 } |
72 | 65 |
73 DriveAppInfo::~DriveAppInfo() { | 66 DriveAppInfo::~DriveAppInfo() { |
74 } | 67 } |
75 | 68 |
76 // FileSystem::DriveAppFileSelector struct implementation. | |
77 | |
78 DriveAppRegistry::DriveAppFileSelector::DriveAppFileSelector( | |
79 const GURL& product_link, | |
80 const google_apis::InstalledApp::IconList& app_icons, | |
81 const google_apis::InstalledApp::IconList& document_icons, | |
82 const std::string& object_type, | |
83 const std::string& app_id, | |
84 bool is_primary_selector) | |
85 : product_link(product_link), | |
86 app_icons(app_icons), | |
87 document_icons(document_icons), | |
88 object_type(object_type), | |
89 app_id(app_id), | |
90 is_primary_selector(is_primary_selector) { | |
91 } | |
92 | |
93 DriveAppRegistry::DriveAppFileSelector::~DriveAppFileSelector() { | |
94 } | |
95 | |
96 // DriveAppRegistry implementation. | |
97 | |
98 DriveAppRegistry::DriveAppRegistry(JobScheduler* scheduler) | 69 DriveAppRegistry::DriveAppRegistry(JobScheduler* scheduler) |
99 : scheduler_(scheduler), | 70 : scheduler_(scheduler), |
100 is_updating_(false), | 71 is_updating_(false), |
101 weak_ptr_factory_(this) { | 72 weak_ptr_factory_(this) { |
102 } | 73 } |
103 | 74 |
104 DriveAppRegistry::~DriveAppRegistry() { | 75 DriveAppRegistry::~DriveAppRegistry() { |
105 STLDeleteValues(&app_extension_map_); | 76 STLDeleteValues(&app_extension_map_); |
106 STLDeleteValues(&app_mimetypes_map_); | 77 STLDeleteValues(&app_mimetypes_map_); |
107 } | 78 } |
108 | 79 |
109 void DriveAppRegistry::GetAppsForFile( | 80 void DriveAppRegistry::GetAppsForFile( |
110 const base::FilePath::StringType& file_extension, | 81 const base::FilePath::StringType& file_extension, |
111 const std::string& mime_type, | 82 const std::string& mime_type, |
112 ScopedVector<DriveAppInfo>* apps) const { | 83 ScopedVector<DriveAppInfo>* apps) const { |
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
114 | 85 |
115 SelectorAppList result_map; | 86 std::vector<DriveAppInfo*> matched_apps; |
116 if (!file_extension.empty()) { | 87 if (!file_extension.empty()) { |
117 const base::FilePath::StringType without_dot = file_extension.substr(1); | 88 const base::FilePath::StringType without_dot = file_extension.substr(1); |
118 FindAppsForSelector(without_dot, app_extension_map_, &result_map); | 89 FindAppsForSelector(without_dot, app_extension_map_, &matched_apps); |
119 } | 90 } |
120 | |
121 if (!mime_type.empty()) | 91 if (!mime_type.empty()) |
122 FindAppsForSelector(mime_type, app_mimetypes_map_, &result_map); | 92 FindAppsForSelector(mime_type, app_mimetypes_map_, &matched_apps); |
123 | 93 |
124 // Insert found Drive apps into |apps|, but skip duplicate results. | 94 // Insert found Drive apps into |apps|, but skip duplicate results. |
125 std::set<std::string> inserted_app_ids; | 95 std::set<std::string> inserted_app_ids; |
126 for (SelectorAppList::const_iterator it = result_map.begin(); | 96 for (size_t i = 0; i < matched_apps.size(); ++i) { |
127 it != result_map.end(); ++it) { | 97 if (inserted_app_ids.count(matched_apps[i]->app_id) == 0) { |
128 if (inserted_app_ids.find(it->second->app_id) == inserted_app_ids.end()) { | 98 inserted_app_ids.insert(matched_apps[i]->app_id); |
129 inserted_app_ids.insert(it->second->app_id); | 99 apps->push_back(new DriveAppInfo(*matched_apps[i])); |
130 apps->push_back(it->second); | |
131 } | 100 } |
132 } | 101 } |
133 } | 102 } |
134 | 103 |
135 void DriveAppRegistry::Update() { | 104 void DriveAppRegistry::Update() { |
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
137 | 106 |
138 if (is_updating_) // There is already an update in progress. | 107 if (is_updating_) // There is already an update in progress. |
139 return; | 108 return; |
140 | 109 |
(...skipping 17 matching lines...) Expand all Loading... |
158 // Failed to fetch the data from the server. We can do nothing here. | 127 // Failed to fetch the data from the server. We can do nothing here. |
159 return; | 128 return; |
160 } | 129 } |
161 | 130 |
162 DCHECK(app_list); | 131 DCHECK(app_list); |
163 UpdateFromAppList(*app_list); | 132 UpdateFromAppList(*app_list); |
164 } | 133 } |
165 | 134 |
166 void DriveAppRegistry::UpdateFromAppList( | 135 void DriveAppRegistry::UpdateFromAppList( |
167 const google_apis::AppList& app_list) { | 136 const google_apis::AppList& app_list) { |
168 url_to_name_map_.clear(); | |
169 STLDeleteValues(&app_extension_map_); | 137 STLDeleteValues(&app_extension_map_); |
170 STLDeleteValues(&app_mimetypes_map_); | 138 STLDeleteValues(&app_mimetypes_map_); |
| 139 |
171 for (size_t i = 0; i < app_list.items().size(); ++i) { | 140 for (size_t i = 0; i < app_list.items().size(); ++i) { |
172 const google_apis::AppResource& app = *app_list.items()[i]; | 141 const google_apis::AppResource& app = *app_list.items()[i]; |
| 142 |
173 if (app.product_url().is_empty()) | 143 if (app.product_url().is_empty()) |
174 continue; | 144 continue; |
| 145 std::string web_store_id = GetWebStoreIdFromUrl(app.product_url()); |
| 146 if (web_store_id.empty()) |
| 147 continue; |
175 | 148 |
176 google_apis::InstalledApp::IconList app_icons; | 149 google_apis::InstalledApp::IconList app_icons; |
177 google_apis::InstalledApp::IconList document_icons; | 150 google_apis::InstalledApp::IconList document_icons; |
178 for (size_t j = 0; j < app.icons().size(); ++j) { | 151 for (size_t j = 0; j < app.icons().size(); ++j) { |
179 const google_apis::DriveAppIcon& icon = *app.icons()[j]; | 152 const google_apis::DriveAppIcon& icon = *app.icons()[j]; |
180 if (icon.icon_url().is_empty()) | 153 if (icon.icon_url().is_empty()) |
181 continue; | 154 continue; |
182 if (icon.category() == google_apis::DriveAppIcon::APPLICATION) | 155 if (icon.category() == google_apis::DriveAppIcon::APPLICATION) |
183 app_icons.push_back(std::make_pair(icon.icon_side_length(), | 156 app_icons.push_back(std::make_pair(icon.icon_side_length(), |
184 icon.icon_url())); | 157 icon.icon_url())); |
185 if (icon.category() == google_apis::DriveAppIcon::DOCUMENT) | 158 if (icon.category() == google_apis::DriveAppIcon::DOCUMENT) |
186 document_icons.push_back(std::make_pair(icon.icon_side_length(), | 159 document_icons.push_back(std::make_pair(icon.icon_side_length(), |
187 icon.icon_url())); | 160 icon.icon_url())); |
188 } | 161 } |
189 std::sort(app_icons.begin(), app_icons.end(), SortBySize); | |
190 std::sort(document_icons.begin(), document_icons.end(), SortBySize); | |
191 | 162 |
192 url_to_name_map_.insert( | 163 AddAppSelectorList(web_store_id, |
193 std::make_pair(app.product_url(), app.name())); | 164 app.name(), |
194 AddAppSelectorList(app.product_url(), | |
195 app_icons, | 165 app_icons, |
196 document_icons, | 166 document_icons, |
197 app.object_type(), | 167 app.object_type(), |
198 app.application_id(), | 168 app.application_id(), |
199 true, // primary | 169 true, // primary |
200 app.primary_mimetypes(), | 170 app.primary_mimetypes(), |
201 &app_mimetypes_map_); | 171 &app_mimetypes_map_); |
202 AddAppSelectorList(app.product_url(), | 172 AddAppSelectorList(web_store_id, |
| 173 app.name(), |
203 app_icons, | 174 app_icons, |
204 document_icons, | 175 document_icons, |
205 app.object_type(), | 176 app.object_type(), |
206 app.application_id(), | 177 app.application_id(), |
207 false, // primary | 178 false, // primary |
208 app.secondary_mimetypes(), | 179 app.secondary_mimetypes(), |
209 &app_mimetypes_map_); | 180 &app_mimetypes_map_); |
210 AddAppSelectorList(app.product_url(), | 181 AddAppSelectorList(web_store_id, |
| 182 app.name(), |
211 app_icons, | 183 app_icons, |
212 document_icons, | 184 document_icons, |
213 app.object_type(), | 185 app.object_type(), |
214 app.application_id(), | 186 app.application_id(), |
215 true, // primary | 187 true, // primary |
216 app.primary_file_extensions(), | 188 app.primary_file_extensions(), |
217 &app_extension_map_); | 189 &app_extension_map_); |
218 AddAppSelectorList(app.product_url(), | 190 AddAppSelectorList(web_store_id, |
| 191 app.name(), |
219 app_icons, | 192 app_icons, |
220 document_icons, | 193 document_icons, |
221 app.object_type(), | 194 app.object_type(), |
222 app.application_id(), | 195 app.application_id(), |
223 false, // primary | 196 false, // primary |
224 app.secondary_file_extensions(), | 197 app.secondary_file_extensions(), |
225 &app_extension_map_); | 198 &app_extension_map_); |
226 } | 199 } |
227 } | 200 } |
228 | 201 |
229 // static. | 202 // static. |
230 void DriveAppRegistry::AddAppSelectorList( | 203 void DriveAppRegistry::AddAppSelectorList( |
231 const GURL& product_link, | 204 const std::string& web_store_id, |
| 205 const std::string& app_name, |
232 const google_apis::InstalledApp::IconList& app_icons, | 206 const google_apis::InstalledApp::IconList& app_icons, |
233 const google_apis::InstalledApp::IconList& document_icons, | 207 const google_apis::InstalledApp::IconList& document_icons, |
234 const std::string& object_type, | 208 const std::string& object_type, |
235 const std::string& app_id, | 209 const std::string& app_id, |
236 bool is_primary_selector, | 210 bool is_primary_selector, |
237 const ScopedVector<std::string>& selectors, | 211 const ScopedVector<std::string>& selectors, |
238 DriveAppFileSelectorMap* map) { | 212 DriveAppFileSelectorMap* map) { |
239 for (ScopedVector<std::string>::const_iterator it = selectors.begin(); | 213 for (ScopedVector<std::string>::const_iterator it = selectors.begin(); |
240 it != selectors.end(); ++it) { | 214 it != selectors.end(); ++it) { |
241 std::string* value = *it; | 215 std::string* value = *it; |
242 map->insert(std::make_pair( | 216 map->insert(std::make_pair( |
243 *value, new DriveAppFileSelector(product_link, | 217 *value, new DriveAppInfo(app_id, |
244 app_icons, | 218 app_icons, |
245 document_icons, | 219 document_icons, |
246 object_type, | 220 web_store_id, |
247 app_id, | 221 app_name, |
248 is_primary_selector))); | 222 object_type, |
| 223 is_primary_selector))); |
249 } | 224 } |
250 } | 225 } |
251 | 226 |
252 void DriveAppRegistry::FindAppsForSelector( | 227 void DriveAppRegistry::FindAppsForSelector( |
253 const std::string& file_selector, | 228 const std::string& file_selector, |
254 const DriveAppFileSelectorMap& map, | 229 const DriveAppFileSelectorMap& map, |
255 SelectorAppList* apps) const { | 230 std::vector<DriveAppInfo*>* matched_apps) const { |
256 for (DriveAppFileSelectorMap::const_iterator it = map.find(file_selector); | 231 for (DriveAppFileSelectorMap::const_iterator it = map.find(file_selector); |
257 it != map.end() && it->first == file_selector; ++it) { | 232 it != map.end() && it->first == file_selector; ++it) { |
258 const DriveAppFileSelector* drive_app = it->second; | 233 matched_apps->push_back(it->second); |
259 std::map<GURL, std::string>::const_iterator product_iter = | |
260 url_to_name_map_.find(drive_app->product_link); | |
261 if (product_iter == url_to_name_map_.end()) { | |
262 NOTREACHED(); | |
263 continue; | |
264 } | |
265 | |
266 std::string web_store_id = GetWebStoreIdFromUrl(drive_app->product_link); | |
267 if (web_store_id.empty()) | |
268 continue; | |
269 | |
270 if (apps->find(drive_app) != apps->end()) | |
271 continue; | |
272 | |
273 apps->insert(std::make_pair( | |
274 drive_app, | |
275 new DriveAppInfo(drive_app->app_id, | |
276 drive_app->app_icons, | |
277 drive_app->document_icons, | |
278 web_store_id, | |
279 product_iter->second, // app name | |
280 drive_app->object_type, | |
281 drive_app->is_primary_selector))); | |
282 } | 234 } |
283 } | 235 } |
284 | 236 |
285 namespace util { | 237 namespace util { |
286 | 238 |
287 GURL FindPreferredIcon( | 239 GURL FindPreferredIcon( |
288 const google_apis::InstalledApp::IconList& icons, | 240 const google_apis::InstalledApp::IconList& icons, |
289 int preferred_size) { | 241 int preferred_size) { |
290 if (icons.empty()) | 242 if (icons.empty()) |
291 return GURL(); | 243 return GURL(); |
292 | 244 |
293 google_apis::InstalledApp::IconList sorted_icons = icons; | 245 google_apis::InstalledApp::IconList sorted_icons = icons; |
294 std::sort(sorted_icons.begin(), sorted_icons.end()); | 246 std::sort(sorted_icons.begin(), sorted_icons.end()); |
295 GURL result = sorted_icons.rbegin()->second; | 247 GURL result = sorted_icons.rbegin()->second; |
296 for (google_apis::InstalledApp::IconList::const_reverse_iterator | 248 for (google_apis::InstalledApp::IconList::const_reverse_iterator |
297 iter = sorted_icons.rbegin(); | 249 iter = sorted_icons.rbegin(); |
298 iter != sorted_icons.rend() && iter->first >= preferred_size; ++iter) { | 250 iter != sorted_icons.rend() && iter->first >= preferred_size; ++iter) { |
299 result = iter->second; | 251 result = iter->second; |
300 } | 252 } |
301 return result; | 253 return result; |
302 } | 254 } |
303 | 255 |
304 } // namespace util | 256 } // namespace util |
305 } // namespace drive | 257 } // namespace drive |
OLD | NEW |