| OLD | NEW |
| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" | 47 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
| 48 #include "chrome/browser/ui/app_list/arc/arc_app_model_builder.h" | 48 #include "chrome/browser/ui/app_list/arc/arc_app_model_builder.h" |
| 49 #endif | 49 #endif |
| 50 | 50 |
| 51 using syncer::SyncChange; | 51 using syncer::SyncChange; |
| 52 | 52 |
| 53 namespace app_list { | 53 namespace app_list { |
| 54 | 54 |
| 55 namespace { | 55 namespace { |
| 56 | 56 |
| 57 const char kOemFolderId[] = "ddb1da55-d478-4243-8642-56d3041f0263"; | |
| 58 | |
| 59 const char kNameKey[] = "name"; | 57 const char kNameKey[] = "name"; |
| 60 const char kParentIdKey[] = "parent_id"; | 58 const char kParentIdKey[] = "parent_id"; |
| 61 const char kPositionKey[] = "position"; | 59 const char kPositionKey[] = "position"; |
| 62 const char kPinPositionKey[] = "pin_position"; | 60 const char kPinPositionKey[] = "pin_position"; |
| 63 const char kTypeKey[] = "type"; | 61 const char kTypeKey[] = "type"; |
| 64 | 62 |
| 65 // Prefix for a sync id of a Drive app. Drive app ids are in a different | 63 // Prefix for a sync id of a Drive app. Drive app ids are in a different |
| 66 // format and have to be used because a Drive app could have only an URL | 64 // format and have to be used because a Drive app could have only an URL |
| 67 // without a matching Chrome app. To differentiate the Drive app id from | 65 // without a matching Chrome app. To differentiate the Drive app id from |
| 68 // Chrome app ids, this prefix will be added to create the sync item id | 66 // Chrome app ids, this prefix will be added to create the sync item id |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 | 283 |
| 286 AppListSyncableService* owner_; | 284 AppListSyncableService* owner_; |
| 287 AppListItem* adding_item_; // Unowned pointer to item being added. | 285 AppListItem* adding_item_; // Unowned pointer to item being added. |
| 288 | 286 |
| 289 DISALLOW_COPY_AND_ASSIGN(ModelObserver); | 287 DISALLOW_COPY_AND_ASSIGN(ModelObserver); |
| 290 }; | 288 }; |
| 291 | 289 |
| 292 // AppListSyncableService | 290 // AppListSyncableService |
| 293 | 291 |
| 294 // static | 292 // static |
| 293 const char AppListSyncableService::kOemFolderId[] = |
| 294 "ddb1da55-d478-4243-8642-56d3041f0263"; |
| 295 |
| 296 // static |
| 295 void AppListSyncableService::RegisterProfilePrefs( | 297 void AppListSyncableService::RegisterProfilePrefs( |
| 296 user_prefs::PrefRegistrySyncable* registry) { | 298 user_prefs::PrefRegistrySyncable* registry) { |
| 297 registry->RegisterDictionaryPref(prefs::kAppListLocalState); | 299 registry->RegisterDictionaryPref(prefs::kAppListLocalState); |
| 298 } | 300 } |
| 299 | 301 |
| 300 AppListSyncableService::AppListSyncableService( | 302 AppListSyncableService::AppListSyncableService( |
| 301 Profile* profile, | 303 Profile* profile, |
| 302 extensions::ExtensionSystem* extension_system) | 304 extensions::ExtensionSystem* extension_system) |
| 303 : profile_(profile), | 305 : profile_(profile), |
| 304 extension_system_(extension_system), | 306 extension_system_(extension_system), |
| (...skipping 834 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1139 GetDriveAppIdFromSyncId(item_id)); | 1141 GetDriveAppIdFromSyncId(item_id)); |
| 1140 } | 1142 } |
| 1141 } | 1143 } |
| 1142 } | 1144 } |
| 1143 | 1145 |
| 1144 std::string AppListSyncableService::FindOrCreateOemFolder() { | 1146 std::string AppListSyncableService::FindOrCreateOemFolder() { |
| 1145 AppListFolderItem* oem_folder = model_->FindFolderItem(kOemFolderId); | 1147 AppListFolderItem* oem_folder = model_->FindFolderItem(kOemFolderId); |
| 1146 if (!oem_folder) { | 1148 if (!oem_folder) { |
| 1147 std::unique_ptr<AppListFolderItem> new_folder(new AppListFolderItem( | 1149 std::unique_ptr<AppListFolderItem> new_folder(new AppListFolderItem( |
| 1148 kOemFolderId, AppListFolderItem::FOLDER_TYPE_OEM)); | 1150 kOemFolderId, AppListFolderItem::FOLDER_TYPE_OEM)); |
| 1149 oem_folder = | |
| 1150 static_cast<AppListFolderItem*>(model_->AddItem(std::move(new_folder))); | |
| 1151 SyncItem* oem_sync_item = FindSyncItem(kOemFolderId); | 1151 SyncItem* oem_sync_item = FindSyncItem(kOemFolderId); |
| 1152 syncer::StringOrdinal oem_position; |
| 1152 if (oem_sync_item) { | 1153 if (oem_sync_item) { |
| 1154 DCHECK(oem_sync_item->item_ordinal.IsValid()); |
| 1153 VLOG(1) << "Creating OEM folder from existing sync item: " | 1155 VLOG(1) << "Creating OEM folder from existing sync item: " |
| 1154 << oem_sync_item->item_ordinal.ToDebugString(); | 1156 << oem_sync_item->item_ordinal.ToDebugString(); |
| 1155 model_->SetItemPosition(oem_folder, oem_sync_item->item_ordinal); | 1157 oem_position = oem_sync_item->item_ordinal; |
| 1156 } else { | 1158 } else { |
| 1157 model_->SetItemPosition(oem_folder, GetOemFolderPos()); | 1159 oem_position = GetOemFolderPos(); |
| 1158 // Do not create a sync item for the OEM folder here, do it in | 1160 // Do not create a sync item for the OEM folder here, do it in |
| 1159 // ResolveFolderPositions() when the item position is finalized. | 1161 // ResolveFolderPositions() when the item position is finalized. |
| 1160 } | 1162 } |
| 1163 oem_folder = |
| 1164 static_cast<AppListFolderItem*>(model_->AddItem(std::move(new_folder))); |
| 1165 model_->SetItemPosition(oem_folder, oem_position); |
| 1161 } | 1166 } |
| 1162 model_->SetItemName(oem_folder, oem_folder_name_); | 1167 model_->SetItemName(oem_folder, oem_folder_name_); |
| 1163 return oem_folder->id(); | 1168 return oem_folder->id(); |
| 1164 } | 1169 } |
| 1165 | 1170 |
| 1166 syncer::StringOrdinal AppListSyncableService::GetOemFolderPos() { | 1171 syncer::StringOrdinal AppListSyncableService::GetOemFolderPos() { |
| 1167 VLOG(1) << "GetOemFolderPos: " << first_app_list_sync_; | 1172 VLOG(1) << "GetOemFolderPos: " << first_app_list_sync_; |
| 1168 if (!first_app_list_sync_) { | 1173 if (!first_app_list_sync_) { |
| 1169 VLOG(1) << "Sync items exist, placing OEM folder at end."; | 1174 VLOG(1) << "Sync items exist, placing OEM folder at end."; |
| 1170 syncer::StringOrdinal last; | 1175 syncer::StringOrdinal last; |
| 1171 for (const auto& sync_pair : sync_items_) { | 1176 for (const auto& sync_pair : sync_items_) { |
| 1172 SyncItem* sync_item = sync_pair.second.get(); | 1177 SyncItem* sync_item = sync_pair.second.get(); |
| 1173 if (sync_item->item_ordinal.IsValid() && | 1178 if (sync_item->item_ordinal.IsValid() && |
| 1174 (!last.IsValid() || sync_item->item_ordinal.GreaterThan(last))) { | 1179 (!last.IsValid() || sync_item->item_ordinal.GreaterThan(last))) { |
| 1175 last = sync_item->item_ordinal; | 1180 last = sync_item->item_ordinal; |
| 1176 } | 1181 } |
| 1177 } | 1182 } |
| 1178 if (last.IsValid()) | 1183 if (last.IsValid()) |
| 1179 return last.CreateAfter(); | 1184 return last.CreateAfter(); |
| 1180 } | 1185 } |
| 1181 | 1186 |
| 1182 // Place the OEM folder just after the web store, which should always be | 1187 // Place the OEM folder just after the web store, which should always be |
| 1183 // followed by a pre-installed app (e.g. Search), so the poosition should be | 1188 // followed by a pre-installed app (e.g. Search), so the poosition should be |
| 1184 // stable. TODO(stevenjb): consider explicitly setting the OEM folder location | 1189 // stable. TODO(stevenjb): consider explicitly setting the OEM folder location |
| 1185 // along with the name in ServicesCustomizationDocument::SetOemFolderName(). | 1190 // along with the name in ServicesCustomizationDocument::SetOemFolderName(). |
| 1186 AppListItemList* item_list = model_->top_level_item_list(); | 1191 AppListItemList* item_list = model_->top_level_item_list(); |
| 1187 if (item_list->item_count() == 0) | 1192 if (!item_list->item_count()) { |
| 1188 return syncer::StringOrdinal(); | 1193 LOG(ERROR) << "No top level item was found. " |
| 1194 << "Placing OEM folder at the beginning."; |
| 1195 return syncer::StringOrdinal::CreateInitialOrdinal(); |
| 1196 } |
| 1189 | 1197 |
| 1190 size_t oem_index = 0; | 1198 size_t web_store_app_index; |
| 1191 for (; oem_index < item_list->item_count() - 1; ++oem_index) { | 1199 if (!item_list->FindItemIndex(extensions::kWebStoreAppId, |
| 1192 AppListItem* cur_item = item_list->item_at(oem_index); | 1200 &web_store_app_index)) { |
| 1193 if (cur_item->id() == extensions::kWebStoreAppId) | 1201 LOG(ERROR) << "Web store position is not found it top items. " |
| 1194 break; | 1202 << "Placing OEM folder at the end."; |
| 1203 return item_list->item_at(item_list->item_count() - 1) |
| 1204 ->position() |
| 1205 .CreateAfter(); |
| 1195 } | 1206 } |
| 1196 syncer::StringOrdinal oem_ordinal; | 1207 |
| 1197 AppListItem* prev = item_list->item_at(oem_index); | 1208 // Skip items with the same position. |
| 1198 if (oem_index + 1 < item_list->item_count()) { | 1209 const AppListItem* web_store_app_item = |
| 1199 AppListItem* next = item_list->item_at(oem_index + 1); | 1210 item_list->item_at(web_store_app_index); |
| 1200 oem_ordinal = prev->position().CreateBetween(next->position()); | 1211 for (size_t j = web_store_app_index + 1; j < item_list->item_count(); ++j) { |
| 1201 } else { | 1212 const AppListItem* next_item = item_list->item_at(j); |
| 1202 oem_ordinal = prev->position().CreateAfter(); | 1213 DCHECK(next_item->position().IsValid()); |
| 1214 if (!next_item->position().Equals(web_store_app_item->position())) { |
| 1215 const syncer::StringOrdinal oem_ordinal = |
| 1216 web_store_app_item->position().CreateBetween(next_item->position()); |
| 1217 VLOG(1) << "Placing OEM Folder at: " << j |
| 1218 << " position: " << oem_ordinal.ToDebugString(); |
| 1219 return oem_ordinal; |
| 1220 } |
| 1203 } | 1221 } |
| 1204 VLOG(1) << "Placing OEM Folder at: " << oem_index | 1222 |
| 1223 const syncer::StringOrdinal oem_ordinal = |
| 1224 web_store_app_item->position().CreateAfter(); |
| 1225 VLOG(1) << "Placing OEM Folder at: " << item_list->item_count() |
| 1205 << " position: " << oem_ordinal.ToDebugString(); | 1226 << " position: " << oem_ordinal.ToDebugString(); |
| 1206 return oem_ordinal; | 1227 return oem_ordinal; |
| 1207 } | 1228 } |
| 1208 | 1229 |
| 1209 bool AppListSyncableService::AppIsOem(const std::string& id) { | 1230 bool AppListSyncableService::AppIsOem(const std::string& id) { |
| 1210 #if defined(OS_CHROMEOS) | 1231 #if defined(OS_CHROMEOS) |
| 1211 const ArcAppListPrefs* arc_prefs = ArcAppListPrefs::Get(profile_); | 1232 const ArcAppListPrefs* arc_prefs = ArcAppListPrefs::Get(profile_); |
| 1212 if (arc_prefs && arc_prefs->IsOem(id)) | 1233 if (arc_prefs && arc_prefs->IsOem(id)) |
| 1213 return true; | 1234 return true; |
| 1214 #endif | 1235 #endif |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1228 res += " { " + item_name + " }"; | 1249 res += " { " + item_name + " }"; |
| 1229 res += " [" + item_ordinal.ToDebugString() + "]"; | 1250 res += " [" + item_ordinal.ToDebugString() + "]"; |
| 1230 if (!parent_id.empty()) | 1251 if (!parent_id.empty()) |
| 1231 res += " <" + parent_id.substr(0, 8) + ">"; | 1252 res += " <" + parent_id.substr(0, 8) + ">"; |
| 1232 res += " [" + item_pin_ordinal.ToDebugString() + "]"; | 1253 res += " [" + item_pin_ordinal.ToDebugString() + "]"; |
| 1233 } | 1254 } |
| 1234 return res; | 1255 return res; |
| 1235 } | 1256 } |
| 1236 | 1257 |
| 1237 } // namespace app_list | 1258 } // namespace app_list |
| OLD | NEW |