Commit 8e659c58 authored by zijiehe's avatar zijiehe Committed by Commit bot

[Chromoting] Implement HostExperimentSessionPlugin

HostExperimentSessionPlugin is a host side SessionPlugin to send host attributes
to, and receive configuration from the client side.
A host can attach this plugin to the Session and use its configuration to
initialize a HostSessionOptions.

This is part of host experiment framework.

BUG=650926

Review-Url: https://codereview.chromium.org/2586133002
Cr-Commit-Position: refs/heads/master@{#441850}
parent 5d473050
......@@ -162,6 +162,8 @@ static_library("host") {
"host_event_logger_win.cc",
"host_exit_codes.cc",
"host_exit_codes.h",
"host_experiment_session_plugin.cc",
"host_experiment_session_plugin.h",
"host_export.h",
"host_extension.h",
"host_extension_session.h",
......@@ -498,6 +500,7 @@ source_set("unit_tests") {
"host_attributes_unittest.cc",
"host_change_notification_listener_unittest.cc",
"host_config_unittest.cc",
"host_experiment_session_plugin_unittest.cc",
"host_extension_session_manager_unittest.cc",
"host_power_save_blocker_unittest.cc",
"host_session_options_unittest.cc",
......
// 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 "remoting/host/host_experiment_session_plugin.h"
#include "remoting/base/constants.h"
#include "remoting/host/host_attributes.h"
namespace remoting {
using buzz::QName;
using buzz::XmlElement;
std::unique_ptr<XmlElement> HostExperimentSessionPlugin::GetNextMessage() {
if (attributes_sent_) {
return nullptr;
}
attributes_sent_ = true;
std::unique_ptr<XmlElement> attributes(
new XmlElement(QName(kChromotingXmlNamespace, "host-attributes")));
attributes->SetBodyText(GetHostAttributes());
return attributes;
}
void HostExperimentSessionPlugin::OnIncomingMessage(
const XmlElement& attachments) {
if (configuration_received_) {
return;
}
const XmlElement* configuration = attachments.FirstNamed(
QName(kChromotingXmlNamespace, "host-configuration"));
if (!configuration) {
return;
}
configuration_received_ = true;
configuration_ = configuration->BodyText();
}
bool HostExperimentSessionPlugin::configuration_received() const {
return configuration_received_;
}
const std::string& HostExperimentSessionPlugin::configuration() const {
return configuration_;
}
} // namespace remoting
// 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.
#ifndef REMOTING_HOST_HOST_EXPERIMENT_SESSION_PLUGIN_H_
#define REMOTING_HOST_HOST_EXPERIMENT_SESSION_PLUGIN_H_
#include <memory>
#include <string>
#include "remoting/protocol/session_plugin.h"
#include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
namespace remoting {
// A SessionPlugin implementation to send host attributes to client, and
// receives experiment settings.
class HostExperimentSessionPlugin : public protocol::SessionPlugin {
public:
using SessionPlugin::SessionPlugin;
// protocol::SessionPlug implementation.
std::unique_ptr<buzz::XmlElement> GetNextMessage() override;
void OnIncomingMessage(const buzz::XmlElement& attachments) override;
// Whether we have received configuration from client.
bool configuration_received() const;
// The configuration sent from client, may be empty.
const std::string& configuration() const;
private:
bool attributes_sent_ = false;
bool configuration_received_ = false;
std::string configuration_;
};
} // namespace remoting
#endif // REMOTING_HOST_HOST_EXPERIMENT_SESSION_PLUGIN_H_
// 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 "remoting/host/host_experiment_session_plugin.h"
#include <memory>
#include "base/bind.h"
#include "remoting/base/constants.h"
#include "remoting/host/host_attributes.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
using buzz::QName;
using buzz::XmlElement;
namespace remoting {
TEST(HostExperimentSessionPluginTest, AttachAttributes) {
HostExperimentSessionPlugin plugin;
std::unique_ptr<XmlElement> attachments = plugin.GetNextMessage();
ASSERT_TRUE(attachments);
ASSERT_EQ(attachments->Name(),
QName(kChromotingXmlNamespace, "host-attributes"));
ASSERT_EQ(attachments->BodyText(), GetHostAttributes());
attachments.reset();
attachments = plugin.GetNextMessage();
ASSERT_FALSE(attachments);
}
TEST(HostExperimentSessionPluginTest, LoadConfiguration) {
std::unique_ptr<XmlElement> attachment(
new XmlElement(QName(kChromotingXmlNamespace, "attachments")));
XmlElement* configuration =
new XmlElement(QName(kChromotingXmlNamespace, "host-configuration"));
configuration->SetBodyText("This Is A Test Configuration");
attachment->AddElement(configuration);
HostExperimentSessionPlugin plugin;
plugin.OnIncomingMessage(*attachment);
ASSERT_TRUE(plugin.configuration_received());
ASSERT_EQ(plugin.configuration(), "This Is A Test Configuration");
}
TEST(HostExperimentSessionPluginTest, IgnoreSecondConfiguration) {
std::unique_ptr<XmlElement> attachment(
new XmlElement(QName(kChromotingXmlNamespace, "attachments")));
XmlElement* configuration =
new XmlElement(QName(kChromotingXmlNamespace, "host-configuration"));
attachment->AddElement(configuration);
configuration->SetBodyText("config1");
HostExperimentSessionPlugin plugin;
plugin.OnIncomingMessage(*attachment);
ASSERT_TRUE(plugin.configuration_received());
ASSERT_EQ(plugin.configuration(), "config1");
configuration->SetBodyText("config2");
plugin.OnIncomingMessage(*attachment);
ASSERT_TRUE(plugin.configuration_received());
ASSERT_EQ(plugin.configuration(), "config1");
}
} // namespace remoting
......@@ -7,9 +7,6 @@
#include <memory>
#include "base/callback.h"
#include "remoting/protocol/jingle_messages.h"
#include "remoting/protocol/session.h"
#include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
namespace remoting {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment