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