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

Side by Side Diff: chrome/browser/ui/app_list/app_list_syncable_service.cc

Issue 118463002: Sync removal of Default apps. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Don't BuildModel() if no extension service. Created 7 years 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/ui/app_list/app_list_syncable_service.h" 5 #include "chrome/browser/ui/app_list/app_list_syncable_service.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/extension_prefs.h"
9 #include "chrome/browser/extensions/extension_service.h" 10 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/app_list/app_list_service.h" 12 #include "chrome/browser/ui/app_list/app_list_service.h"
13 #include "chrome/browser/ui/app_list/extension_app_item.h" 13 #include "chrome/browser/ui/app_list/extension_app_item.h"
14 #include "chrome/browser/ui/app_list/extension_app_model_builder.h" 14 #include "chrome/browser/ui/app_list/extension_app_model_builder.h"
15 #include "chrome/browser/ui/host_desktop.h" 15 #include "chrome/browser/ui/host_desktop.h"
16 #include "chrome/common/chrome_switches.h" 16 #include "chrome/common/chrome_switches.h"
17 #include "content/public/browser/notification_source.h" 17 #include "content/public/browser/notification_source.h"
18 #include "sync/api/sync_change_processor.h" 18 #include "sync/api/sync_change_processor.h"
19 #include "sync/api/sync_data.h" 19 #include "sync/api/sync_data.h"
20 #include "sync/api/sync_merge_result.h" 20 #include "sync/api/sync_merge_result.h"
21 #include "sync/protocol/sync.pb.h" 21 #include "sync/protocol/sync.pb.h"
22 #include "ui/app_list/app_list_folder_item.h"
22 #include "ui/app_list/app_list_item_model.h" 23 #include "ui/app_list/app_list_item_model.h"
23 #include "ui/app_list/app_list_model.h" 24 #include "ui/app_list/app_list_model.h"
24 25
25 using syncer::SyncChange; 26 using syncer::SyncChange;
26 27
27 namespace app_list { 28 namespace app_list {
28 29
29 namespace { 30 namespace {
30 31
31 void UpdateSyncItemFromSync(const sync_pb::AppListSpecifics& specifics, 32 void UpdateSyncItemFromSync(const sync_pb::AppListSpecifics& specifics,
32 AppListSyncableService::SyncItem* item) { 33 AppListSyncableService::SyncItem* item) {
33 DCHECK_EQ(item->item_id, specifics.item_id()); 34 DCHECK_EQ(item->item_id, specifics.item_id());
34 item->item_type = specifics.item_type(); 35 item->item_type = specifics.item_type();
35 item->item_name = specifics.item_name(); 36 item->item_name = specifics.item_name();
36 item->parent_id = specifics.parent_id(); 37 item->parent_id = specifics.parent_id();
37 if (!specifics.page_ordinal().empty()) 38 if (!specifics.page_ordinal().empty())
38 item->page_ordinal = syncer::StringOrdinal(specifics.page_ordinal()); 39 item->page_ordinal = syncer::StringOrdinal(specifics.page_ordinal());
39 if (!specifics.item_ordinal().empty()) 40 if (!specifics.item_ordinal().empty())
40 item->item_ordinal = syncer::StringOrdinal(specifics.item_ordinal()); 41 item->item_ordinal = syncer::StringOrdinal(specifics.item_ordinal());
41 } 42 }
42 43
43 bool UpdateSyncItemFromAppItem(const AppListItemModel* app_item, 44 bool UpdateSyncItemFromAppItem(const AppListItemModel* app_item,
44 AppListSyncableService::SyncItem* sync_item) { 45 AppListSyncableService::SyncItem* sync_item) {
45 DCHECK_EQ(sync_item->item_id, app_item->id()); 46 DCHECK_EQ(sync_item->item_id, app_item->id());
46 bool changed = false; 47 bool changed = false;
48 if (sync_item->item_name != app_item->title()) {
49 sync_item->item_name = app_item->title();
50 changed = true;
51 }
47 if (!sync_item->item_ordinal.IsValid() || 52 if (!sync_item->item_ordinal.IsValid() ||
48 !app_item->position().Equals(sync_item->item_ordinal)) { 53 !app_item->position().Equals(sync_item->item_ordinal)) {
49 sync_item->item_ordinal = app_item->position(); 54 sync_item->item_ordinal = app_item->position();
50 changed = true; 55 changed = true;
51 } 56 }
52 // TODO(stevenjb): Set parent_id and page_ordinal. 57 // TODO(stevenjb): Set parent_id and page_ordinal.
53 return changed; 58 return changed;
54 } 59 }
55 60
56 void GetSyncSpecificsFromSyncItem(const AppListSyncableService::SyncItem* item, 61 void GetSyncSpecificsFromSyncItem(const AppListSyncableService::SyncItem* item,
(...skipping 11 matching lines...) Expand all
68 73
69 syncer::SyncData GetSyncDataFromSyncItem( 74 syncer::SyncData GetSyncDataFromSyncItem(
70 const AppListSyncableService::SyncItem* item) { 75 const AppListSyncableService::SyncItem* item) {
71 sync_pb::EntitySpecifics specifics; 76 sync_pb::EntitySpecifics specifics;
72 GetSyncSpecificsFromSyncItem(item, specifics.mutable_app_list()); 77 GetSyncSpecificsFromSyncItem(item, specifics.mutable_app_list());
73 return syncer::SyncData::CreateLocalData(item->item_id, 78 return syncer::SyncData::CreateLocalData(item->item_id,
74 item->item_id, 79 item->item_id,
75 specifics); 80 specifics);
76 } 81 }
77 82
83 bool AppIsDefault(ExtensionService* service, const std::string& id) {
84 return service && service->extension_prefs()->WasInstalledByDefault(id);
85 }
86
87 bool AppIsPlatformApp(ExtensionService* service, const std::string& id) {
88 if (!service)
89 return false;
90 const extensions::Extension* app = service->GetInstalledExtension(id);
91 DVLOG_IF(1, !app) << "No App for ID: " << id;
92 return app ? app->is_platform_app() : false;
93 }
94
95 void UninstallExtension(ExtensionService* service, const std::string& id) {
96 if (service && service->GetInstalledExtension(id))
97 service->UninstallExtension(id, false, NULL);
98 }
99
78 } // namespace 100 } // namespace
79 101
80 // AppListSyncableService::SyncItem 102 // AppListSyncableService::SyncItem
81 103
82 AppListSyncableService::SyncItem::SyncItem( 104 AppListSyncableService::SyncItem::SyncItem(
83 const std::string& id, 105 const std::string& id,
84 sync_pb::AppListSpecifics::AppListItemType type) 106 sync_pb::AppListSpecifics::AppListItemType type)
85 : item_id(id), 107 : item_id(id),
86 item_type(type) { 108 item_type(type) {
87 } 109 }
88 110
89 AppListSyncableService::SyncItem::~SyncItem() { 111 AppListSyncableService::SyncItem::~SyncItem() {
90 } 112 }
91 113
92 // AppListSyncableService 114 // AppListSyncableService
93 115
94 AppListSyncableService::AppListSyncableService( 116 AppListSyncableService::AppListSyncableService(
95 Profile* profile, 117 Profile* profile,
96 ExtensionService* extension_service) 118 ExtensionService* extension_service)
97 : profile_(profile), 119 : profile_(profile),
120 extension_service_(extension_service),
98 model_(new AppListModel) { 121 model_(new AppListModel) {
99 if (extension_service && extension_service->is_ready()) { 122 if (!extension_service) {
123 LOG(WARNING) << "AppListSyncableService created with no ExtensionService";
124 return;
125 }
126
127 if (extension_service->is_ready()) {
100 BuildModel(); 128 BuildModel();
101 return; 129 return;
102 } 130 }
103 131
104 // The extensions for this profile have not yet all been loaded. 132 // The extensions for this profile have not yet all been loaded.
105 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, 133 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
106 content::Source<Profile>(profile)); 134 content::Source<Profile>(profile));
107 } 135 }
108 136
109 AppListSyncableService::~AppListSyncableService() { 137 AppListSyncableService::~AppListSyncableService() {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 } 173 }
146 174
147 const AppListSyncableService::SyncItem* 175 const AppListSyncableService::SyncItem*
148 AppListSyncableService::GetSyncItem(const std::string& id) const { 176 AppListSyncableService::GetSyncItem(const std::string& id) const {
149 SyncItemMap::const_iterator iter = sync_items_.find(id); 177 SyncItemMap::const_iterator iter = sync_items_.find(id);
150 if (iter != sync_items_.end()) 178 if (iter != sync_items_.end())
151 return iter->second; 179 return iter->second;
152 return NULL; 180 return NULL;
153 } 181 }
154 182
155 void AppListSyncableService::AddExtensionAppItem(ExtensionAppItem* item) { 183 void AppListSyncableService::AddItem(AppListItemModel* item) {
156 SyncItem* sync_item = AddItem( 184 const std::string& item_id = item->id();
157 sync_pb::AppListSpecifics_AppListItemType_TYPE_APP, item); 185 if (item_id.empty()) {
158 if (!sync_item) 186 LOG(ERROR) << "AppListItemModel item with empty ID";
159 return; // Item already exists. 187 return;
160 sync_item->item_name = item->extension_name(); 188 }
189 sync_pb::AppListSpecifics::AppListItemType type;
190 const char* item_type = item->GetAppType();
191 if (item_type == ExtensionAppItem::kAppType) {
192 type = sync_pb::AppListSpecifics_AppListItemType_TYPE_APP;
193 } else if (item_type == AppListFolderItem::kAppType) {
194 type = sync_pb::AppListSpecifics_AppListItemType_TYPE_FOLDER;
195 } else {
196 LOG(ERROR) << "Unrecognized model type: " << item_type;
197 return;
198 }
199 SyncItem* sync_item = FindSyncItem(item_id);
200 if (sync_item) {
201 // If there is an existing, non-REMOVE_DEFAULT entry, update it.
202 if (sync_item->item_type !=
203 sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP) {
204 DCHECK_EQ(sync_item->item_type, type);
205 DVLOG(2) << this << ": AddItem already exists: " << sync_item->ToString();
206 UpdateItem(item);
207 return;
208 }
209 // If there is an existing REMOVE_DEFAULT entry, uninstall the app instead.
210 if (type == sync_pb::AppListSpecifics_AppListItemType_TYPE_APP &&
211 AppIsDefault(extension_service_, item_id)) {
212 DVLOG(1) << this << ": AddItem: Uninstall: " << sync_item->ToString();
213 UninstallExtension(extension_service_, item_id);
214 return;
215 }
216 // Otherwise, delete the REMOVE_DEFAULT entry.
Nicolas Zea 2013/12/20 20:44:30 I'm not sure I understand the purpose of this. If
stevenjb 2013/12/20 21:05:56 I updated this comment and the comment above to cl
217 if (SyncStarted()) {
218 DVLOG(2) << this << " -> SYNC DELETE: " << sync_item->ToString();
219 SyncChange sync_change(FROM_HERE, SyncChange::ACTION_DELETE,
220 GetSyncDataFromSyncItem(sync_item));
221 sync_processor_->ProcessSyncChanges(
222 FROM_HERE, syncer::SyncChangeList(1, sync_change));
223 }
224 delete sync_item;
225 sync_items_.erase(item_id);
Nicolas Zea 2013/12/20 20:44:30 should you return after this?
stevenjb 2013/12/20 21:05:56 No, now we add the app as usual. Comment added.
226 }
227
228 sync_item = CreateSyncItem(item_id, type);
229 UpdateSyncItemFromAppItem(item, sync_item);
230 model_->item_list()->AddItem(item);
231 DVLOG(1) << this << ": AddItem: " << sync_item->ToString()
232 << " Default: " << AppIsDefault(extension_service_, item->id());
161 SendSyncChange(sync_item, SyncChange::ACTION_ADD); 233 SendSyncChange(sync_item, SyncChange::ACTION_ADD);
162 } 234 }
163 235
164 void AppListSyncableService::UpdateExtensionAppItem(ExtensionAppItem* item) { 236 void AppListSyncableService::UpdateItem(AppListItemModel* item) {
165 SyncItem* sync_item = FindSyncItem(item->id()); 237 SyncItem* sync_item = FindSyncItem(item->id());
166 if (!sync_item) { 238 if (!sync_item) {
167 LOG(ERROR) << "UpdateExtensionAppItem: no sync item: " << item->id(); 239 LOG(ERROR) << "UpdateItem: no sync item: " << item->id();
168 return; 240 return;
169 } 241 }
170 bool changed = UpdateSyncItemFromAppItem(item, sync_item); 242 bool changed = UpdateSyncItemFromAppItem(item, sync_item);
171 if (sync_item->item_name != item->extension_name()) {
172 sync_item->item_name = item->extension_name();
173 changed = true;
174 }
175 if (!changed) { 243 if (!changed) {
176 DVLOG(2) << this << " - Update: SYNC NO CHANGE: " << sync_item->ToString(); 244 DVLOG(2) << this << " - Update: SYNC NO CHANGE: " << sync_item->ToString();
177 return; 245 return;
178 } 246 }
179 SendSyncChange(sync_item, SyncChange::ACTION_UPDATE); 247 SendSyncChange(sync_item, SyncChange::ACTION_UPDATE);
180 } 248 }
181 249
182 void AppListSyncableService::RemoveItem(const std::string& id) { 250 void AppListSyncableService::RemoveItem(const std::string& id) {
183 DVLOG(2) << this << ": RemoveItem: " << id.substr(0, 8); 251 DVLOG(2) << this << ": RemoveItem: " << id.substr(0, 8);
184 SyncItemMap::iterator iter = sync_items_.find(id); 252 SyncItemMap::iterator iter = sync_items_.find(id);
185 if (iter == sync_items_.end()) 253 if (iter == sync_items_.end()) {
254 DVLOG(2) << this << " : No Sync Item.";
186 return; 255 return;
256 }
257 // Always delete the item from the model.
258 model_->item_list()->DeleteItem(id);
259
260 // Check for existing RemoveDefault sync item.
187 SyncItem* sync_item = iter->second; 261 SyncItem* sync_item = iter->second;
262 if (sync_item->item_type ==
263 sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP) {
264 // RemoveDefault item exists, just return.
265 DVLOG(2) << this << " : RemoveDefault Item exists.";
266 return;
267 }
268
269 // Existing entry is a normal entry, send a Delete sync change and remove
270 // the entry.
188 if (SyncStarted()) { 271 if (SyncStarted()) {
189 DVLOG(2) << this << " -> SYNC DELETE: " << sync_item->ToString(); 272 DVLOG(2) << this << " -> SYNC DELETE: " << sync_item->ToString();
190 SyncChange sync_change(FROM_HERE, SyncChange::ACTION_DELETE, 273 SyncChange sync_change(FROM_HERE, SyncChange::ACTION_DELETE,
191 GetSyncDataFromSyncItem(sync_item)); 274 GetSyncDataFromSyncItem(sync_item));
192 sync_processor_->ProcessSyncChanges( 275 sync_processor_->ProcessSyncChanges(
193 FROM_HERE, syncer::SyncChangeList(1, sync_change)); 276 FROM_HERE, syncer::SyncChangeList(1, sync_change));
Nicolas Zea 2013/12/20 20:44:30 Are you creating two incompatible changes for the
stevenjb 2013/12/20 21:05:56 If the app is a Default app, we will DELETE any AP
Nicolas Zea 2013/12/30 19:49:00 It should theoretically work, but it's a bit confu
stevenjb 2014/01/02 21:15:48 Fair enough. Done.
194 } 277 }
195 delete sync_item; 278 delete sync_item;
196 sync_items_.erase(iter); 279 sync_items_.erase(iter);
197 model_->item_list()->DeleteItem(id); 280
281 if (!AppIsDefault(extension_service_, id))
282 return;
283 // This is a Default app; create and send a REMOVE_DEFAULT sync change.
284 // Note: we add the REMOVE_DEFAULT entry after we delete the existing entry.
285 sync_item = CreateSyncItem(
286 id, sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP);
287 DVLOG(1) << this << ": RemoveDefaultItem: " << sync_item->ToString();
288 SendSyncChange(sync_item, SyncChange::ACTION_ADD);
198 } 289 }
199 290
200 // AppListSyncableService syncer::SyncableService 291 // AppListSyncableService syncer::SyncableService
201 292
202 syncer::SyncMergeResult AppListSyncableService::MergeDataAndStartSyncing( 293 syncer::SyncMergeResult AppListSyncableService::MergeDataAndStartSyncing(
203 syncer::ModelType type, 294 syncer::ModelType type,
204 const syncer::SyncDataList& initial_sync_data, 295 const syncer::SyncDataList& initial_sync_data,
205 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, 296 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
206 scoped_ptr<syncer::SyncErrorFactory> error_handler) { 297 scoped_ptr<syncer::SyncErrorFactory> error_handler) {
207 DCHECK(!sync_processor_.get()); 298 DCHECK(!sync_processor_.get());
(...skipping 13 matching lines...) Expand all
221 for (SyncItemMap::const_iterator iter = sync_items_.begin(); 312 for (SyncItemMap::const_iterator iter = sync_items_.begin();
222 iter != sync_items_.end(); ++iter) { 313 iter != sync_items_.end(); ++iter) {
223 unsynced_items.insert(iter->first); 314 unsynced_items.insert(iter->first);
224 } 315 }
225 316
226 // Create SyncItem entries for initial_sync_data. 317 // Create SyncItem entries for initial_sync_data.
227 size_t new_items = 0, updated_items = 0; 318 size_t new_items = 0, updated_items = 0;
228 for (syncer::SyncDataList::const_iterator iter = initial_sync_data.begin(); 319 for (syncer::SyncDataList::const_iterator iter = initial_sync_data.begin();
229 iter != initial_sync_data.end(); ++iter) { 320 iter != initial_sync_data.end(); ++iter) {
230 const syncer::SyncData& data = *iter; 321 const syncer::SyncData& data = *iter;
322 DVLOG(2) << this << " Initial Sync Item: "
323 << data.GetSpecifics().app_list().item_id()
324 << " Type: " << data.GetSpecifics().app_list().item_type();
231 DCHECK_EQ(syncer::APP_LIST, data.GetDataType()); 325 DCHECK_EQ(syncer::APP_LIST, data.GetDataType());
232 if (CreateOrUpdateSyncItem(data.GetSpecifics().app_list())) 326 if (ProcessSyncItem(data.GetSpecifics().app_list()))
233 ++new_items; 327 ++new_items;
234 else 328 else
235 ++updated_items; 329 ++updated_items;
236 unsynced_items.erase(data.GetSpecifics().app_list().item_id()); 330 unsynced_items.erase(data.GetSpecifics().app_list().item_id());
237 } 331 }
238 332
239 result.set_num_items_after_association(sync_items_.size()); 333 result.set_num_items_after_association(sync_items_.size());
240 result.set_num_items_added(new_items); 334 result.set_num_items_added(new_items);
241 result.set_num_items_deleted(0); 335 result.set_num_items_deleted(0);
242 result.set_num_items_modified(updated_items); 336 result.set_num_items_modified(updated_items);
(...skipping 16 matching lines...) Expand all
259 DCHECK_EQ(type, syncer::APP_LIST); 353 DCHECK_EQ(type, syncer::APP_LIST);
260 354
261 sync_processor_.reset(); 355 sync_processor_.reset();
262 sync_error_handler_.reset(); 356 sync_error_handler_.reset();
263 } 357 }
264 358
265 syncer::SyncDataList AppListSyncableService::GetAllSyncData( 359 syncer::SyncDataList AppListSyncableService::GetAllSyncData(
266 syncer::ModelType type) const { 360 syncer::ModelType type) const {
267 DCHECK_EQ(syncer::APP_LIST, type); 361 DCHECK_EQ(syncer::APP_LIST, type);
268 362
269 DVLOG(1) << this << "GetAllSyncData: " << sync_items_.size(); 363 DVLOG(1) << this << ": GetAllSyncData: " << sync_items_.size();
270 syncer::SyncDataList list; 364 syncer::SyncDataList list;
271 for (SyncItemMap::const_iterator iter = sync_items_.begin(); 365 for (SyncItemMap::const_iterator iter = sync_items_.begin();
272 iter != sync_items_.end(); ++iter) { 366 iter != sync_items_.end(); ++iter) {
273 DVLOG(2) << this << " -> SYNC: " << iter->second->ToString(); 367 DVLOG(2) << this << " -> SYNC: " << iter->second->ToString();
274 list.push_back(GetSyncDataFromSyncItem(iter->second)); 368 list.push_back(GetSyncDataFromSyncItem(iter->second));
275 } 369 }
276 return list; 370 return list;
277 } 371 }
278 372
279 syncer::SyncError AppListSyncableService::ProcessSyncChanges( 373 syncer::SyncError AppListSyncableService::ProcessSyncChanges(
280 const tracked_objects::Location& from_here, 374 const tracked_objects::Location& from_here,
281 const syncer::SyncChangeList& change_list) { 375 const syncer::SyncChangeList& change_list) {
282 if (!sync_processor_.get()) { 376 if (!sync_processor_.get()) {
283 return syncer::SyncError(FROM_HERE, 377 return syncer::SyncError(FROM_HERE,
284 syncer::SyncError::DATATYPE_ERROR, 378 syncer::SyncError::DATATYPE_ERROR,
285 "App List syncable service is not started.", 379 "App List syncable service is not started.",
286 syncer::APP_LIST); 380 syncer::APP_LIST);
287 } 381 }
288 382
289 // Process incoming changes first.
290 DVLOG(1) << this << ": ProcessSyncChanges: " << change_list.size(); 383 DVLOG(1) << this << ": ProcessSyncChanges: " << change_list.size();
291 for (syncer::SyncChangeList::const_iterator iter = change_list.begin(); 384 for (syncer::SyncChangeList::const_iterator iter = change_list.begin();
292 iter != change_list.end(); ++iter) { 385 iter != change_list.end(); ++iter) {
293 const SyncChange& change = *iter; 386 const SyncChange& change = *iter;
387 DVLOG(2) << this << " Change: "
388 << change.sync_data().GetSpecifics().app_list().item_id()
389 << " (" << change.change_type() << ")";
294 if (change.change_type() == SyncChange::ACTION_ADD || 390 if (change.change_type() == SyncChange::ACTION_ADD ||
295 change.change_type() == SyncChange::ACTION_UPDATE) { 391 change.change_type() == SyncChange::ACTION_UPDATE) {
296 CreateOrUpdateSyncItem(change.sync_data().GetSpecifics().app_list()); 392 ProcessSyncItem(change.sync_data().GetSpecifics().app_list());
297 } else if (change.change_type() == SyncChange::ACTION_DELETE) { 393 } else if (change.change_type() == SyncChange::ACTION_DELETE) {
298 DeleteSyncItem(change.sync_data().GetSpecifics().app_list()); 394 DeleteSyncItem(change.sync_data().GetSpecifics().app_list());
299 } else { 395 } else {
300 LOG(ERROR) << "Invalid sync change"; 396 LOG(ERROR) << "Invalid sync change";
301 } 397 }
302 } 398 }
303 return syncer::SyncError(); 399 return syncer::SyncError();
304 } 400 }
305 401
306 // AppListSyncableService private 402 // AppListSyncableService private
307 403
308 void AppListSyncableService::CreateAppItemFromSyncItem(SyncItem* sync_item) { 404 bool AppListSyncableService::ProcessSyncItem(
309 if (sync_item->item_type == sync_pb::AppListSpecifics::TYPE_APP) { 405 const sync_pb::AppListSpecifics& specifics) {
310 std::string extension_id = sync_item->item_id; 406 const std::string& item_id = specifics.item_id();
311 const ExtensionService* extension_service = 407 if (item_id.empty()) {
312 extensions::ExtensionSystem::Get(profile_)->extension_service(); 408 LOG(ERROR) << "AppList item with empty ID";
313 const extensions::Extension* app = 409 return false;
314 extension_service->GetInstalledExtension(extension_id);
315 DVLOG_IF(1, !app) << this << "No App for ID: " << extension_id;
316 bool is_platform_app = app ? app->is_platform_app() : false;
317 ExtensionAppItem* app_item = new ExtensionAppItem(
318 profile_,
319 sync_item,
320 extension_id,
321 sync_item->item_name,
322 gfx::ImageSkia(),
323 is_platform_app);
324 model_->item_list()->AddItem(app_item);
325 return;
326 } 410 }
327 if (sync_item->item_type == 411 SyncItem* sync_item = FindSyncItem(item_id);
412 if (sync_item) {
413 // If an item of the same type exists, update it.
414 if (sync_item->item_type == specifics.item_type()) {
415 UpdateSyncItemFromSync(specifics, sync_item);
416 ProcessExistingSyncItem(sync_item);
417 DVLOG(2) << this << " <- SYNC UPDATE: " << sync_item->ToString();
418 return false;
419 }
420 // Otherwise, one of the entries should be TYPE_REMOVE_DEFAULT_APP.
421 if (sync_item->item_type !=
422 sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP &&
423 specifics.item_type() !=
424 sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP) {
425 LOG(ERROR) << "Synced item type: " << specifics.item_type()
426 << " != existing sync item type: " << sync_item->item_type
427 << " Deleting item from model!";
428 model_->item_list()->DeleteItem(item_id);
429 }
430 DVLOG(2) << this << " - ProcessSyncItem: Delete existing entry: "
431 << sync_item->ToString();
432 delete sync_item;
433 sync_items_.erase(item_id);
434 }
435
436 sync_item = CreateSyncItem(item_id, specifics.item_type());
437 UpdateSyncItemFromSync(specifics, sync_item);
438 ProcessNewSyncItem(sync_item);
439 DVLOG(2) << this << " <- SYNC ADD: " << sync_item->ToString();
440 return true;
441 }
442
443 void AppListSyncableService::ProcessNewSyncItem(SyncItem* sync_item) {
444 switch (sync_item->item_type) {
445 case sync_pb::AppListSpecifics::TYPE_APP: {
446 std::string extension_id = sync_item->item_id;
447 bool is_platform_app = AppIsPlatformApp(extension_service_, extension_id);
448 ExtensionAppItem* app_item = new ExtensionAppItem(
449 profile_,
450 sync_item,
451 extension_id,
452 sync_item->item_name,
453 gfx::ImageSkia(),
454 is_platform_app);
455 model_->item_list()->AddItem(app_item);
456 return;
457 }
458 case sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP: {
459 DVLOG(1) << this << ": Uninstall: " << sync_item->ToString();
460 UninstallExtension(extension_service_, sync_item->item_id);
461 return;
462 }
463 case sync_pb::AppListSpecifics::TYPE_FOLDER: {
464 // TODO(stevenjb): Implement
465 LOG(WARNING) << "TYPE_FOLDER not supported";
466 return;
467 }
468 case sync_pb::AppListSpecifics::TYPE_URL: {
469 // TODO(stevenjb): Implement
470 LOG(WARNING) << "TYPE_URL not supported";
471 return;
472 }
473 }
474 NOTREACHED() << "Unrecoginized sync item type: " << sync_item->ToString();
475 }
476
477 void AppListSyncableService::ProcessExistingSyncItem(SyncItem* sync_item) {
478 if (sync_item->item_type !=
328 sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP) { 479 sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP) {
329 // TODO(stevenjb): Implement 480 AppListItemModel* item =
481 model_->item_list()->FindItem(sync_item->item_id);
482 if (item && !item->position().Equals(sync_item->item_ordinal))
483 model_->item_list()->SetItemPosition(item, sync_item->item_ordinal);
330 } 484 }
331 if (sync_item->item_type == sync_pb::AppListSpecifics::TYPE_FOLDER) {
332 // TODO(stevenjb): Implement
333 }
334 if (sync_item->item_type == sync_pb::AppListSpecifics::TYPE_URL) {
335 // TODO(stevenjb): Implement
336 }
337 LOG(ERROR) << "Unsupported type: " << sync_item->item_type;
338 } 485 }
339 486
340 bool AppListSyncableService::SyncStarted() { 487 bool AppListSyncableService::SyncStarted() {
341 if (sync_processor_.get()) 488 if (sync_processor_.get())
342 return true; 489 return true;
343 if (flare_.is_null()) { 490 if (flare_.is_null()) {
344 DVLOG(2) << this << ": SyncStarted: Flare."; 491 DVLOG(2) << this << ": SyncStarted: Flare.";
345 flare_ = sync_start_util::GetFlareForSyncableService(profile_->GetPath()); 492 flare_ = sync_start_util::GetFlareForSyncableService(profile_->GetPath());
346 flare_.Run(syncer::APP_LIST); 493 flare_.Run(syncer::APP_LIST);
347 } 494 }
348 return false; 495 return false;
349 } 496 }
350 497
351 AppListSyncableService::SyncItem* AppListSyncableService::AddItem(
352 sync_pb::AppListSpecifics::AppListItemType type,
353 AppListItemModel* app_item) {
354 const std::string& item_id = app_item->id();
355 if (item_id.empty()) {
356 LOG(ERROR) << "AppListItemModel item with empty ID";
357 return NULL;
358 }
359 bool new_item = false;
360 SyncItem* sync_item = FindOrCreateSyncItem(item_id, type, &new_item);
361 if (!new_item) {
362 DVLOG(2) << this << ": AddItem already exists: " << sync_item->ToString();
363 return NULL; // Item already exists.
364 }
365 UpdateSyncItemFromAppItem(app_item, sync_item);
366 DVLOG(1) << this << ": AddItem: " << sync_item->ToString();
367 model_->item_list()->AddItem(app_item);
368 return sync_item;
369 }
370
371 void AppListSyncableService::SendSyncChange( 498 void AppListSyncableService::SendSyncChange(
372 SyncItem* sync_item, 499 SyncItem* sync_item,
373 SyncChange::SyncChangeType sync_change_type) { 500 SyncChange::SyncChangeType sync_change_type) {
374 if (!SyncStarted()) { 501 if (!SyncStarted()) {
375 DVLOG(2) << this << " - SendSyncChange: SYNC NOT STARTED: " 502 DVLOG(2) << this << " - SendSyncChange: SYNC NOT STARTED: "
376 << sync_item->ToString(); 503 << sync_item->ToString();
377 return; 504 return;
378 } 505 }
379 if (sync_change_type == SyncChange::ACTION_ADD) 506 if (sync_change_type == SyncChange::ACTION_ADD)
380 DVLOG(2) << this << " -> SYNC ADD: " << sync_item->ToString(); 507 DVLOG(2) << this << " -> SYNC ADD: " << sync_item->ToString();
381 else 508 else
382 DVLOG(2) << this << " -> SYNC UPDATE: " << sync_item->ToString(); 509 DVLOG(2) << this << " -> SYNC UPDATE: " << sync_item->ToString();
383 SyncChange sync_change(FROM_HERE, sync_change_type, 510 SyncChange sync_change(FROM_HERE, sync_change_type,
384 GetSyncDataFromSyncItem(sync_item)); 511 GetSyncDataFromSyncItem(sync_item));
385 sync_processor_->ProcessSyncChanges( 512 sync_processor_->ProcessSyncChanges(
386 FROM_HERE, syncer::SyncChangeList(1, sync_change)); 513 FROM_HERE, syncer::SyncChangeList(1, sync_change));
387 } 514 }
388 515
389 AppListSyncableService::SyncItem* 516 AppListSyncableService::SyncItem*
390 AppListSyncableService::FindSyncItem(const std::string& item_id) { 517 AppListSyncableService::FindSyncItem(const std::string& item_id) {
391 SyncItemMap::iterator iter = sync_items_.find(item_id); 518 SyncItemMap::iterator iter = sync_items_.find(item_id);
392 if (iter == sync_items_.end()) 519 if (iter == sync_items_.end())
393 return NULL; 520 return NULL;
394 return iter->second; 521 return iter->second;
395 } 522 }
396 523
397 AppListSyncableService::SyncItem* AppListSyncableService::FindOrCreateSyncItem( 524 AppListSyncableService::SyncItem*
525 AppListSyncableService::CreateSyncItem(
398 const std::string& item_id, 526 const std::string& item_id,
399 sync_pb::AppListSpecifics::AppListItemType type, 527 sync_pb::AppListSpecifics::AppListItemType item_type) {
400 bool* new_item) { 528 DCHECK(!ContainsKey(sync_items_, item_id));
401 SyncItem* item = FindSyncItem(item_id); 529 SyncItem* sync_item = new SyncItem(item_id, item_type);
402 if (item) { 530 sync_items_[item_id] = sync_item;
403 DCHECK(type == item->item_type); 531 return sync_item;
404 *new_item = false;
405 return item;
406 }
407
408 item = new SyncItem(item_id, type);
409 sync_items_[item_id] = item;
410 *new_item = true;
411 return item;
412 }
413
414 bool AppListSyncableService::CreateOrUpdateSyncItem(
415 const sync_pb::AppListSpecifics& specifics) {
416 const std::string& item_id = specifics.item_id();
417 if (item_id.empty()) {
418 LOG(ERROR) << "CreateOrUpdate AppList item with empty ID";
419 return false;
420 }
421 bool new_item = false;
422 SyncItem* sync_item =
423 FindOrCreateSyncItem(item_id, specifics.item_type(), &new_item);
424 DVLOG(2) << this << "CreateOrUpdateSyncItem: " << sync_item->ToString()
425 << " New: " << new_item << " Pos: " << specifics.item_ordinal();
426 UpdateSyncItemFromSync(specifics, sync_item);
427 // Update existing item in model
428 AppListItemModel* item = model_->item_list()->FindItem(sync_item->item_id);
429 if (item && !item->position().Equals(sync_item->item_ordinal))
430 model_->item_list()->SetItemPosition(item, sync_item->item_ordinal);
431 if (new_item) {
432 CreateAppItemFromSyncItem(sync_item);
433 DVLOG(2) << this << " <- SYNC ADD: " << sync_item->ToString();
434 } else {
435 DVLOG(2) << this << " <- SYNC UPDATE: " << sync_item->ToString();
436 }
437 return new_item;
438 } 532 }
439 533
440 void AppListSyncableService::DeleteSyncItem( 534 void AppListSyncableService::DeleteSyncItem(
441 const sync_pb::AppListSpecifics& specifics) { 535 const sync_pb::AppListSpecifics& specifics) {
442 const std::string& item_id = specifics.item_id(); 536 const std::string& item_id = specifics.item_id();
443 if (item_id.empty()) { 537 if (item_id.empty()) {
444 LOG(ERROR) << "Delete AppList item with empty ID"; 538 LOG(ERROR) << "Delete AppList item with empty ID";
445 return; 539 return;
446 } 540 }
447 DVLOG(2) << this << "DeleteSyncItem: " << item_id.substr(0, 8); 541 DVLOG(2) << this << ": DeleteSyncItem: " << item_id.substr(0, 8);
448 SyncItemMap::iterator iter = sync_items_.find(item_id); 542 SyncItemMap::iterator iter = sync_items_.find(item_id);
449 if (iter == sync_items_.end()) 543 if (iter == sync_items_.end())
450 return; 544 return;
545 sync_pb::AppListSpecifics::AppListItemType item_type =
546 iter->second->item_type;
451 DVLOG(2) << this << " <- SYNC DELETE: " << iter->second->ToString(); 547 DVLOG(2) << this << " <- SYNC DELETE: " << iter->second->ToString();
452 delete iter->second; 548 delete iter->second;
453 sync_items_.erase(iter); 549 sync_items_.erase(iter);
454 model_->item_list()->DeleteItem(item_id); 550 if (item_type != sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP)
551 model_->item_list()->DeleteItem(item_id);
455 } 552 }
456 553
457 std::string AppListSyncableService::SyncItem::ToString() const { 554 std::string AppListSyncableService::SyncItem::ToString() const {
458 return item_id.substr(0, 8) + " [" + item_ordinal.ToDebugString() + "]"; 555 std::string res = item_id.substr(0, 8);
556 if (item_type == sync_pb::AppListSpecifics::TYPE_REMOVE_DEFAULT_APP) {
557 res += " { RemoveDefault }";
558 } else {
559 res += " { " + item_name + " }";
560 res += " [" + item_ordinal.ToDebugString() + "]";
561 }
562 return res;
459 } 563 }
460 564
461 } // namespace app_list 565 } // namespace app_list
OLDNEW
« no previous file with comments | « chrome/browser/ui/app_list/app_list_syncable_service.h ('k') | chrome/browser/ui/app_list/extension_app_model_builder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698