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

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: make generated flag logic easier to read, fix a not-detecting-user-install-properly bug 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
27 using extensions::Extension;
28 using extensions::ExtensionRegistry;
29
30 namespace {
31
32 void IgnoreUninstallResult(google_apis::GDataErrorCode) {
33 }
34
35 } // namespace
36
37 DriveAppProvider::DriveAppProvider(Profile* profile)
38 : profile_(profile),
39 service_bridge_(DriveServiceBridge::Create(profile).Pass()),
40 mapping_(new DriveAppMapping(profile->GetPrefs())),
41 weak_ptr_factory_(this) {
42 service_bridge_->GetAppRegistry()->AddObserver(this);
43 extensions::InstallTracker::Get(profile_)->AddObserver(this);
44 }
45
46 DriveAppProvider::~DriveAppProvider() {
47 extensions::InstallTracker::Get(profile_)->RemoveObserver(this);
48 service_bridge_->GetAppRegistry()->RemoveObserver(this);
49 }
50
51 // static
52 void DriveAppProvider::AppendDependsOnFactories(
53 std::set<BrowserContextKeyedServiceFactory*>* factories) {
54 factories->insert(extensions::InstallTrackerFactory::GetInstance());
55 DriveServiceBridge::AppendDependsOnFactories(factories);
56 }
57
58 void DriveAppProvider::SetDriveServiceBridgeForTest(
59 scoped_ptr<DriveServiceBridge> test_bridge) {
60 service_bridge_->GetAppRegistry()->RemoveObserver(this);
61 service_bridge_ = test_bridge.Pass();
62 service_bridge_->GetAppRegistry()->AddObserver(this);
63 }
64
65 void DriveAppProvider::UpdateMappingAndExtensionSystem(
66 const std::string& drive_app_id,
67 const Extension* new_app,
68 bool is_new_app_generated) {
69 const std::string& new_chrome_app_id = new_app->id();
70
71 const std::string existing_chrome_app_id =
72 mapping_->GetChromeApp(drive_app_id);
73 if (existing_chrome_app_id == new_chrome_app_id)
74 return;
75
76 const bool is_existing_app_generated =
77 mapping_->IsChromeAppGenerated(existing_chrome_app_id);
78 mapping_->Add(drive_app_id, new_chrome_app_id, is_new_app_generated);
79
80 const Extension* existing_app =
81 ExtensionRegistry::Get(profile_)->GetExtensionById(
82 existing_chrome_app_id, ExtensionRegistry::EVERYTHING);
83 if (existing_app && is_existing_app_generated) {
84 extensions::ExtensionSystem::Get(profile_)
85 ->extension_service()
86 ->UninstallExtension(existing_chrome_app_id, false, NULL);
87 }
88 }
89
90 void DriveAppProvider::ProcessDeferredOnExtensionInstalled(
91 const std::string drive_app_id,
92 const std::string chrome_app_id) {
93 const Extension* app = ExtensionRegistry::Get(profile_)->GetExtensionById(
94 chrome_app_id, ExtensionRegistry::EVERYTHING);
95 if (!app)
96 return;
97
98 UpdateMappingAndExtensionSystem(drive_app_id, app, false);
99 }
100
101 void DriveAppProvider::SchedulePendingConverters() {
102 if (pending_converters_.empty())
103 return;
104
105 if (!pending_converters_.front()->IsStarted())
106 pending_converters_.front()->Start();
107 }
108
109 void DriveAppProvider::OnLocalAppConverted(const DriveAppConverter* converter,
110 bool success) {
111 DCHECK_EQ(pending_converters_.front(), converter);
112
113 if (success) {
114 const bool was_generated =
115 mapping_->GetDriveApp(converter->app()->id()) ==
116 converter->app_info().app_id &&
117 mapping_->IsChromeAppGenerated(converter->app()->id());
118 UpdateMappingAndExtensionSystem(
119 converter->app_info().app_id,
120 converter->app(),
121 converter->is_new_install() || was_generated);
122 } else {
123 LOG(WARNING) << "Failed to convert drive app to web app, "
124 << "drive app id= " << converter->app_info().app_id
125 << ", name=" << converter->app_info().app_name;
126 }
127
128 pending_converters_.erase(pending_converters_.begin());
129 SchedulePendingConverters();
130 }
131
132 bool DriveAppProvider::IsMappedUrlAppUpToDate(
133 const drive::DriveAppInfo& drive_app) const {
134 const std::string& url_app_id = mapping_->GetChromeApp(drive_app.app_id);
135 if (url_app_id.empty())
136 return false;
137
138 const Extension* url_app = ExtensionRegistry::Get(profile_)->GetExtensionById(
139 url_app_id, ExtensionRegistry::EVERYTHING);
140 if (!url_app)
141 return false;
142 DCHECK(url_app->is_hosted_app() && url_app->from_bookmark());
143
144 return drive_app.app_name == url_app->name() &&
145 drive_app.create_url ==
146 extensions::AppLaunchInfo::GetLaunchWebURL(url_app);
147 }
148
149 void DriveAppProvider::AddOrUpdateDriveApp(
150 const drive::DriveAppInfo& drive_app) {
151 const Extension* chrome_app =
152 ExtensionRegistry::Get(profile_)->GetExtensionById(
153 drive_app.product_id, ExtensionRegistry::EVERYTHING);
154 if (chrome_app) {
155 UpdateMappingAndExtensionSystem(drive_app.app_id, chrome_app, false);
156 return;
157 }
158
159 if (IsMappedUrlAppUpToDate(drive_app))
160 return;
161
162 ScopedVector<DriveAppConverter>::iterator it = pending_converters_.begin();
163 while (it != pending_converters_.end()) {
164 if (!(*it)->IsStarted() && (*it)->app_info().app_id == drive_app.app_id) {
165 it = pending_converters_.erase(it);
166 } else {
167 ++it;
168 }
169 }
170
171 pending_converters_.push_back(
172 new DriveAppConverter(profile_,
173 drive_app,
174 base::Bind(&DriveAppProvider::OnLocalAppConverted,
175 base::Unretained(this))));
176 }
177
178 void DriveAppProvider::ProcessRemovedDriveApp(const std::string& drive_app_id) {
179 const std::string chrome_app_id = mapping_->GetChromeApp(drive_app_id);
180 const bool is_generated = mapping_->IsChromeAppGenerated(chrome_app_id);
181 mapping_->Remove(drive_app_id);
182
183 if (chrome_app_id.empty() || !is_generated)
184 return;
185
186 const Extension* existing_app =
187 ExtensionRegistry::Get(profile_)
188 ->GetExtensionById(chrome_app_id, ExtensionRegistry::EVERYTHING);
189 if (!existing_app)
190 return;
191
192 extensions::ExtensionSystem::Get(profile_)
193 ->extension_service()
194 ->UninstallExtension(chrome_app_id, false, NULL);
195 }
196
197 void DriveAppProvider::OnDriveAppRegistryUpdated() {
198 service_bridge_->GetAppRegistry()->GetAppList(&drive_apps_);
199
200 IdSet current_ids;
201 for (size_t i = 0; i < drive_apps_.size(); ++i)
202 current_ids.insert(drive_apps_[i].app_id);
203
204 const IdSet existing_ids = mapping_->GetDriveAppIds();
205 const IdSet ids_to_remove =
206 base::STLSetDifference<IdSet>(existing_ids, current_ids);
207 for (IdSet::const_iterator it = ids_to_remove.begin();
208 it != ids_to_remove.end();
209 ++it) {
210 ProcessRemovedDriveApp(*it);
211 }
212
213 for (size_t i = 0; i < drive_apps_.size(); ++i) {
214 AddOrUpdateDriveApp(drive_apps_[i]);
215 }
216 SchedulePendingConverters();
217 }
218
219 void DriveAppProvider::OnExtensionInstalled(const Extension* extension) {
220 // Bail if the |extension| is installed from a converter. The post install
221 // processing will be handled in OnLocalAppConverted.
222 if (!pending_converters_.empty() &&
223 pending_converters_.front()->IsInstalling(extension->id())) {
224 return;
225 }
226
227 // Only user installed app reaches here. If it is mapped, make sure it is not
228 // tagged as generated.
229 const std::string drive_app_id = mapping_->GetDriveApp(extension->id());
230 if (!drive_app_id.empty() &&
231 mapping_->IsChromeAppGenerated(extension->id())) {
232 mapping_->Add(drive_app_id, extension->id(), false);
233 return;
234 }
235
236 for (size_t i = 0; i < drive_apps_.size(); ++i) {
237 if (drive_apps_[i].product_id == extension->id()) {
238 // Defer the processing because it touches the extensions system and
239 // it is better to let the current task finish to avoid unexpected
240 // incomplete status.
241 base::MessageLoop::current()->PostTask(
242 FROM_HERE,
243 base::Bind(&DriveAppProvider::ProcessDeferredOnExtensionInstalled,
244 weak_ptr_factory_.GetWeakPtr(),
245 drive_apps_[i].app_id,
246 extension->id()));
247 return;
248 }
249 }
250 }
251
252 void DriveAppProvider::OnExtensionUninstalled(const Extension* extension) {
253 std::string drive_app_id = mapping_->GetDriveApp(extension->id());
254 if (drive_app_id.empty())
255 return;
256
257 service_bridge_->GetAppRegistry()->UninstallApp(
258 drive_app_id, base::Bind(&IgnoreUninstallResult));
259 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698