Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/arc/extensions/fake_arc_support.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/json/json_reader.h" | |
| 11 #include "base/json/json_writer.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/chromeos/arc/extensions/arc_support_message_host.h" | |
| 15 | |
| 16 namespace arc { | |
| 17 | |
| 18 FakeArcSupport::FakeArcSupport(ArcSupportHost* support_host) | |
| 19 : support_host_(support_host), weak_ptr_factory_(this) { | |
| 20 DCHECK(support_host_); | |
| 21 support_host_->SetRequestOpenAppCallbackForTest( | |
| 22 base::Bind(&FakeArcSupport::Open, weak_ptr_factory_.GetWeakPtr())); | |
| 23 } | |
| 24 | |
| 25 FakeArcSupport::~FakeArcSupport() { | |
| 26 // Ensure that message host is disconnected. | |
| 27 if (native_message_host_) | |
| 28 Close(); | |
| 29 } | |
| 30 | |
| 31 void FakeArcSupport::Open(Profile* profile) { | |
| 32 DCHECK(!native_message_host_); | |
| 33 native_message_host_ = ArcSupportMessageHost::Create(); | |
| 34 native_message_host_->Start(this); | |
| 35 support_host_->SetMessageHost( | |
| 36 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); | |
| 37 } | |
| 38 | |
| 39 void FakeArcSupport::Close() { | |
| 40 DCHECK(native_message_host_); | |
| 41 native_message_host_->OnMessage("{\"event\": \"onWindowClosed\"}"); | |
| 42 support_host_->UnsetMessageHost( | |
| 43 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); | |
| 44 native_message_host_.reset(); | |
| 45 } | |
| 46 | |
| 47 void FakeArcSupport::ClickAgreeButton() { | |
| 48 DCHECK(native_message_host_); | |
| 49 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::TERMS); | |
| 50 | |
| 51 base::DictionaryValue message; | |
| 52 message.SetString("event", "onAgreed"); | |
| 53 message.SetBoolean("isMetricsEnabled", metrics_mode_); | |
| 54 message.SetBoolean("isBackupRestoreEnabled", backup_and_restore_mode_); | |
| 55 message.SetBoolean("isLocationServiceEnabled", location_service_mode_); | |
| 56 | |
| 57 std::string message_string; | |
| 58 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
| |
| 59 native_message_host_->OnMessage(message_string); | |
| 60 } | |
| 61 | |
| 62 void FakeArcSupport::ClickRetryButton() { | |
| 63 DCHECK(native_message_host_); | |
| 64 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR); | |
| 65 native_message_host_->OnMessage("{\"event\": \"onRetryClicked\"}"); | |
| 66 } | |
| 67 | |
| 68 void FakeArcSupport::PostMessageFromNativeHost( | |
| 69 const std::string& message_string) { | |
| 70 std::unique_ptr<base::DictionaryValue> message = | |
| 71 base::DictionaryValue::From(base::JSONReader::Read(message_string)); | |
| 72 DCHECK(message); | |
| 73 | |
| 74 std::string action; | |
| 75 if (!message->GetString("action", &action)) { | |
| 76 NOTREACHED(); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 if (action == "initialize") { | |
| 81 // Do nothing as emulation. | |
| 82 } else if (action == "showPage") { | |
| 83 std::string page; | |
| 84 if (!message->GetString("page", &page)) { | |
| 85 NOTREACHED(); | |
| 86 return; | |
| 87 } | |
| 88 if (page == "terms") { | |
| 89 ui_page_ = ArcSupportHost::UIPage::TERMS; | |
| 90 } else if (page == "lso-loading") { | |
| 91 ui_page_ = ArcSupportHost::UIPage::LSO; | |
| 92 } else if (page == "arc-loading") { | |
| 93 ui_page_ = ArcSupportHost::UIPage::ARC_LOADING; | |
| 94 } else { | |
| 95 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.
| |
| 96 } | |
| 97 } else if (action == "showErrorPage") { | |
| 98 ui_page_ = ArcSupportHost::UIPage::ERROR; | |
| 99 } else if (action == "setMetricsMode") { | |
| 100 if (!message->GetBoolean("enabled", &metrics_mode_)) { | |
| 101 NOTREACHED(); | |
| 102 return; | |
| 103 } | |
| 104 } else if (action == "setBackupAndRestoreMode") { | |
| 105 if (!message->GetBoolean("enabled", &backup_and_restore_mode_)) { | |
| 106 NOTREACHED(); | |
| 107 return; | |
| 108 } | |
| 109 } else if (action == "setLocationServiceMode") { | |
| 110 if (!message->GetBoolean("enabled", &location_service_mode_)) { | |
| 111 NOTREACHED(); | |
| 112 return; | |
| 113 } | |
| 114 } else { | |
| 115 // Unknown or unsupported action. | |
| 116 NOTREACHED(); | |
|
Luis Héctor Chávez
2016/11/29 00:15:28
nit: NOTREACHED() << action;
hidehiko
2016/11/29 17:43:28
Acknowledged.
| |
| 117 } | |
| 118 } | |
| 119 | |
| 120 void FakeArcSupport::CloseChannel(const std::string& error_message) { | |
| 121 NOTREACHED(); | |
| 122 } | |
| 123 | |
| 124 } // namespace arc | |
| OLD | NEW |