Chromium Code Reviews| 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_) { |
|
Luis Héctor Chávez
2017/05/30 16:06:27
nit: use guard clause pattern
if (!native_message
victorhsieh0
2017/05/30 19:52:42
Done.
| |
| 28 Close(); | 28 support_host_->UnsetMessageHost( |
| 29 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); | |
| 30 native_message_host_.reset(); | |
| 31 } | |
| 29 } | 32 } |
| 30 | 33 |
| 31 void FakeArcSupport::Open(Profile* profile) { | 34 void FakeArcSupport::Open(Profile* profile) { |
| 32 DCHECK(!native_message_host_); | 35 DCHECK(!native_message_host_); |
| 33 native_message_host_ = ArcSupportMessageHost::Create(); | 36 native_message_host_ = ArcSupportMessageHost::Create(); |
| 34 native_message_host_->Start(this); | 37 native_message_host_->Start(this); |
| 35 support_host_->SetMessageHost( | 38 support_host_->SetMessageHost( |
| 36 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); | 39 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); |
| 37 } | 40 } |
| 38 | 41 |
| 39 void FakeArcSupport::Close() { | 42 void FakeArcSupport::Close() { |
| 40 DCHECK(native_message_host_); | 43 DCHECK(native_message_host_); |
| 41 native_message_host_->OnMessage("{\"event\": \"onWindowClosed\"}"); | 44 native_message_host_->OnMessage("{\"event\": \"onWindowClosed\"}"); |
| 42 support_host_->UnsetMessageHost( | 45 support_host_->UnsetMessageHost( |
| 43 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); | 46 static_cast<ArcSupportMessageHost*>(native_message_host_.get())); |
| 44 native_message_host_.reset(); | 47 native_message_host_.reset(); |
| 45 } | 48 } |
| 46 | 49 |
| 50 void FakeArcSupport::EmulateAuthCodeResponse(const std::string& auth_code) { | |
| 51 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::LSO); | |
| 52 base::DictionaryValue message; | |
| 53 message.SetString("event", "onAuthSucceeded"); | |
| 54 message.SetString("code", auth_code); | |
| 55 OnMessage(message); | |
| 56 } | |
| 57 | |
| 58 void FakeArcSupport::EmulateAuthFailure() { | |
| 59 DCHECK(native_message_host_); | |
| 60 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::LSO); | |
| 61 native_message_host_->OnMessage("{\"event\": \"onAuthFailed\"}"); | |
| 62 } | |
| 63 | |
| 47 void FakeArcSupport::ClickAgreeButton() { | 64 void FakeArcSupport::ClickAgreeButton() { |
| 48 DCHECK(native_message_host_); | |
| 49 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::TERMS); | 65 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::TERMS); |
| 50 | 66 |
| 51 base::DictionaryValue message; | 67 base::DictionaryValue message; |
| 52 message.SetString("event", "onAgreed"); | 68 message.SetString("event", "onAgreed"); |
| 53 message.SetBoolean("isMetricsEnabled", metrics_mode_); | 69 message.SetBoolean("isMetricsEnabled", metrics_mode_); |
| 54 message.SetBoolean("isBackupRestoreEnabled", backup_and_restore_mode_); | 70 message.SetBoolean("isBackupRestoreEnabled", backup_and_restore_mode_); |
| 55 message.SetBoolean("isLocationServiceEnabled", location_service_mode_); | 71 message.SetBoolean("isLocationServiceEnabled", location_service_mode_); |
| 56 | 72 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 } | 73 } |
| 64 | 74 |
| 65 void FakeArcSupport::ClickRetryButton() { | 75 void FakeArcSupport::ClickRetryButton() { |
| 66 DCHECK(native_message_host_); | 76 DCHECK(native_message_host_); |
| 67 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR); | 77 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR); |
| 68 native_message_host_->OnMessage("{\"event\": \"onRetryClicked\"}"); | 78 native_message_host_->OnMessage("{\"event\": \"onRetryClicked\"}"); |
| 69 } | 79 } |
| 70 | 80 |
| 81 void FakeArcSupport::ClickSendFeedbackButton() { | |
| 82 DCHECK(native_message_host_); | |
| 83 DCHECK_EQ(ui_page_, ArcSupportHost::UIPage::ERROR); | |
| 84 native_message_host_->OnMessage("{\"event\": \"onSendFeedbackClicked\"}"); | |
| 85 } | |
| 86 | |
| 71 void FakeArcSupport::PostMessageFromNativeHost( | 87 void FakeArcSupport::PostMessageFromNativeHost( |
| 72 const std::string& message_string) { | 88 const std::string& message_string) { |
| 73 std::unique_ptr<base::DictionaryValue> message = | 89 std::unique_ptr<base::DictionaryValue> message = |
| 74 base::DictionaryValue::From(base::JSONReader::Read(message_string)); | 90 base::DictionaryValue::From(base::JSONReader::Read(message_string)); |
| 75 DCHECK(message); | 91 DCHECK(message); |
| 76 | 92 |
| 77 std::string action; | 93 std::string action; |
| 78 if (!message->GetString("action", &action)) { | 94 if (!message->GetString("action", &action)) { |
| 79 NOTREACHED() << message_string; | 95 NOTREACHED() << message_string; |
| 80 return; | 96 return; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 } else { | 133 } else { |
| 118 // Unknown or unsupported action. | 134 // Unknown or unsupported action. |
| 119 NOTREACHED() << message_string; | 135 NOTREACHED() << message_string; |
| 120 } | 136 } |
| 121 } | 137 } |
| 122 | 138 |
| 123 void FakeArcSupport::CloseChannel(const std::string& error_message) { | 139 void FakeArcSupport::CloseChannel(const std::string& error_message) { |
| 124 NOTREACHED(); | 140 NOTREACHED(); |
| 125 } | 141 } |
| 126 | 142 |
| 143 void FakeArcSupport::OnMessage(const base::DictionaryValue& message) { | |
| 144 DCHECK(native_message_host_); | |
| 145 std::string message_string; | |
| 146 if (!base::JSONWriter::Write(message, &message_string)) { | |
| 147 NOTREACHED(); | |
| 148 return; | |
| 149 } | |
| 150 native_message_host_->OnMessage(message_string); | |
| 151 } | |
| 152 | |
| 127 } // namespace arc | 153 } // namespace arc |
| OLD | NEW |