shelf_types.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/public/cpp/shelf_types.h"
  5. #include "base/logging.h"
  6. #include "base/notreached.h"
  7. #include "base/strings/string_split.h"
  8. namespace ash {
  9. namespace {
  10. // A delimiter used to serialize the ShelfID string pair as a single string.
  11. constexpr char kDelimiter[] = "|";
  12. } // namespace
  13. std::ostream& operator<<(std::ostream& out, ShelfAutoHideState state) {
  14. switch (state) {
  15. case SHELF_AUTO_HIDE_SHOWN:
  16. return out << "SHOWN";
  17. case SHELF_AUTO_HIDE_HIDDEN:
  18. return out << "HIDDEN";
  19. }
  20. }
  21. std::ostream& operator<<(std::ostream& out, ShelfBackgroundType type) {
  22. switch (type) {
  23. case ShelfBackgroundType::kDefaultBg:
  24. return out << "DefaultBg";
  25. case ShelfBackgroundType::kMaximized:
  26. return out << "Maximized";
  27. case ShelfBackgroundType::kAppList:
  28. return out << "AppList";
  29. case ShelfBackgroundType::kHomeLauncher:
  30. return out << "HomeLauncher";
  31. case ShelfBackgroundType::kMaximizedWithAppList:
  32. return out << "MaximizedWithAppList";
  33. case ShelfBackgroundType::kOobe:
  34. return out << "Oobe";
  35. case ShelfBackgroundType::kLogin:
  36. return out << "Login";
  37. case ShelfBackgroundType::kLoginNonBlurredWallpaper:
  38. return out << "LoginNonBlurredWallpaper";
  39. case ShelfBackgroundType::kOverview:
  40. return out << "Overview";
  41. case ShelfBackgroundType::kInApp:
  42. return out << "InApp";
  43. }
  44. }
  45. bool IsValidShelfItemType(int64_t type) {
  46. switch (type) {
  47. case TYPE_PINNED_APP:
  48. case TYPE_BROWSER_SHORTCUT:
  49. case TYPE_APP:
  50. case TYPE_UNPINNED_BROWSER_SHORTCUT:
  51. case TYPE_DIALOG:
  52. case TYPE_UNDEFINED:
  53. return true;
  54. }
  55. return false;
  56. }
  57. bool IsPinnedShelfItemType(ShelfItemType type) {
  58. switch (type) {
  59. case TYPE_PINNED_APP:
  60. case TYPE_BROWSER_SHORTCUT:
  61. return true;
  62. case TYPE_APP:
  63. case TYPE_UNPINNED_BROWSER_SHORTCUT:
  64. case TYPE_DIALOG:
  65. case TYPE_UNDEFINED:
  66. return false;
  67. }
  68. NOTREACHED();
  69. return false;
  70. }
  71. bool SamePinState(ShelfItemType a, ShelfItemType b) {
  72. if (!IsPinnedShelfItemType(a) && a != TYPE_APP)
  73. return false;
  74. if (!IsPinnedShelfItemType(b) && b != TYPE_APP)
  75. return false;
  76. const bool a_unpinned = (a == TYPE_APP);
  77. const bool b_unpinned = (b == TYPE_APP);
  78. return a_unpinned == b_unpinned;
  79. }
  80. ShelfID::ShelfID(const std::string& app_id, const std::string& launch_id)
  81. : app_id(app_id), launch_id(launch_id) {
  82. DCHECK(launch_id.empty() || !app_id.empty()) << "launch ids require app ids.";
  83. }
  84. ShelfID::~ShelfID() = default;
  85. ShelfID::ShelfID(const ShelfID& other) = default;
  86. ShelfID::ShelfID(ShelfID&& other) = default;
  87. ShelfID& ShelfID::operator=(const ShelfID& other) = default;
  88. bool ShelfID::operator==(const ShelfID& other) const {
  89. return app_id == other.app_id && launch_id == other.launch_id;
  90. }
  91. bool ShelfID::operator!=(const ShelfID& other) const {
  92. return !(*this == other);
  93. }
  94. bool ShelfID::operator<(const ShelfID& other) const {
  95. return app_id < other.app_id ||
  96. (app_id == other.app_id && launch_id < other.launch_id);
  97. }
  98. bool ShelfID::IsNull() const {
  99. return app_id.empty() && launch_id.empty();
  100. }
  101. std::string ShelfID::Serialize() const {
  102. DCHECK_EQ(std::string::npos, app_id.find(kDelimiter)) << "Invalid ShelfID";
  103. DCHECK_EQ(std::string::npos, launch_id.find(kDelimiter)) << "Invalid ShelfID";
  104. return app_id + kDelimiter + launch_id;
  105. }
  106. // static
  107. ShelfID ShelfID::Deserialize(const std::string* string) {
  108. if (!string)
  109. return ShelfID();
  110. std::vector<std::string> components = base::SplitString(
  111. *string, kDelimiter, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
  112. DCHECK_EQ(2u, components.size()) << "ShelfID serialized incorrectly.";
  113. return ShelfID(components[0], components[1]);
  114. }
  115. std::ostream& operator<<(std::ostream& o, const ShelfID& id) {
  116. return o << "app_id:" << id.app_id << ", launch_id:" << id.launch_id;
  117. }
  118. } // namespace ash