OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/drive/drive_app_registry.h" | 5 #include "chrome/browser/drive/drive_app_registry.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 // Append list of app ids in |map| looked up by |selector| to |matched_apps|. | 28 // Append list of app ids in |map| looked up by |selector| to |matched_apps|. |
29 void FindAppsForSelector(const std::string& selector, | 29 void FindAppsForSelector(const std::string& selector, |
30 const std::multimap<std::string, std::string>& map, | 30 const std::multimap<std::string, std::string>& map, |
31 std::vector<std::string>* matched_apps) { | 31 std::vector<std::string>* matched_apps) { |
32 typedef std::multimap<std::string, std::string>::const_iterator iterator; | 32 typedef std::multimap<std::string, std::string>::const_iterator iterator; |
33 std::pair<iterator, iterator> range = map.equal_range(selector); | 33 std::pair<iterator, iterator> range = map.equal_range(selector); |
34 for (iterator it = range.first; it != range.second; ++it) | 34 for (iterator it = range.first; it != range.second; ++it) |
35 matched_apps->push_back(it->second); | 35 matched_apps->push_back(it->second); |
36 } | 36 } |
37 | 37 |
38 void RemoveAppFromSelector(const std::string& app_id, | |
39 std::multimap<std::string, std::string>* map) { | |
40 typedef std::multimap<std::string, std::string>::iterator iterator; | |
41 for (iterator it = map->begin(); it != map->end(); ) { | |
42 iterator now = it++; | |
43 if (now->second == app_id) | |
44 map->erase(now); | |
45 } | |
46 } | |
47 | |
48 } // namespace | 38 } // namespace |
49 | 39 |
50 namespace drive { | 40 namespace drive { |
51 | 41 |
52 DriveAppInfo::DriveAppInfo() { | 42 DriveAppInfo::DriveAppInfo() { |
53 } | 43 } |
54 | 44 |
55 DriveAppInfo::DriveAppInfo( | 45 DriveAppInfo::DriveAppInfo( |
56 const std::string& app_id, | 46 const std::string& app_id, |
57 const google_apis::InstalledApp::IconList& app_icons, | 47 const google_apis::InstalledApp::IconList& app_icons, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 if (inserted_app_ids.count(matched_apps[i]) == 0) { | 88 if (inserted_app_ids.count(matched_apps[i]) == 0) { |
99 inserted_app_ids.insert(matched_apps[i]); | 89 inserted_app_ids.insert(matched_apps[i]); |
100 std::map<std::string, DriveAppInfo>::const_iterator it = | 90 std::map<std::string, DriveAppInfo>::const_iterator it = |
101 all_apps_.find(matched_apps[i]); | 91 all_apps_.find(matched_apps[i]); |
102 DCHECK(it != all_apps_.end()); | 92 DCHECK(it != all_apps_.end()); |
103 apps->push_back(it->second); | 93 apps->push_back(it->second); |
104 } | 94 } |
105 } | 95 } |
106 } | 96 } |
107 | 97 |
108 void DriveAppRegistry::GetAppList(std::vector<DriveAppInfo>* apps) const { | |
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
110 | |
111 apps->clear(); | |
112 for (std::map<std::string, DriveAppInfo>::const_iterator | |
113 it = all_apps_.begin(); it != all_apps_.end(); ++it) { | |
114 apps->push_back(it->second); | |
115 } | |
116 } | |
117 | |
118 void DriveAppRegistry::Update() { | 98 void DriveAppRegistry::Update() { |
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
120 | 100 |
121 if (is_updating_) // There is already an update in progress. | 101 if (is_updating_) // There is already an update in progress. |
122 return; | 102 return; |
123 is_updating_ = true; | 103 is_updating_ = true; |
124 | 104 |
125 drive_service_->GetAppList( | 105 drive_service_->GetAppList( |
126 base::Bind(&DriveAppRegistry::UpdateAfterGetAppList, | 106 base::Bind(&DriveAppRegistry::UpdateAfterGetAppList, |
127 weak_ptr_factory_.GetWeakPtr())); | 107 weak_ptr_factory_.GetWeakPtr())); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 app.create_url()); | 153 app.create_url()); |
174 | 154 |
175 // TODO(kinaba): consider taking primary/secondary distinction into account. | 155 // TODO(kinaba): consider taking primary/secondary distinction into account. |
176 AddAppSelectorList(app.primary_mimetypes(), id, &mimetype_map_); | 156 AddAppSelectorList(app.primary_mimetypes(), id, &mimetype_map_); |
177 AddAppSelectorList(app.secondary_mimetypes(), id, &mimetype_map_); | 157 AddAppSelectorList(app.secondary_mimetypes(), id, &mimetype_map_); |
178 AddAppSelectorList(app.primary_file_extensions(), id, &extension_map_); | 158 AddAppSelectorList(app.primary_file_extensions(), id, &extension_map_); |
179 AddAppSelectorList(app.secondary_file_extensions(), id, &extension_map_); | 159 AddAppSelectorList(app.secondary_file_extensions(), id, &extension_map_); |
180 } | 160 } |
181 } | 161 } |
182 | 162 |
183 void DriveAppRegistry::UninstallApp(const std::string& app_id, | |
184 const UninstallCallback& callback) { | |
185 DCHECK(!callback.is_null()); | |
186 | |
187 drive_service_->UninstallApp(app_id, | |
188 base::Bind(&DriveAppRegistry::OnAppUninstalled, | |
189 weak_ptr_factory_.GetWeakPtr(), | |
190 app_id, | |
191 callback)); | |
192 } | |
193 | |
194 void DriveAppRegistry::OnAppUninstalled(const std::string& app_id, | |
195 const UninstallCallback& callback, | |
196 google_apis::GDataErrorCode error) { | |
197 if (error == google_apis::HTTP_SUCCESS) { | |
198 all_apps_.erase(app_id); | |
199 RemoveAppFromSelector(app_id, &mimetype_map_); | |
200 RemoveAppFromSelector(app_id, &extension_map_); | |
201 } | |
202 callback.Run(error); | |
203 } | |
204 | |
205 // static | |
206 bool DriveAppRegistry::IsAppUninstallSupported() { | |
207 #ifdef USE_OFFICIAL_GOOGLE_API_KEYS | |
208 return true; | |
209 #else | |
210 return false; | |
211 #endif | |
212 } | |
213 | |
214 namespace util { | 163 namespace util { |
215 | 164 |
216 GURL FindPreferredIcon(const google_apis::InstalledApp::IconList& icons, | 165 GURL FindPreferredIcon(const google_apis::InstalledApp::IconList& icons, |
217 int preferred_size) { | 166 int preferred_size) { |
218 if (icons.empty()) | 167 if (icons.empty()) |
219 return GURL(); | 168 return GURL(); |
220 | 169 |
221 google_apis::InstalledApp::IconList sorted_icons = icons; | 170 google_apis::InstalledApp::IconList sorted_icons = icons; |
222 std::sort(sorted_icons.rbegin(), sorted_icons.rend()); | 171 std::sort(sorted_icons.rbegin(), sorted_icons.rend()); |
223 | 172 |
224 // Go forward while the size is larger or equal to preferred_size. | 173 // Go forward while the size is larger or equal to preferred_size. |
225 size_t i = 1; | 174 size_t i = 1; |
226 while (i < sorted_icons.size() && sorted_icons[i].first >= preferred_size) | 175 while (i < sorted_icons.size() && sorted_icons[i].first >= preferred_size) |
227 ++i; | 176 ++i; |
228 return sorted_icons[i - 1].second; | 177 return sorted_icons[i - 1].second; |
229 } | 178 } |
230 | 179 |
231 } // namespace util | 180 } // namespace util |
232 } // namespace drive | 181 } // namespace drive |
OLD | NEW |