Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: chrome/browser/drive/drive_app_registry.cc

Issue 1190203002: Move (most of) chrome/browser/drive into components/drive/service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebasing... Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/drive/drive_app_registry.h"
6
7 #include <algorithm>
8 #include <set>
9 #include <utility>
10
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "chrome/browser/drive/drive_app_registry_observer.h"
14 #include "chrome/browser/drive/drive_service_interface.h"
15 #include "google_apis/drive/drive_api_parser.h"
16 #include "google_apis/google_api_keys.h"
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 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
49
50 namespace drive {
51
52 DriveAppInfo::DriveAppInfo() {
53 }
54
55 DriveAppInfo::DriveAppInfo(
56 const std::string& app_id,
57 const std::string& product_id,
58 const IconList& app_icons,
59 const IconList& document_icons,
60 const std::string& app_name,
61 const GURL& create_url,
62 bool is_removable)
63 : app_id(app_id),
64 product_id(product_id),
65 app_icons(app_icons),
66 document_icons(document_icons),
67 app_name(app_name),
68 create_url(create_url),
69 is_removable(is_removable) {
70 }
71
72 DriveAppInfo::~DriveAppInfo() {
73 }
74
75 DriveAppRegistry::DriveAppRegistry(DriveServiceInterface* drive_service)
76 : drive_service_(drive_service),
77 is_updating_(false),
78 weak_ptr_factory_(this) {
79 }
80
81 DriveAppRegistry::~DriveAppRegistry() {
82 }
83
84 void DriveAppRegistry::GetAppsForFile(
85 const base::FilePath::StringType& file_extension,
86 const std::string& mime_type,
87 std::vector<DriveAppInfo>* apps) const {
88 DCHECK(thread_checker_.CalledOnValidThread());
89
90 std::vector<std::string> matched_apps;
91 if (!file_extension.empty()) {
92 const std::string without_dot =
93 base::FilePath(file_extension.substr(1)).AsUTF8Unsafe();
94 FindAppsForSelector(without_dot, extension_map_, &matched_apps);
95 }
96 if (!mime_type.empty())
97 FindAppsForSelector(mime_type, mimetype_map_, &matched_apps);
98
99 // Insert found Drive apps into |apps|, but skip duplicate results.
100 std::set<std::string> inserted_app_ids;
101 for (size_t i = 0; i < matched_apps.size(); ++i) {
102 if (inserted_app_ids.count(matched_apps[i]) == 0) {
103 inserted_app_ids.insert(matched_apps[i]);
104 std::map<std::string, DriveAppInfo>::const_iterator it =
105 all_apps_.find(matched_apps[i]);
106 DCHECK(it != all_apps_.end());
107 apps->push_back(it->second);
108 }
109 }
110 }
111
112 void DriveAppRegistry::GetAppList(std::vector<DriveAppInfo>* apps) const {
113 DCHECK(thread_checker_.CalledOnValidThread());
114
115 apps->clear();
116 for (std::map<std::string, DriveAppInfo>::const_iterator
117 it = all_apps_.begin(); it != all_apps_.end(); ++it) {
118 apps->push_back(it->second);
119 }
120 }
121
122 void DriveAppRegistry::Update() {
123 DCHECK(thread_checker_.CalledOnValidThread());
124
125 if (is_updating_) // There is already an update in progress.
126 return;
127 is_updating_ = true;
128
129 drive_service_->GetAppList(
130 base::Bind(&DriveAppRegistry::UpdateAfterGetAppList,
131 weak_ptr_factory_.GetWeakPtr()));
132 }
133
134 void DriveAppRegistry::UpdateAfterGetAppList(
135 google_apis::DriveApiErrorCode gdata_error,
136 scoped_ptr<google_apis::AppList> app_list) {
137 DCHECK(thread_checker_.CalledOnValidThread());
138
139 DCHECK(is_updating_);
140 is_updating_ = false;
141
142 // Failed to fetch the data from the server. We can do nothing here.
143 if (gdata_error != google_apis::HTTP_SUCCESS)
144 return;
145
146 DCHECK(app_list);
147 UpdateFromAppList(*app_list);
148 }
149
150 void DriveAppRegistry::UpdateFromAppList(const google_apis::AppList& app_list) {
151 all_apps_.clear();
152 extension_map_.clear();
153 mimetype_map_.clear();
154
155 for (size_t i = 0; i < app_list.items().size(); ++i) {
156 const google_apis::AppResource& app = *app_list.items()[i];
157 const std::string id = app.application_id();
158
159 DriveAppInfo::IconList app_icons;
160 DriveAppInfo::IconList document_icons;
161 for (size_t j = 0; j < app.icons().size(); ++j) {
162 const google_apis::DriveAppIcon& icon = *app.icons()[j];
163 if (icon.icon_url().is_empty())
164 continue;
165 if (icon.category() == google_apis::DriveAppIcon::APPLICATION)
166 app_icons.push_back(std::make_pair(icon.icon_side_length(),
167 icon.icon_url()));
168 if (icon.category() == google_apis::DriveAppIcon::DOCUMENT)
169 document_icons.push_back(std::make_pair(icon.icon_side_length(),
170 icon.icon_url()));
171 }
172
173 all_apps_[id] = DriveAppInfo(app.application_id(),
174 app.product_id(),
175 app_icons,
176 document_icons,
177 app.name(),
178 app.create_url(),
179 app.is_removable());
180
181 // TODO(kinaba): consider taking primary/secondary distinction into account.
182 AddAppSelectorList(app.primary_mimetypes(), id, &mimetype_map_);
183 AddAppSelectorList(app.secondary_mimetypes(), id, &mimetype_map_);
184 AddAppSelectorList(app.primary_file_extensions(), id, &extension_map_);
185 AddAppSelectorList(app.secondary_file_extensions(), id, &extension_map_);
186 }
187
188 FOR_EACH_OBSERVER(DriveAppRegistryObserver,
189 observers_,
190 OnDriveAppRegistryUpdated());
191 }
192
193 void DriveAppRegistry::AddObserver(DriveAppRegistryObserver* observer) {
194 observers_.AddObserver(observer);
195 }
196
197 void DriveAppRegistry::RemoveObserver(DriveAppRegistryObserver* observer) {
198 observers_.RemoveObserver(observer);
199 }
200
201 void DriveAppRegistry::UninstallApp(const std::string& app_id,
202 const UninstallCallback& callback) {
203 DCHECK(!callback.is_null());
204
205 drive_service_->UninstallApp(app_id,
206 base::Bind(&DriveAppRegistry::OnAppUninstalled,
207 weak_ptr_factory_.GetWeakPtr(),
208 app_id,
209 callback));
210 }
211
212 void DriveAppRegistry::OnAppUninstalled(const std::string& app_id,
213 const UninstallCallback& callback,
214 google_apis::DriveApiErrorCode error) {
215 if (error == google_apis::HTTP_NO_CONTENT) {
216 all_apps_.erase(app_id);
217 RemoveAppFromSelector(app_id, &mimetype_map_);
218 RemoveAppFromSelector(app_id, &extension_map_);
219 }
220 callback.Run(error);
221 }
222
223 // static
224 bool DriveAppRegistry::IsAppUninstallSupported() {
225 return google_apis::IsGoogleChromeAPIKeyUsed();
226 }
227
228 namespace util {
229
230 GURL FindPreferredIcon(const DriveAppInfo::IconList& icons,
231 int preferred_size) {
232 if (icons.empty())
233 return GURL();
234
235 DriveAppInfo::IconList sorted_icons = icons;
236 std::sort(sorted_icons.rbegin(), sorted_icons.rend());
237
238 // Go forward while the size is larger or equal to preferred_size.
239 size_t i = 1;
240 while (i < sorted_icons.size() && sorted_icons[i].first >= preferred_size)
241 ++i;
242 return sorted_icons[i - 1].second;
243 }
244
245 } // namespace util
246 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/drive/drive_app_registry.h ('k') | chrome/browser/drive/drive_app_registry_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698