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

Side by Side Diff: chrome/browser/apps/drive/drive_app_provider.cc

Issue 308003005: app_list: Drive app integration. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: crx_installer changes Created 6 years, 6 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 | Annotate | Revision Log
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/apps/drive/drive_app_provider.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/stl_util.h"
14 #include "chrome/browser/apps/drive/drive_app_converter.h"
15 #include "chrome/browser/apps/drive/drive_app_mapping.h"
16 #include "chrome/browser/apps/drive/drive_service_bridge.h"
17 #include "chrome/browser/drive/drive_app_registry.h"
18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/install_tracker.h"
20 #include "chrome/browser/extensions/install_tracker_factory.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
23 #include "extensions/browser/extension_registry.h"
24 #include "extensions/browser/extension_system.h"
25 #include "extensions/common/extension.h"
26 #include "ui/app_list/app_list_item.h"
27 #include "ui/app_list/app_list_model.h"
28
29 using extensions::Extension;
30 using extensions::ExtensionRegistry;
31
32 namespace {
33
34 void IgnoreUninstallResult(google_apis::GDataErrorCode) {
35 }
36
37 } // namespace
38
39 DriveAppProvider::DriveAppProvider(Profile* profile,
40 app_list::AppListModel* model)
41 : profile_(profile),
42 model_(model),
43 service_bridge_(DriveServiceBridge::Create(profile).Pass()),
44 mapping_(new DriveAppMapping(profile->GetPrefs())),
45 weak_ptr_factory_(this) {
46 service_bridge_->GetAppRegistry()->AddObserver(this);
47 extensions::InstallTracker::Get(profile_)->AddObserver(this);
48 }
49
50 DriveAppProvider::~DriveAppProvider() {
51 extensions::InstallTracker::Get(profile_)->RemoveObserver(this);
52 service_bridge_->GetAppRegistry()->RemoveObserver(this);
53 }
54
55 // static
56 void DriveAppProvider::AppendDependsOnFactories(
57 std::set<BrowserContextKeyedServiceFactory*>* factories) {
58 factories->insert(extensions::InstallTrackerFactory::GetInstance());
59 DriveServiceBridge::AppendDependsOnFactories(factories);
60 }
61
62 void DriveAppProvider::SetDriveServiceBridgeForTest(
63 scoped_ptr<DriveServiceBridge> test_bridge) {
64 service_bridge_->GetAppRegistry()->RemoveObserver(this);
65 service_bridge_ = test_bridge.Pass();
66 service_bridge_->GetAppRegistry()->AddObserver(this);
67 }
68
69 void DriveAppProvider::MigrateModelSettings(
70 const std::string& drive_app_id,
71 const std::string& old_chrome_app_id,
72 const std::string& new_chrome_app_id) {
73 if (!model_ || old_chrome_app_id == new_chrome_app_id)
74 return;
75
76 app_list::AppListItem* old_app_item = model_->FindItem(old_chrome_app_id);
77 if (!old_app_item) {
78 NOTREACHED();
79 return;
80 }
81
82 app_list::AppListItem* new_app_item = model_->FindItem(new_chrome_app_id);
83 if (!new_app_item) {
84 NOTREACHED();
85 return;
86 }
87
88 model_->MoveItemToFolderAt(
89 new_app_item, old_app_item->folder_id(), old_app_item->position());
90 }
91
92 void DriveAppProvider::UpdateMappingAndExtensionSystem(
93 const std::string& drive_app_id,
94 const Extension* new_app) {
95 const std::string& new_chrome_app_id = new_app->id();
96
97 const std::string existing_chrome_app_id =
98 mapping_->GetChromeApp(drive_app_id);
99 if (existing_chrome_app_id.empty()) {
100 mapping_->Add(drive_app_id, new_chrome_app_id);
101 return;
102 }
103
104 if (existing_chrome_app_id == new_chrome_app_id)
105 return;
106
107 const Extension* existing_app =
108 ExtensionRegistry::Get(profile_)->GetExtensionById(
109 existing_chrome_app_id, ExtensionRegistry::EVERYTHING);
110 if (!existing_app) {
111 mapping_->Add(drive_app_id, new_chrome_app_id);
112 return;
113 }
114
115 if (existing_app->from_bookmark()) {
116 MigrateModelSettings(
117 drive_app_id, existing_chrome_app_id, new_chrome_app_id);
118 mapping_->Add(drive_app_id, new_chrome_app_id);
119 extensions::ExtensionSystem::Get(profile_)
120 ->extension_service()
121 ->UninstallExtension(existing_chrome_app_id, false, NULL);
122 return;
123 }
124
125 if (new_app->from_bookmark()) {
126 // New app is an URL app. Existing app is not URL app. Keep existing.
127 extensions::ExtensionSystem::Get(profile_)
128 ->extension_service()
129 ->UninstallExtension(new_chrome_app_id, false, NULL);
130 } else {
131 // Both existing and new chrome apps are not URL apps, keep existing app
132 // but update mapping to use the new one.
133 mapping_->Add(drive_app_id, new_chrome_app_id);
134 }
135 }
136
137 void DriveAppProvider::ProcessDeferredOnExtensionInstalled(
138 const std::string drive_app_id,
139 const std::string chrome_app_id) {
140 const Extension* app = ExtensionRegistry::Get(profile_)->GetExtensionById(
141 chrome_app_id, ExtensionRegistry::EVERYTHING);
142 if (!app)
143 return;
144
145 UpdateMappingAndExtensionSystem(drive_app_id, app);
146 }
147
148 void DriveAppProvider::SchedulePendingConverters() {
149 if (pending_converters_.empty())
150 return;
151
152 if (!pending_converters_.front()->IsStarted())
153 pending_converters_.front()->Start();
154 }
155
156 void DriveAppProvider::OnLocalAppConverted(const DriveAppConverter* converter,
157 bool success) {
158 DCHECK_EQ(pending_converters_.front(), converter);
159
160 if (success) {
161 UpdateMappingAndExtensionSystem(converter->app_info().app_id,
162 converter->app());
163 } else {
164 LOG(WARNING) << "Failed to convert drive app to web app, "
165 << "drive app id= " << converter->app_info().app_id
166 << ", name=" << converter->app_info().app_name;
167 }
168
169 pending_converters_.erase(pending_converters_.begin());
170 SchedulePendingConverters();
171 }
172
173 bool DriveAppProvider::IsDriveAppUpToDate(
174 const drive::DriveAppInfo& drive_app) const {
175 const std::string& url_app_id = mapping_->GetChromeApp(drive_app.app_id);
176 if (url_app_id.empty())
177 return false;
178
179 const Extension* url_app = ExtensionRegistry::Get(profile_)->GetExtensionById(
180 url_app_id, ExtensionRegistry::EVERYTHING);
181 if (!url_app)
182 return false;
183
184 return drive_app.app_name == url_app->name() &&
185 drive_app.create_url ==
186 extensions::AppLaunchInfo::GetLaunchWebURL(url_app);
187 }
188
189 void DriveAppProvider::AddOrUpdateDriveApp(
190 const drive::DriveAppInfo& drive_app) {
191 const Extension* chrome_app =
192 ExtensionRegistry::Get(profile_)->GetExtensionById(
193 drive_app.product_id, ExtensionRegistry::EVERYTHING);
194 if (chrome_app) {
195 UpdateMappingAndExtensionSystem(drive_app.app_id, chrome_app);
196 return;
197 }
198
199 if (IsDriveAppUpToDate(drive_app))
200 return;
201
202 ScopedVector<DriveAppConverter>::iterator it = pending_converters_.begin();
203 while (it != pending_converters_.end()) {
204 if (!(*it)->IsStarted() && (*it)->app_info().app_id == drive_app.app_id) {
205 it = pending_converters_.erase(it);
206 } else {
207 ++it;
208 }
209 }
210
211 pending_converters_.push_back(
212 new DriveAppConverter(profile_,
213 drive_app,
214 base::Bind(&DriveAppProvider::OnLocalAppConverted,
215 base::Unretained(this))));
216 }
217
218 void DriveAppProvider::RemoveDriveApp(const std::string& drive_app_id) {
219 const std::string chrome_app_id = mapping_->GetChromeApp(drive_app_id);
220 mapping_->Remove(drive_app_id);
221
222 if (chrome_app_id.empty())
223 return;
224
225 const Extension* existing_app =
226 ExtensionRegistry::Get(profile_)
227 ->GetExtensionById(chrome_app_id, ExtensionRegistry::EVERYTHING);
228 if (!existing_app || !existing_app->from_bookmark())
229 return;
230
231 extensions::ExtensionSystem::Get(profile_)
232 ->extension_service()
233 ->UninstallExtension(chrome_app_id, false, NULL);
234 }
235
236 void DriveAppProvider::OnDriveAppRegistryUpdated() {
237 service_bridge_->GetAppRegistry()->GetAppList(&drive_apps_);
238
239 IdSet current_ids;
240 for (size_t i = 0; i < drive_apps_.size(); ++i)
241 current_ids.insert(drive_apps_[i].app_id);
242
243 const IdSet existing_ids = mapping_->GetDriveAppIds();
244 const IdSet ids_to_remove =
245 base::STLSetDifference<IdSet>(existing_ids, current_ids);
246 for (IdSet::const_iterator it = ids_to_remove.begin();
247 it != ids_to_remove.end();
248 ++it) {
249 RemoveDriveApp(*it);
250 }
251
252 for (size_t i = 0; i < drive_apps_.size(); ++i) {
253 AddOrUpdateDriveApp(drive_apps_[i]);
254 }
255 SchedulePendingConverters();
256 }
257
258 void DriveAppProvider::OnExtensionInstalled(const Extension* extension) {
259 for (size_t i = 0; i < drive_apps_.size(); ++i) {
260 if (drive_apps_[i].product_id == extension->id()) {
261 base::MessageLoop::current()->PostTask(
262 FROM_HERE,
263 base::Bind(&DriveAppProvider::ProcessDeferredOnExtensionInstalled,
264 weak_ptr_factory_.GetWeakPtr(),
265 drive_apps_[i].app_id,
266 extension->id()));
267 return;
268 }
269 }
270 }
271
272 void DriveAppProvider::OnExtensionUninstalled(const Extension* extension) {
273 std::string drive_app_id = mapping_->GetDriveApp(extension->id());
274 if (drive_app_id.empty())
275 return;
276
277 service_bridge_->GetAppRegistry()->UninstallApp(
278 drive_app_id, base::Bind(&IgnoreUninstallResult));
279 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698