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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/services/app_service/public/cpp/instance_update.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "components/services/app_service/public/cpp/instance.h"
#include "components/services/app_service/public/cpp/macros.h"
namespace apps {
// static
void InstanceUpdate::Merge(Instance* state, const Instance* delta) {
DCHECK(state);
if (!delta) {
return;
}
if ((delta->AppId() != state->AppId()) ||
delta->InstanceId() != state->InstanceId()) {
LOG(ERROR) << "inconsistent (app_id, instance_id): (" << delta->AppId()
<< ", " << delta->InstanceId() << ") vs (" << state->AppId()
<< ", " << state->InstanceId() << ") ";
DCHECK(false);
return;
}
if (!delta->LaunchId().empty()) {
state->SetLaunchId(delta->LaunchId());
}
if (delta->State() != InstanceState::kUnknown) {
state->UpdateState(delta->State(), delta->LastUpdatedTime());
}
if (delta->BrowserContext()) {
state->SetBrowserContext(delta->BrowserContext());
}
if (delta->Window()) {
state->SetWindow(delta->Window());
}
// When adding new fields to the Instance class, this function should also be
// updated.
}
// static
bool InstanceUpdate::Equals(const Instance* state, const Instance* delta) {
if (delta == nullptr) {
return true;
}
if (state == nullptr) {
if (static_cast<InstanceState>(delta->State() &
InstanceState::kDestroyed) !=
InstanceState::kUnknown) {
return true;
}
return false;
}
if ((delta->AppId() != state->AppId()) ||
delta->InstanceId() != state->InstanceId()) {
LOG(ERROR) << "inconsistent (app_id, instance_id): (" << delta->AppId()
<< ", " << delta->InstanceId() << ") vs (" << state->AppId()
<< ", " << state->InstanceId() << ") ";
DCHECK(false);
return false;
}
if (delta->Window() && delta->Window() != state->Window()) {
return false;
}
if (!delta->LaunchId().empty() && delta->LaunchId() != state->LaunchId()) {
return false;
}
if (delta->State() != InstanceState::kUnknown &&
(delta->State() != state->State() ||
delta->LastUpdatedTime() != state->LastUpdatedTime())) {
return false;
}
if (delta->BrowserContext() &&
delta->BrowserContext() != state->BrowserContext()) {
return false;
}
return true;
// When adding new fields to the Instance class, this function should also be
// updated.
}
InstanceUpdate::InstanceUpdate(const Instance* state, const Instance* delta)
: state_(state), delta_(delta) {
DCHECK(state_ || delta_);
if (state_ && delta_) {
DCHECK(state_->AppId() == delta->AppId());
DCHECK(state_->InstanceId() == delta->InstanceId());
}
}
bool InstanceUpdate::StateIsNull() const {
return state_ == nullptr;
}
bool InstanceUpdate::IsCreation() const {
return StateIsNull() && (State() & apps::InstanceState::kDestroyed) ==
apps::InstanceState::kUnknown;
}
bool InstanceUpdate::IsDestruction() const {
return StateChanged() && State() == apps::InstanceState::kDestroyed;
}
const std::string& InstanceUpdate::AppId() const {
return delta_ ? delta_->AppId() : state_->AppId();
}
const base::UnguessableToken& InstanceUpdate::InstanceId() const {
return delta_ ? delta_->InstanceId() : state_->InstanceId();
}
aura::Window* InstanceUpdate::Window() const {
GET_VALUE(Window);
}
bool InstanceUpdate::WindowChanged() const {
IS_VALUE_CHANGED(Window);
}
const std::string& InstanceUpdate::LaunchId() const {
GET_VALUE_WITH_CHECK_AND_DEFAULT_RETURN(LaunchId(), empty,
base::EmptyString());
}
bool InstanceUpdate::LaunchIdChanged() const {
IS_VALUE_CHANGED_WITH_CHECK(LaunchId(), empty);
}
InstanceState InstanceUpdate::State() const {
GET_VALUE_WITH_DEFAULT_VALUE(State(), InstanceState::kUnknown);
}
bool InstanceUpdate::StateChanged() const {
IS_VALUE_CHANGED_WITH_DEFAULT_VALUE(State(), InstanceState::kUnknown);
}
base::Time InstanceUpdate::LastUpdatedTime() const {
GET_VALUE_WITH_CHECK_AND_DEFAULT_RETURN(LastUpdatedTime(), is_null,
base::Time());
}
bool InstanceUpdate::LastUpdatedTimeChanged() const {
IS_VALUE_CHANGED_WITH_CHECK(LastUpdatedTime(), is_null);
}
content::BrowserContext* InstanceUpdate::BrowserContext() const {
GET_VALUE(BrowserContext);
}
bool InstanceUpdate::BrowserContextChanged() const {
IS_VALUE_CHANGED(BrowserContext);
}
} // namespace apps
|