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

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

Powered by Google App Engine
This is Rietveld 408576698