| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/arc/extensions/fake_arc_support.h" | 5 #include "chrome/browser/chromeos/arc/extensions/fake_arc_support.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/chromeos/arc/extensions/arc_support_message_host.h" | 14 #include "chrome/browser/chromeos/arc/extensions/arc_support_message_host.h" |
| 15 | 15 |
| 16 namespace arc { | 16 namespace arc { |
| 17 | 17 |
| 18 FakeArcSupport::FakeArcSupport(ArcSupportHost* support_host) | 18 FakeArcSupport::FakeArcSupport(ArcSupportHost* support_host) |
| 19 : support_host_(support_host), weak_ptr_factory_(this) { | 19 : support_host_(support_host), weak_ptr_factory_(this) { |
| 20 DCHECK(support_host_); | 20 DCHECK(support_host_); |
| 21 support_host_->SetRequestOpenAppCallbackForTesting( | 21 support_host_->SetRequestOpenAppCallbackForTesting( |
| 22 base::Bind(&FakeArcSupport::Open, weak_ptr_factory_.GetWeakPtr())); | 22 base::Bind(&FakeArcSupport::Open, weak_ptr_factory_.GetWeakPtr())); |
| 23 } | 23 } |
| 24 | 24 |
| 25 FakeArcSupport::~FakeArcSupport() { | 25 FakeArcSupport::~FakeArcSupport() { |
| 26 // Ensure that message host is disconnected. | 26 // Ensure that message host is disconnected. |
| 27 if (native_message_host_) | 27 if (!native_message_host_) |
| 28 Close(); | 28 return; |
| 29 UnsetMessageHost(); |
| 29 } | 30 } |
| 30 | 31 |
| 31 void FakeArcSupport::Open(Profile* profile) { | 32 void FakeArcSupport::Open(Profile* profile) { |
| 32 DCHECK(!native_message_host_); | 33 DCHECK(!native_message_host_); |
| 33 native_message_host_ = ArcSupportMessageHost::Create(); | 34 native_message_host_ = ArcSupportMessageHost::Create(); |
| 34 native_message_host_->Start(this); | 35 native_message_host_->Start(this); |
| 35 support_host_->SetMessageHost( | 36 support_host_->SetMessageHost( |
| 36 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); | 37 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); |
| 37 } | 38 } |
| 38 | 39 |
| 39 void FakeArcSupport::Close() { | 40 void FakeArcSupport::Close() { |
| 40 DCHECK(native_message_host_); | 41 DCHECK(native_message_host_); |
| 41 native_message_host_->OnMessage("{\"event\": \"onWindowClosed\"}"); | 42 native_message_host_->OnMessage("{\"event\": \"onWindowClosed\"}"); |
| 42 support_host_->UnsetMessageHost( | 43 UnsetMessageHost(); |
| 43 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); | 44 } |
| 44 native_message_host_.reset(); | 45 |
| 46 void FakeArcSupport::EmulateAuthCodeResponse(const std::string& auth_code) { |
| 47 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::LSO); |
| 48 base::DictionaryValue message; |
| 49 message.SetString("event", "onAuthSucceeded"); |
| 50 message.SetString("code", auth_code); |
| 51 OnMessage(message); |
| 52 } |
| 53 |
| 54 void FakeArcSupport::EmulateAuthFailure() { |
| 55 DCHECK(native_message_host_); |
| 56 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::LSO); |
| 57 native_message_host_->OnMessage("{\"event\": \"onAuthFailed\"}"); |
| 45 } | 58 } |
| 46 | 59 |
| 47 void FakeArcSupport::ClickAgreeButton() { | 60 void FakeArcSupport::ClickAgreeButton() { |
| 48 DCHECK(native_message_host_); | |
| 49 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::TERMS); | 61 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::TERMS); |
| 50 | 62 |
| 51 base::DictionaryValue message; | 63 base::DictionaryValue message; |
| 52 message.SetString("event", "onAgreed"); | 64 message.SetString("event", "onAgreed"); |
| 53 message.SetBoolean("isMetricsEnabled", metrics_mode_); | 65 message.SetBoolean("isMetricsEnabled", metrics_mode_); |
| 54 message.SetBoolean("isBackupRestoreEnabled", backup_and_restore_mode_); | 66 message.SetBoolean("isBackupRestoreEnabled", backup_and_restore_mode_); |
| 55 message.SetBoolean("isLocationServiceEnabled", location_service_mode_); | 67 message.SetBoolean("isLocationServiceEnabled", location_service_mode_); |
| 56 | 68 OnMessage(message); |
| 57 std::string message_string; | |
| 58 if (!base::JSONWriter::Write(message, &message_string)) { | |
| 59 NOTREACHED(); | |
| 60 return; | |
| 61 } | |
| 62 native_message_host_->OnMessage(message_string); | |
| 63 } | 69 } |
| 64 | 70 |
| 65 void FakeArcSupport::ClickRetryButton() { | 71 void FakeArcSupport::ClickRetryButton() { |
| 66 DCHECK(native_message_host_); | 72 DCHECK(native_message_host_); |
| 67 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR); | 73 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR); |
| 68 native_message_host_->OnMessage("{\"event\": \"onRetryClicked\"}"); | 74 native_message_host_->OnMessage("{\"event\": \"onRetryClicked\"}"); |
| 69 } | 75 } |
| 70 | 76 |
| 77 void FakeArcSupport::ClickSendFeedbackButton() { |
| 78 DCHECK(native_message_host_); |
| 79 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR); |
| 80 native_message_host_->OnMessage("{\"event\": \"onSendFeedbackClicked\"}"); |
| 81 } |
| 82 |
| 83 void FakeArcSupport::UnsetMessageHost() { |
| 84 support_host_->UnsetMessageHost( |
| 85 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); |
| 86 native_message_host_.reset(); |
| 87 } |
| 88 |
| 71 void FakeArcSupport::PostMessageFromNativeHost( | 89 void FakeArcSupport::PostMessageFromNativeHost( |
| 72 const std::string& message_string) { | 90 const std::string& message_string) { |
| 73 std::unique_ptr<base::DictionaryValue> message = | 91 std::unique_ptr<base::DictionaryValue> message = |
| 74 base::DictionaryValue::From(base::JSONReader::Read(message_string)); | 92 base::DictionaryValue::From(base::JSONReader::Read(message_string)); |
| 75 DCHECK(message); | 93 DCHECK(message); |
| 76 | 94 |
| 77 std::string action; | 95 std::string action; |
| 78 if (!message->GetString("action", &action)) { | 96 if (!message->GetString("action", &action)) { |
| 79 NOTREACHED() << message_string; | 97 NOTREACHED() << message_string; |
| 80 return; | 98 return; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 } else { | 135 } else { |
| 118 // Unknown or unsupported action. | 136 // Unknown or unsupported action. |
| 119 NOTREACHED() << message_string; | 137 NOTREACHED() << message_string; |
| 120 } | 138 } |
| 121 } | 139 } |
| 122 | 140 |
| 123 void FakeArcSupport::CloseChannel(const std::string& error_message) { | 141 void FakeArcSupport::CloseChannel(const std::string& error_message) { |
| 124 NOTREACHED(); | 142 NOTREACHED(); |
| 125 } | 143 } |
| 126 | 144 |
| 145 void FakeArcSupport::OnMessage(const base::DictionaryValue& message) { |
| 146 DCHECK(native_message_host_); |
| 147 std::string message_string; |
| 148 if (!base::JSONWriter::Write(message, &message_string)) { |
| 149 NOTREACHED(); |
| 150 return; |
| 151 } |
| 152 native_message_host_->OnMessage(message_string); |
| 153 } |
| 154 |
| 127 } // namespace arc | 155 } // namespace arc |
| OLD | NEW |