1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152

ash / public / cpp / shelf_types.cc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/public/cpp/shelf_types.h"

#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/string_split.h"

namespace ash {

namespace {

// A delimiter used to serialize the ShelfID string pair as a single string.
constexpr char kDelimiter[] = "|";

}  // namespace

std::ostream& operator<<(std::ostream& out, ShelfAlignment alignment) {
  switch (alignment) {
    case ShelfAlignment::kBottom:
      return out << "Bottom";
    case ShelfAlignment::kLeft:
      return out << "Left";
    case ShelfAlignment::kRight:
      return out << "Right";
    case ShelfAlignment::kBottomLocked:
      return out << "BottomLocked";
  }
}

std::ostream& operator<<(std::ostream& out, ShelfAutoHideState state) {
  switch (state) {
    case SHELF_AUTO_HIDE_SHOWN:
      return out << "SHOWN";
    case SHELF_AUTO_HIDE_HIDDEN:
      return out << "HIDDEN";
  }
}

std::ostream& operator<<(std::ostream& out, ShelfBackgroundType type) {
  switch (type) {
    case ShelfBackgroundType::kDefaultBg:
      return out << "DefaultBg";
    case ShelfBackgroundType::kMaximized:
      return out << "Maximized";
    case ShelfBackgroundType::kHomeLauncher:
      return out << "HomeLauncher";
    case ShelfBackgroundType::kOobe:
      return out << "Oobe";
    case ShelfBackgroundType::kLogin:
      return out << "Login";
    case ShelfBackgroundType::kLoginNonBlurredWallpaper:
      return out << "LoginNonBlurredWallpaper";
    case ShelfBackgroundType::kOverview:
      return out << "Overview";
    case ShelfBackgroundType::kInApp:
      return out << "InApp";
  }
}

bool IsValidShelfItemType(int64_t type) {
  switch (type) {
    case TYPE_PINNED_APP:
    case TYPE_BROWSER_SHORTCUT:
    case TYPE_APP:
    case TYPE_UNPINNED_BROWSER_SHORTCUT:
    case TYPE_DIALOG:
    case TYPE_UNDEFINED:
      return true;
  }
  return false;
}

bool IsPinnedShelfItemType(ShelfItemType type) {
  switch (type) {
    case TYPE_PINNED_APP:
    case TYPE_BROWSER_SHORTCUT:
      return true;
    case TYPE_APP:
    case TYPE_UNPINNED_BROWSER_SHORTCUT:
    case TYPE_DIALOG:
    case TYPE_UNDEFINED:
      return false;
  }
  NOTREACHED();
}

bool SamePinState(ShelfItemType a, ShelfItemType b) {
  if (!IsPinnedShelfItemType(a) && a != TYPE_APP)
    return false;
  if (!IsPinnedShelfItemType(b) && b != TYPE_APP)
    return false;
  const bool a_unpinned = (a == TYPE_APP);
  const bool b_unpinned = (b == TYPE_APP);
  return a_unpinned == b_unpinned;
}

ShelfID::ShelfID(const std::string& app_id, const std::string& launch_id)
    : app_id(app_id), launch_id(launch_id) {
  DCHECK(launch_id.empty() || !app_id.empty()) << "launch ids require app ids.";
}

ShelfID::~ShelfID() = default;

ShelfID::ShelfID(const ShelfID& other) = default;

ShelfID::ShelfID(ShelfID&& other) = default;

ShelfID& ShelfID::operator=(const ShelfID& other) = default;

bool ShelfID::operator==(const ShelfID& other) const {
  return app_id == other.app_id && launch_id == other.launch_id;
}

bool ShelfID::operator!=(const ShelfID& other) const {
  return !(*this == other);
}

bool ShelfID::operator<(const ShelfID& other) const {
  return app_id < other.app_id ||
         (app_id == other.app_id && launch_id < other.launch_id);
}

bool ShelfID::IsNull() const {
  return app_id.empty() && launch_id.empty();
}

std::string ShelfID::Serialize() const {
  DCHECK(!base::Contains(app_id, kDelimiter)) << "Invalid ShelfID";
  DCHECK(!base::Contains(launch_id, kDelimiter)) << "Invalid ShelfID";
  return app_id + kDelimiter + launch_id;
}

// static
ShelfID ShelfID::Deserialize(const std::string* string) {
  if (!string)
    return ShelfID();
  std::vector<std::string> components = base::SplitString(
      *string, kDelimiter, base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
  DCHECK_EQ(2u, components.size()) << "ShelfID serialized incorrectly.";
  return ShelfID(components[0], components[1]);
}

std::ostream& operator<<(std::ostream& o, const ShelfID& id) {
  return o << "app_id:" << id.app_id << ", launch_id:" << id.launch_id;
}

}  // namespace ash