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
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168

content / browser / web_exposed_isolation_info.cc [blame]

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

#include "content/browser/web_exposed_isolation_info.h"

#include <ostream>

#include "base/notreached.h"

namespace content {

namespace {

constexpr char kComparisonErrorMessage[] =
    "You are comparing optional WebExposedIsolationInfo objects using "
    "operator==, use WebExposedIsolationInfo::AreCompatible() instead.";

}  // namespace

// static
WebExposedIsolationInfo WebExposedIsolationInfo::CreateNonIsolated() {
  return WebExposedIsolationInfo(std::nullopt /* origin */,
                                 false /* isolated_application */);
}

WebExposedIsolationInfo WebExposedIsolationInfo::CreateIsolated(
    const url::Origin& origin) {
  return WebExposedIsolationInfo(origin, false /* isolated_application */);
}

WebExposedIsolationInfo WebExposedIsolationInfo::CreateIsolatedApplication(
    const url::Origin& origin) {
  return WebExposedIsolationInfo(origin, true /* isolated_application */);
}

bool WebExposedIsolationInfo::AreCompatible(const WebExposedIsolationInfo& a,
                                            const WebExposedIsolationInfo& b) {
  return a == b;
}

bool WebExposedIsolationInfo::AreCompatible(
    const std::optional<WebExposedIsolationInfo>& a,
    const WebExposedIsolationInfo& b) {
  if (!a.has_value())
    return true;
  return AreCompatible(a.value(), b);
}

bool WebExposedIsolationInfo::AreCompatible(
    const WebExposedIsolationInfo& a,
    const std::optional<WebExposedIsolationInfo>& b) {
  return AreCompatible(b, a);
}

bool WebExposedIsolationInfo::AreCompatible(
    const std::optional<WebExposedIsolationInfo>& a,
    const std::optional<WebExposedIsolationInfo>& b) {
  if (!a.has_value() || !b.has_value())
    return true;
  return AreCompatible(a.value(), b.value());
}

WebExposedIsolationInfo::WebExposedIsolationInfo(
    const std::optional<url::Origin>& origin,
    bool isolated_application)
    : origin_(origin), isolated_application_(isolated_application) {}

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

WebExposedIsolationInfo::~WebExposedIsolationInfo() = default;

const url::Origin& WebExposedIsolationInfo::origin() const {
  DCHECK(is_isolated())
      << "The origin() getter should only be used for "
         "WebExposedIsolationInfo's where is_isolated() is true.";
  return origin_.value();
}

bool WebExposedIsolationInfo::operator==(
    const WebExposedIsolationInfo& b) const {
  if (is_isolated_application() != b.is_isolated_application())
    return false;

  if (is_isolated() != b.is_isolated())
    return false;

  if (is_isolated() && !(origin_->IsSameOriginWith(b.origin())))
    return false;

  return true;
}

bool WebExposedIsolationInfo::operator!=(
    const WebExposedIsolationInfo& b) const {
  return !(operator==(b));
}

bool WebExposedIsolationInfo::operator<(
    const WebExposedIsolationInfo& b) const {
  // Nonisolated < Isolated < Isolated Application.
  if (is_isolated_application() != b.is_isolated_application())
    return !is_isolated_application();

  if (is_isolated() != b.is_isolated())
    return !is_isolated();

  // Within Isolated and Isolated Application, compare origins:
  if (is_isolated()) {
    DCHECK(b.is_isolated());
    return origin_.value() < b.origin();
  }

  // Nonisolated == Nonisolated.
  DCHECK(!is_isolated() && !b.is_isolated());
  return false;
}

std::ostream& operator<<(std::ostream& out,
                         const WebExposedIsolationInfo& info) {
  out << "{";
  if (info.is_isolated()) {
    out << info.origin();
    if (info.is_isolated_application())
      out << " (application)";
  }
  out << "}";
  return out;
}

bool operator==(const std::optional<WebExposedIsolationInfo>& a,
                const std::optional<WebExposedIsolationInfo>& b) {
  NOTREACHED_IN_MIGRATION() << kComparisonErrorMessage;
  return false;
}

bool operator==(const WebExposedIsolationInfo& a,
                const std::optional<WebExposedIsolationInfo>& b) {
  NOTREACHED_IN_MIGRATION() << kComparisonErrorMessage;
  return false;
}

bool operator==(const std::optional<WebExposedIsolationInfo>& a,
                const WebExposedIsolationInfo& b) {
  NOTREACHED_IN_MIGRATION() << kComparisonErrorMessage;
  return false;
}

bool operator!=(const std::optional<WebExposedIsolationInfo>& a,
                const std::optional<WebExposedIsolationInfo>& b) {
  NOTREACHED_IN_MIGRATION() << kComparisonErrorMessage;
  return false;
}

bool operator!=(const WebExposedIsolationInfo& a,
                const std::optional<WebExposedIsolationInfo>& b) {
  NOTREACHED_IN_MIGRATION() << kComparisonErrorMessage;
  return false;
}

bool operator!=(const std::optional<WebExposedIsolationInfo>& a,
                const WebExposedIsolationInfo& b) {
  NOTREACHED_IN_MIGRATION() << kComparisonErrorMessage;
  return false;
}

}  // namespace content