Chromium Code Reviews| Index: chrome/browser/chromeos/arc/extensions/fake_arc_support.cc |
| diff --git a/chrome/browser/chromeos/arc/extensions/fake_arc_support.cc b/chrome/browser/chromeos/arc/extensions/fake_arc_support.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..57e0d36b6740da121a89bbbf17659529b0370ef5 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/extensions/fake_arc_support.cc |
| @@ -0,0 +1,124 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/arc/extensions/fake_arc_support.h" |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/logging.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/chromeos/arc/extensions/arc_support_message_host.h" |
| + |
| +namespace arc { |
| + |
| +FakeArcSupport::FakeArcSupport(ArcSupportHost* support_host) |
| + : support_host_(support_host), weak_ptr_factory_(this) { |
| + DCHECK(support_host_); |
| + support_host_->SetRequestOpenAppCallbackForTest( |
| + base::Bind(&FakeArcSupport::Open, weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +FakeArcSupport::~FakeArcSupport() { |
| + // Ensure that message host is disconnected. |
| + if (native_message_host_) |
| + Close(); |
| +} |
| + |
| +void FakeArcSupport::Open(Profile* profile) { |
| + DCHECK(!native_message_host_); |
| + native_message_host_ = ArcSupportMessageHost::Create(); |
| + 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\"}"); |
| + support_host_->UnsetMessageHost( |
| + static_cast<ArcSupportMessageHost*>(native_message_host_.get())); |
| + native_message_host_.reset(); |
| +} |
| + |
| +void FakeArcSupport::ClickAgreeButton() { |
| + DCHECK(native_message_host_); |
| + DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::TERMS); |
| + |
| + base::DictionaryValue message; |
| + message.SetString("event", "onAgreed"); |
| + message.SetBoolean("isMetricsEnabled", metrics_mode_); |
| + message.SetBoolean("isBackupRestoreEnabled", backup_and_restore_mode_); |
| + message.SetBoolean("isLocationServiceEnabled", location_service_mode_); |
| + |
| + std::string message_string; |
| + base::JSONWriter::Write(message, &message_string); |
|
Luis Héctor Chávez
2016/11/29 00:15:28
nit: DCHECK? NOTREACHED?
hidehiko
2016/11/29 17:43:28
Sorry, DCHECK for ... what?
Luis Héctor Chávez
2016/11/29 18:18:12
base::JSONWriter::Write returns bool. We should st
|
| + native_message_host_->OnMessage(message_string); |
| +} |
| + |
| +void FakeArcSupport::ClickRetryButton() { |
| + DCHECK(native_message_host_); |
| + DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR); |
| + native_message_host_->OnMessage("{\"event\": \"onRetryClicked\"}"); |
| +} |
| + |
| +void FakeArcSupport::PostMessageFromNativeHost( |
| + const std::string& message_string) { |
| + std::unique_ptr<base::DictionaryValue> message = |
| + base::DictionaryValue::From(base::JSONReader::Read(message_string)); |
| + DCHECK(message); |
| + |
| + std::string action; |
| + if (!message->GetString("action", &action)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + if (action == "initialize") { |
| + // Do nothing as emulation. |
| + } else if (action == "showPage") { |
| + std::string page; |
| + if (!message->GetString("page", &page)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + if (page == "terms") { |
| + ui_page_ = ArcSupportHost::UIPage::TERMS; |
| + } else if (page == "lso-loading") { |
| + ui_page_ = ArcSupportHost::UIPage::LSO; |
| + } else if (page == "arc-loading") { |
| + ui_page_ = ArcSupportHost::UIPage::ARC_LOADING; |
| + } else { |
| + NOTREACHED(); |
|
Luis Héctor Chávez
2016/11/29 00:15:28
nit: NOTREACHED() << page;
hidehiko
2016/11/29 17:43:28
Instead, let's output entire message.
|
| + } |
| + } else if (action == "showErrorPage") { |
| + ui_page_ = ArcSupportHost::UIPage::ERROR; |
| + } else if (action == "setMetricsMode") { |
| + if (!message->GetBoolean("enabled", &metrics_mode_)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + } else if (action == "setBackupAndRestoreMode") { |
| + if (!message->GetBoolean("enabled", &backup_and_restore_mode_)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + } else if (action == "setLocationServiceMode") { |
| + if (!message->GetBoolean("enabled", &location_service_mode_)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + } else { |
| + // Unknown or unsupported action. |
| + NOTREACHED(); |
|
Luis Héctor Chávez
2016/11/29 00:15:28
nit: NOTREACHED() << action;
hidehiko
2016/11/29 17:43:28
Acknowledged.
|
| + } |
| +} |
| + |
| +void FakeArcSupport::CloseChannel(const std::string& error_message) { |
| + NOTREACHED(); |
| +} |
| + |
| +} // namespace arc |