| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/app_list/app_list_model.h" | 5 #include "ui/app_list/app_list_model.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "ui/app_list/app_list_folder_item.h" | 10 #include "ui/app_list/app_list_folder_item.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 void AppListModel::RemoveObserver(AppListModelObserver* observer) { | 35 void AppListModel::RemoveObserver(AppListModelObserver* observer) { |
| 36 observers_.RemoveObserver(observer); | 36 observers_.RemoveObserver(observer); |
| 37 } | 37 } |
| 38 | 38 |
| 39 void AppListModel::SetStatus(Status status) { | 39 void AppListModel::SetStatus(Status status) { |
| 40 if (status_ == status) | 40 if (status_ == status) |
| 41 return; | 41 return; |
| 42 | 42 |
| 43 status_ = status; | 43 status_ = status; |
| 44 FOR_EACH_OBSERVER(AppListModelObserver, | 44 for (auto& observer : observers_) |
| 45 observers_, | 45 observer.OnAppListModelStatusChanged(); |
| 46 OnAppListModelStatusChanged()); | |
| 47 } | 46 } |
| 48 | 47 |
| 49 void AppListModel::SetState(State state) { | 48 void AppListModel::SetState(State state) { |
| 50 if (state_ == state) | 49 if (state_ == state) |
| 51 return; | 50 return; |
| 52 | 51 |
| 53 State old_state = state_; | 52 State old_state = state_; |
| 54 | 53 |
| 55 state_ = state; | 54 state_ = state; |
| 56 | 55 |
| 57 FOR_EACH_OBSERVER(AppListModelObserver, | 56 for (auto& observer : observers_) |
| 58 observers_, | 57 observer.OnAppListModelStateChanged(old_state, state_); |
| 59 OnAppListModelStateChanged(old_state, state_)); | |
| 60 } | 58 } |
| 61 | 59 |
| 62 AppListItem* AppListModel::FindItem(const std::string& id) { | 60 AppListItem* AppListModel::FindItem(const std::string& id) { |
| 63 AppListItem* item = top_level_item_list_->FindItem(id); | 61 AppListItem* item = top_level_item_list_->FindItem(id); |
| 64 if (item) | 62 if (item) |
| 65 return item; | 63 return item; |
| 66 for (size_t i = 0; i < top_level_item_list_->item_count(); ++i) { | 64 for (size_t i = 0; i < top_level_item_list_->item_count(); ++i) { |
| 67 AppListItem* child_item = | 65 AppListItem* child_item = |
| 68 top_level_item_list_->item_at(i)->FindChildItem(id); | 66 top_level_item_list_->item_at(i)->FindChildItem(id); |
| 69 if (child_item) | 67 if (child_item) |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 if (!item->IsInFolder()) { | 223 if (!item->IsInFolder()) { |
| 226 top_level_item_list_->SetItemPosition(item, new_position); | 224 top_level_item_list_->SetItemPosition(item, new_position); |
| 227 // Note: this will trigger OnListItemMoved which will signal observers. | 225 // Note: this will trigger OnListItemMoved which will signal observers. |
| 228 // (This is done this way because some View code still moves items within | 226 // (This is done this way because some View code still moves items within |
| 229 // the item list directly). | 227 // the item list directly). |
| 230 return; | 228 return; |
| 231 } | 229 } |
| 232 AppListFolderItem* folder = FindFolderItem(item->folder_id()); | 230 AppListFolderItem* folder = FindFolderItem(item->folder_id()); |
| 233 DCHECK(folder); | 231 DCHECK(folder); |
| 234 folder->item_list()->SetItemPosition(item, new_position); | 232 folder->item_list()->SetItemPosition(item, new_position); |
| 235 FOR_EACH_OBSERVER(AppListModelObserver, | 233 for (auto& observer : observers_) |
| 236 observers_, | 234 observer.OnAppListItemUpdated(item); |
| 237 OnAppListItemUpdated(item)); | |
| 238 } | 235 } |
| 239 | 236 |
| 240 void AppListModel::SetItemName(AppListItem* item, const std::string& name) { | 237 void AppListModel::SetItemName(AppListItem* item, const std::string& name) { |
| 241 item->SetName(name); | 238 item->SetName(name); |
| 242 DVLOG(2) << "AppListModel::SetItemName: " << item->ToDebugString(); | 239 DVLOG(2) << "AppListModel::SetItemName: " << item->ToDebugString(); |
| 243 FOR_EACH_OBSERVER(AppListModelObserver, | 240 for (auto& observer : observers_) |
| 244 observers_, | 241 observer.OnAppListItemUpdated(item); |
| 245 OnAppListItemUpdated(item)); | |
| 246 } | 242 } |
| 247 | 243 |
| 248 void AppListModel::SetItemNameAndShortName(AppListItem* item, | 244 void AppListModel::SetItemNameAndShortName(AppListItem* item, |
| 249 const std::string& name, | 245 const std::string& name, |
| 250 const std::string& short_name) { | 246 const std::string& short_name) { |
| 251 item->SetNameAndShortName(name, short_name); | 247 item->SetNameAndShortName(name, short_name); |
| 252 DVLOG(2) << "AppListModel::SetItemNameAndShortName: " | 248 DVLOG(2) << "AppListModel::SetItemNameAndShortName: " |
| 253 << item->ToDebugString(); | 249 << item->ToDebugString(); |
| 254 FOR_EACH_OBSERVER(AppListModelObserver, | 250 for (auto& observer : observers_) |
| 255 observers_, | 251 observer.OnAppListItemUpdated(item); |
| 256 OnAppListItemUpdated(item)); | |
| 257 } | 252 } |
| 258 | 253 |
| 259 void AppListModel::DeleteItem(const std::string& id) { | 254 void AppListModel::DeleteItem(const std::string& id) { |
| 260 AppListItem* item = FindItem(id); | 255 AppListItem* item = FindItem(id); |
| 261 if (!item) | 256 if (!item) |
| 262 return; | 257 return; |
| 263 if (!item->IsInFolder()) { | 258 if (!item->IsInFolder()) { |
| 264 DCHECK_EQ(0u, item->ChildItemCount()) | 259 DCHECK_EQ(0u, item->ChildItemCount()) |
| 265 << "Invalid call to DeleteItem for item with children: " << id; | 260 << "Invalid call to DeleteItem for item with children: " << id; |
| 266 FOR_EACH_OBSERVER(AppListModelObserver, | 261 for (auto& observer : observers_) |
| 267 observers_, | 262 observer.OnAppListItemWillBeDeleted(item); |
| 268 OnAppListItemWillBeDeleted(item)); | |
| 269 top_level_item_list_->DeleteItem(id); | 263 top_level_item_list_->DeleteItem(id); |
| 270 FOR_EACH_OBSERVER(AppListModelObserver, observers_, OnAppListItemDeleted()); | 264 for (auto& observer : observers_) |
| 265 observer.OnAppListItemDeleted(); |
| 271 return; | 266 return; |
| 272 } | 267 } |
| 273 AppListFolderItem* folder = FindFolderItem(item->folder_id()); | 268 AppListFolderItem* folder = FindFolderItem(item->folder_id()); |
| 274 DCHECK(folder) << "Folder not found for item: " << item->ToDebugString(); | 269 DCHECK(folder) << "Folder not found for item: " << item->ToDebugString(); |
| 275 std::unique_ptr<AppListItem> child_item = RemoveItemFromFolder(folder, item); | 270 std::unique_ptr<AppListItem> child_item = RemoveItemFromFolder(folder, item); |
| 276 DCHECK_EQ(item, child_item.get()); | 271 DCHECK_EQ(item, child_item.get()); |
| 277 FOR_EACH_OBSERVER(AppListModelObserver, | 272 for (auto& observer : observers_) |
| 278 observers_, | 273 observer.OnAppListItemWillBeDeleted(item); |
| 279 OnAppListItemWillBeDeleted(item)); | |
| 280 child_item.reset(); // Deletes item. | 274 child_item.reset(); // Deletes item. |
| 281 FOR_EACH_OBSERVER(AppListModelObserver, observers_, OnAppListItemDeleted()); | 275 for (auto& observer : observers_) |
| 276 observer.OnAppListItemDeleted(); |
| 282 } | 277 } |
| 283 | 278 |
| 284 void AppListModel::DeleteUninstalledItem(const std::string& id) { | 279 void AppListModel::DeleteUninstalledItem(const std::string& id) { |
| 285 AppListItem* item = FindItem(id); | 280 AppListItem* item = FindItem(id); |
| 286 if (!item) | 281 if (!item) |
| 287 return; | 282 return; |
| 288 const std::string folder_id = item->folder_id(); | 283 const std::string folder_id = item->folder_id(); |
| 289 DeleteItem(id); | 284 DeleteItem(id); |
| 290 | 285 |
| 291 // crbug.com/368111: Upon uninstall of 2nd-to-last folder item, reparent last | 286 // crbug.com/368111: Upon uninstall of 2nd-to-last folder item, reparent last |
| (...skipping 30 matching lines...) Expand all Loading... |
| 322 } | 317 } |
| 323 folder_ids.push_back(folder->id()); | 318 folder_ids.push_back(folder->id()); |
| 324 } | 319 } |
| 325 // Delete folders. | 320 // Delete folders. |
| 326 for (size_t i = 0; i < folder_ids.size(); ++i) | 321 for (size_t i = 0; i < folder_ids.size(); ++i) |
| 327 DeleteItem(folder_ids[i]); | 322 DeleteItem(folder_ids[i]); |
| 328 } | 323 } |
| 329 | 324 |
| 330 void AppListModel::SetCustomLauncherPageEnabled(bool enabled) { | 325 void AppListModel::SetCustomLauncherPageEnabled(bool enabled) { |
| 331 custom_launcher_page_enabled_ = enabled; | 326 custom_launcher_page_enabled_ = enabled; |
| 332 FOR_EACH_OBSERVER(AppListModelObserver, observers_, | 327 for (auto& observer : observers_) |
| 333 OnCustomLauncherPageEnabledStateChanged(enabled)); | 328 observer.OnCustomLauncherPageEnabledStateChanged(enabled); |
| 334 } | 329 } |
| 335 | 330 |
| 336 std::vector<SearchResult*> AppListModel::FilterSearchResultsByDisplayType( | 331 std::vector<SearchResult*> AppListModel::FilterSearchResultsByDisplayType( |
| 337 SearchResults* results, | 332 SearchResults* results, |
| 338 SearchResult::DisplayType display_type, | 333 SearchResult::DisplayType display_type, |
| 339 size_t max_results) { | 334 size_t max_results) { |
| 340 std::vector<SearchResult*> matches; | 335 std::vector<SearchResult*> matches; |
| 341 for (size_t i = 0; i < results->item_count(); ++i) { | 336 for (size_t i = 0; i < results->item_count(); ++i) { |
| 342 SearchResult* item = results->GetItemAt(i); | 337 SearchResult* item = results->GetItemAt(i); |
| 343 if (item->display_type() == display_type) { | 338 if (item->display_type() == display_type) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 360 --custom_launcher_page_subpage_depth_; | 355 --custom_launcher_page_subpage_depth_; |
| 361 return true; | 356 return true; |
| 362 } | 357 } |
| 363 | 358 |
| 364 void AppListModel::ClearCustomLauncherPageSubpages() { | 359 void AppListModel::ClearCustomLauncherPageSubpages() { |
| 365 custom_launcher_page_subpage_depth_ = 0; | 360 custom_launcher_page_subpage_depth_ = 0; |
| 366 } | 361 } |
| 367 | 362 |
| 368 void AppListModel::SetSearchEngineIsGoogle(bool is_google) { | 363 void AppListModel::SetSearchEngineIsGoogle(bool is_google) { |
| 369 search_engine_is_google_ = is_google; | 364 search_engine_is_google_ = is_google; |
| 370 FOR_EACH_OBSERVER(AppListModelObserver, observers_, | 365 for (auto& observer : observers_) |
| 371 OnSearchEngineIsGoogleChanged(is_google)); | 366 observer.OnSearchEngineIsGoogleChanged(is_google); |
| 372 } | 367 } |
| 373 | 368 |
| 374 // Private methods | 369 // Private methods |
| 375 | 370 |
| 376 void AppListModel::OnListItemMoved(size_t from_index, | 371 void AppListModel::OnListItemMoved(size_t from_index, |
| 377 size_t to_index, | 372 size_t to_index, |
| 378 AppListItem* item) { | 373 AppListItem* item) { |
| 379 FOR_EACH_OBSERVER(AppListModelObserver, | 374 for (auto& observer : observers_) |
| 380 observers_, | 375 observer.OnAppListItemUpdated(item); |
| 381 OnAppListItemUpdated(item)); | |
| 382 } | 376 } |
| 383 | 377 |
| 384 AppListFolderItem* AppListModel::FindOrCreateFolderItem( | 378 AppListFolderItem* AppListModel::FindOrCreateFolderItem( |
| 385 const std::string& folder_id) { | 379 const std::string& folder_id) { |
| 386 if (folder_id.empty()) | 380 if (folder_id.empty()) |
| 387 return NULL; | 381 return NULL; |
| 388 | 382 |
| 389 AppListFolderItem* dest_folder = FindFolderItem(folder_id); | 383 AppListFolderItem* dest_folder = FindFolderItem(folder_id); |
| 390 if (dest_folder) | 384 if (dest_folder) |
| 391 return dest_folder; | 385 return dest_folder; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 402 top_level_item_list_->CreatePositionBefore(syncer::StringOrdinal())); | 396 top_level_item_list_->CreatePositionBefore(syncer::StringOrdinal())); |
| 403 AppListItem* new_folder_item = | 397 AppListItem* new_folder_item = |
| 404 AddItemToItemListAndNotify(std::move(new_folder)); | 398 AddItemToItemListAndNotify(std::move(new_folder)); |
| 405 return static_cast<AppListFolderItem*>(new_folder_item); | 399 return static_cast<AppListFolderItem*>(new_folder_item); |
| 406 } | 400 } |
| 407 | 401 |
| 408 AppListItem* AppListModel::AddItemToItemListAndNotify( | 402 AppListItem* AppListModel::AddItemToItemListAndNotify( |
| 409 std::unique_ptr<AppListItem> item_ptr) { | 403 std::unique_ptr<AppListItem> item_ptr) { |
| 410 DCHECK(!item_ptr->IsInFolder()); | 404 DCHECK(!item_ptr->IsInFolder()); |
| 411 AppListItem* item = top_level_item_list_->AddItem(std::move(item_ptr)); | 405 AppListItem* item = top_level_item_list_->AddItem(std::move(item_ptr)); |
| 412 FOR_EACH_OBSERVER(AppListModelObserver, | 406 for (auto& observer : observers_) |
| 413 observers_, | 407 observer.OnAppListItemAdded(item); |
| 414 OnAppListItemAdded(item)); | |
| 415 return item; | 408 return item; |
| 416 } | 409 } |
| 417 | 410 |
| 418 AppListItem* AppListModel::AddItemToItemListAndNotifyUpdate( | 411 AppListItem* AppListModel::AddItemToItemListAndNotifyUpdate( |
| 419 std::unique_ptr<AppListItem> item_ptr) { | 412 std::unique_ptr<AppListItem> item_ptr) { |
| 420 DCHECK(!item_ptr->IsInFolder()); | 413 DCHECK(!item_ptr->IsInFolder()); |
| 421 AppListItem* item = top_level_item_list_->AddItem(std::move(item_ptr)); | 414 AppListItem* item = top_level_item_list_->AddItem(std::move(item_ptr)); |
| 422 FOR_EACH_OBSERVER(AppListModelObserver, | 415 for (auto& observer : observers_) |
| 423 observers_, | 416 observer.OnAppListItemUpdated(item); |
| 424 OnAppListItemUpdated(item)); | |
| 425 return item; | 417 return item; |
| 426 } | 418 } |
| 427 | 419 |
| 428 AppListItem* AppListModel::AddItemToFolderItemAndNotify( | 420 AppListItem* AppListModel::AddItemToFolderItemAndNotify( |
| 429 AppListFolderItem* folder, | 421 AppListFolderItem* folder, |
| 430 std::unique_ptr<AppListItem> item_ptr) { | 422 std::unique_ptr<AppListItem> item_ptr) { |
| 431 CHECK_NE(folder->id(), item_ptr->folder_id()); | 423 CHECK_NE(folder->id(), item_ptr->folder_id()); |
| 432 AppListItem* item = folder->item_list()->AddItem(std::move(item_ptr)); | 424 AppListItem* item = folder->item_list()->AddItem(std::move(item_ptr)); |
| 433 item->set_folder_id(folder->id()); | 425 item->set_folder_id(folder->id()); |
| 434 FOR_EACH_OBSERVER(AppListModelObserver, | 426 for (auto& observer : observers_) |
| 435 observers_, | 427 observer.OnAppListItemUpdated(item); |
| 436 OnAppListItemUpdated(item)); | |
| 437 return item; | 428 return item; |
| 438 } | 429 } |
| 439 | 430 |
| 440 std::unique_ptr<AppListItem> AppListModel::RemoveItem(AppListItem* item) { | 431 std::unique_ptr<AppListItem> AppListModel::RemoveItem(AppListItem* item) { |
| 441 if (!item->IsInFolder()) | 432 if (!item->IsInFolder()) |
| 442 return top_level_item_list_->RemoveItem(item->id()); | 433 return top_level_item_list_->RemoveItem(item->id()); |
| 443 | 434 |
| 444 AppListFolderItem* folder = FindFolderItem(item->folder_id()); | 435 AppListFolderItem* folder = FindFolderItem(item->folder_id()); |
| 445 return RemoveItemFromFolder(folder, item); | 436 return RemoveItemFromFolder(folder, item); |
| 446 } | 437 } |
| 447 | 438 |
| 448 std::unique_ptr<AppListItem> AppListModel::RemoveItemFromFolder( | 439 std::unique_ptr<AppListItem> AppListModel::RemoveItemFromFolder( |
| 449 AppListFolderItem* folder, | 440 AppListFolderItem* folder, |
| 450 AppListItem* item) { | 441 AppListItem* item) { |
| 451 std::string folder_id = folder->id(); | 442 std::string folder_id = folder->id(); |
| 452 CHECK_EQ(item->folder_id(), folder_id); | 443 CHECK_EQ(item->folder_id(), folder_id); |
| 453 std::unique_ptr<AppListItem> result = | 444 std::unique_ptr<AppListItem> result = |
| 454 folder->item_list()->RemoveItem(item->id()); | 445 folder->item_list()->RemoveItem(item->id()); |
| 455 result->set_folder_id(""); | 446 result->set_folder_id(""); |
| 456 if (folder->item_list()->item_count() == 0) { | 447 if (folder->item_list()->item_count() == 0) { |
| 457 DVLOG(2) << "Deleting empty folder: " << folder->ToDebugString(); | 448 DVLOG(2) << "Deleting empty folder: " << folder->ToDebugString(); |
| 458 DeleteItem(folder_id); | 449 DeleteItem(folder_id); |
| 459 } | 450 } |
| 460 return result; | 451 return result; |
| 461 } | 452 } |
| 462 | 453 |
| 463 } // namespace app_list | 454 } // namespace app_list |
| OLD | NEW |