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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/arc/extensions/fake_arc_support.h"
#include <optional>
#include <string>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/notreached.h"
#include "base/strings/to_string.h"
#include "base/values.h"
#include "chrome/browser/ash/arc/extensions/arc_support_message_host.h"
#include "chrome/browser/profiles/profile.h"
namespace {
void SerializeAndSend(extensions::NativeMessageHost* native_message_host,
const base::Value::Dict& message) {
DCHECK(native_message_host);
std::string message_string;
if (!base::JSONWriter::Write(message, &message_string)) {
NOTREACHED();
}
native_message_host->OnMessage(message_string);
}
} // namespace
namespace arc {
FakeArcSupport::FakeArcSupport(ArcSupportHost* support_host)
: support_host_(support_host) {
DCHECK(support_host_);
support_host_->SetRequestOpenAppCallbackForTesting(base::BindRepeating(
&FakeArcSupport::Open, weak_ptr_factory_.GetWeakPtr()));
}
FakeArcSupport::~FakeArcSupport() {
// Ensure that message host is disconnected.
if (!native_message_host_)
return;
UnsetMessageHost();
}
void FakeArcSupport::Open(Profile* profile) {
DCHECK(!native_message_host_);
native_message_host_ = ArcSupportMessageHost::Create(profile);
native_message_host_->Start(this);
support_host_->SetMessageHost(
static_cast<ArcSupportMessageHost*>(native_message_host_.get()));
}
void FakeArcSupport::Close() {
DCHECK(native_message_host_);
native_message_host_->OnMessage("{\"event\": \"onWindowClosed\"}");
UnsetMessageHost();
}
void FakeArcSupport::ClickAgreeButton() {
DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::TERMS);
base::Value::Dict message;
message.Set("event", "onAgreed");
message.Set("tosContent", tos_content_);
message.Set("tosShown", tos_shown_);
message.Set("isMetricsEnabled", metrics_mode_);
message.Set("isBackupRestoreEnabled", backup_and_restore_mode_);
message.Set("isBackupRestoreManaged", backup_and_restore_managed_);
message.Set("isLocationServiceEnabled", location_service_mode_);
message.Set("isLocationServiceManaged", location_service_managed_);
SerializeAndSend(native_message_host_.get(), message);
}
void FakeArcSupport::ClickCancelButton() {
DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::TERMS);
base::Value::Dict message;
message.Set("event", "onCanceled");
message.Set("tosContent", tos_content_);
message.Set("tosShown", tos_shown_);
message.Set("isMetricsEnabled", metrics_mode_);
message.Set("isBackupRestoreEnabled", backup_and_restore_mode_);
message.Set("isBackupRestoreManaged", backup_and_restore_managed_);
message.Set("isLocationServiceEnabled", location_service_mode_);
message.Set("isLocationServiceManaged", location_service_managed_);
SerializeAndSend(native_message_host_.get(), message);
// The cancel button closes the window.
Close();
}
void FakeArcSupport::ClickRetryButton() {
DCHECK(native_message_host_);
DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR);
native_message_host_->OnMessage("{\"event\": \"onRetryClicked\"}");
}
void FakeArcSupport::ClickSendFeedbackButton() {
DCHECK(native_message_host_);
DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR);
native_message_host_->OnMessage("{\"event\": \"onSendFeedbackClicked\"}");
}
void FakeArcSupport::ClickRunNetworkTestsButton() {
DCHECK(native_message_host_);
DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR);
native_message_host_->OnMessage("{\"event\": \"onRunNetworkTestsClicked\"}");
}
void FakeArcSupport::TosLoadResult(bool success) {
DCHECK(native_message_host_);
native_message_host_->OnMessage(
base::StrCat({"{\"event\": \"onTosLoadResult\", \"success\": ",
base::ToString(success), "}"}));
}
void FakeArcSupport::AddObserver(Observer* observer) {
observer_list_.AddObserver(observer);
}
void FakeArcSupport::RemoveObserver(Observer* observer) {
observer_list_.RemoveObserver(observer);
}
bool FakeArcSupport::HasObserver(Observer* observer) {
return observer_list_.HasObserver(observer);
}
void FakeArcSupport::UnsetMessageHost() {
support_host_->UnsetMessageHost(
static_cast<ArcSupportMessageHost*>(native_message_host_.get()));
native_message_host_.reset();
}
void FakeArcSupport::PostMessageFromNativeHost(
const std::string& message_string) {
std::optional<base::Value> parsed_json =
base::JSONReader::Read(message_string);
DCHECK(parsed_json);
const base::Value::Dict& message = parsed_json->GetDict();
const std::string* action = message.FindString("action");
if (!action) {
NOTREACHED() << message_string;
}
ArcSupportHost::UIPage prev_ui_page = ui_page_;
if (*action == "initialize") {
// Do nothing as emulation.
} else if (*action == "showPage") {
const std::string* page = message.FindString("page");
if (!page) {
NOTREACHED() << message_string;
}
if (*page == "terms") {
ui_page_ = ArcSupportHost::UIPage::TERMS;
} else if (*page == "arc-loading") {
ui_page_ = ArcSupportHost::UIPage::ARC_LOADING;
} else {
NOTREACHED() << message_string;
}
} else if (*action == "showErrorPage") {
ui_page_ = ArcSupportHost::UIPage::ERROR;
native_message_host_->OnMessage(base::StrCat(
{"{\"event\": \"onErrorPageShown\", "
"\"networkTestsShown\": ",
message.FindBool("shouldShowNetworkTests").value_or(false) ? "true"
: "false",
"}"}));
} else if (*action == "setMetricsMode") {
std::optional<bool> opt = message.FindBool("enabled");
if (!opt) {
NOTREACHED() << message_string;
}
metrics_mode_ = opt.value();
} else if (*action == "setBackupAndRestoreMode") {
std::optional<bool> opt = message.FindBool("enabled");
if (!opt) {
NOTREACHED() << message_string;
}
backup_and_restore_mode_ = opt.value();
} else if (*action == "setLocationServiceMode") {
std::optional<bool> opt = message.FindBool("enabled");
if (!opt) {
NOTREACHED() << message_string;
}
location_service_mode_ = opt.value();
} else if (*action == "closeWindow") {
// Do nothing as emulation.
} else if (*action == "setWindowBounds") {
// Do nothing as emulation.
} else {
// Unknown or unsupported action.
NOTREACHED() << message_string;
}
if (prev_ui_page != ui_page_) {
for (auto& observer : observer_list_)
observer.OnPageChanged(ui_page_);
}
}
void FakeArcSupport::CloseChannel(const std::string& error_message) {
NOTREACHED();
}
} // namespace arc
|