| 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/chromeos/drive/drive_app_registry.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <set> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "chrome/browser/drive/drive_service_interface.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 #include "google_apis/drive/drive_api_parser.h" | |
| 15 | |
| 16 using content::BrowserThread; | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Add {selector -> app_id} mapping to |map|. | |
| 21 void AddAppSelectorList(const ScopedVector<std::string>& selectors, | |
| 22 const std::string& app_id, | |
| 23 std::multimap<std::string, std::string>* map) { | |
| 24 for (size_t i = 0; i < selectors.size(); ++i) | |
| 25 map->insert(std::make_pair(*selectors[i], app_id)); | |
| 26 } | |
| 27 | |
| 28 // Append list of app ids in |map| looked up by |selector| to |matched_apps|. | |
| 29 void FindAppsForSelector(const std::string& selector, | |
| 30 const std::multimap<std::string, std::string>& map, | |
| 31 std::vector<std::string>* matched_apps) { | |
| 32 typedef std::multimap<std::string, std::string>::const_iterator iterator; | |
| 33 std::pair<iterator, iterator> range = map.equal_range(selector); | |
| 34 for (iterator it = range.first; it != range.second; ++it) | |
| 35 matched_apps->push_back(it->second); | |
| 36 } | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 namespace drive { | |
| 41 | |
| 42 DriveAppInfo::DriveAppInfo() { | |
| 43 } | |
| 44 | |
| 45 DriveAppInfo::DriveAppInfo( | |
| 46 const std::string& app_id, | |
| 47 const google_apis::InstalledApp::IconList& app_icons, | |
| 48 const google_apis::InstalledApp::IconList& document_icons, | |
| 49 const std::string& app_name, | |
| 50 const GURL& create_url) | |
| 51 : app_id(app_id), | |
| 52 app_icons(app_icons), | |
| 53 document_icons(document_icons), | |
| 54 app_name(app_name), | |
| 55 create_url(create_url) { | |
| 56 } | |
| 57 | |
| 58 DriveAppInfo::~DriveAppInfo() { | |
| 59 } | |
| 60 | |
| 61 DriveAppRegistry::DriveAppRegistry(DriveServiceInterface* drive_service) | |
| 62 : drive_service_(drive_service), | |
| 63 is_updating_(false), | |
| 64 weak_ptr_factory_(this) { | |
| 65 } | |
| 66 | |
| 67 DriveAppRegistry::~DriveAppRegistry() { | |
| 68 } | |
| 69 | |
| 70 void DriveAppRegistry::GetAppsForFile( | |
| 71 const base::FilePath::StringType& file_extension, | |
| 72 const std::string& mime_type, | |
| 73 std::vector<DriveAppInfo>* apps) const { | |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 75 | |
| 76 std::vector<std::string> matched_apps; | |
| 77 if (!file_extension.empty()) { | |
| 78 const base::FilePath::StringType without_dot = file_extension.substr(1); | |
| 79 FindAppsForSelector(without_dot, extension_map_, &matched_apps); | |
| 80 } | |
| 81 if (!mime_type.empty()) | |
| 82 FindAppsForSelector(mime_type, mimetype_map_, &matched_apps); | |
| 83 | |
| 84 // Insert found Drive apps into |apps|, but skip duplicate results. | |
| 85 std::set<std::string> inserted_app_ids; | |
| 86 for (size_t i = 0; i < matched_apps.size(); ++i) { | |
| 87 if (inserted_app_ids.count(matched_apps[i]) == 0) { | |
| 88 inserted_app_ids.insert(matched_apps[i]); | |
| 89 std::map<std::string, DriveAppInfo>::const_iterator it = | |
| 90 all_apps_.find(matched_apps[i]); | |
| 91 DCHECK(it != all_apps_.end()); | |
| 92 apps->push_back(it->second); | |
| 93 } | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void DriveAppRegistry::Update() { | |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 99 | |
| 100 if (is_updating_) // There is already an update in progress. | |
| 101 return; | |
| 102 is_updating_ = true; | |
| 103 | |
| 104 drive_service_->GetAppList( | |
| 105 base::Bind(&DriveAppRegistry::UpdateAfterGetAppList, | |
| 106 weak_ptr_factory_.GetWeakPtr())); | |
| 107 } | |
| 108 | |
| 109 void DriveAppRegistry::UpdateAfterGetAppList( | |
| 110 google_apis::GDataErrorCode gdata_error, | |
| 111 scoped_ptr<google_apis::AppList> app_list) { | |
| 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 113 | |
| 114 DCHECK(is_updating_); | |
| 115 is_updating_ = false; | |
| 116 | |
| 117 // Failed to fetch the data from the server. We can do nothing here. | |
| 118 if (gdata_error != google_apis::HTTP_SUCCESS) | |
| 119 return; | |
| 120 | |
| 121 DCHECK(app_list); | |
| 122 UpdateFromAppList(*app_list); | |
| 123 } | |
| 124 | |
| 125 void DriveAppRegistry::UpdateFromAppList(const google_apis::AppList& app_list) { | |
| 126 all_apps_.clear(); | |
| 127 extension_map_.clear(); | |
| 128 mimetype_map_.clear(); | |
| 129 | |
| 130 for (size_t i = 0; i < app_list.items().size(); ++i) { | |
| 131 const google_apis::AppResource& app = *app_list.items()[i]; | |
| 132 const std::string id = app.application_id(); | |
| 133 | |
| 134 google_apis::InstalledApp::IconList app_icons; | |
| 135 google_apis::InstalledApp::IconList document_icons; | |
| 136 for (size_t j = 0; j < app.icons().size(); ++j) { | |
| 137 const google_apis::DriveAppIcon& icon = *app.icons()[j]; | |
| 138 if (icon.icon_url().is_empty()) | |
| 139 continue; | |
| 140 if (icon.category() == google_apis::DriveAppIcon::APPLICATION) | |
| 141 app_icons.push_back(std::make_pair(icon.icon_side_length(), | |
| 142 icon.icon_url())); | |
| 143 if (icon.category() == google_apis::DriveAppIcon::DOCUMENT) | |
| 144 document_icons.push_back(std::make_pair(icon.icon_side_length(), | |
| 145 icon.icon_url())); | |
| 146 } | |
| 147 | |
| 148 all_apps_[id] = DriveAppInfo(app.application_id(), | |
| 149 app_icons, | |
| 150 document_icons, | |
| 151 app.name(), | |
| 152 app.create_url()); | |
| 153 | |
| 154 // TODO(kinaba): consider taking primary/secondary distinction into account. | |
| 155 AddAppSelectorList(app.primary_mimetypes(), id, &mimetype_map_); | |
| 156 AddAppSelectorList(app.secondary_mimetypes(), id, &mimetype_map_); | |
| 157 AddAppSelectorList(app.primary_file_extensions(), id, &extension_map_); | |
| 158 AddAppSelectorList(app.secondary_file_extensions(), id, &extension_map_); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 namespace util { | |
| 163 | |
| 164 GURL FindPreferredIcon(const google_apis::InstalledApp::IconList& icons, | |
| 165 int preferred_size) { | |
| 166 if (icons.empty()) | |
| 167 return GURL(); | |
| 168 | |
| 169 google_apis::InstalledApp::IconList sorted_icons = icons; | |
| 170 std::sort(sorted_icons.rbegin(), sorted_icons.rend()); | |
| 171 | |
| 172 // Go forward while the size is larger or equal to preferred_size. | |
| 173 size_t i = 1; | |
| 174 while (i < sorted_icons.size() && sorted_icons[i].first >= preferred_size) | |
| 175 ++i; | |
| 176 return sorted_icons[i - 1].second; | |
| 177 } | |
| 178 | |
| 179 } // namespace util | |
| 180 } // namespace drive | |
| OLD | NEW |