Skip to content

Commit

Permalink
[Chromoting] Implement HostExperimentSessionPlugin
Browse files Browse the repository at this point in the history
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}
  • Loading branch information
zijiehe authored and Commit bot committed Jan 6, 2017
1 parent 5d47305 commit 8e659c5
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 3 deletions.
3 changes: 3 additions & 0 deletions remoting/host/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
50 changes: 50 additions & 0 deletions remoting/host/host_experiment_session_plugin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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
41 changes: 41 additions & 0 deletions remoting/host/host_experiment_session_plugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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_
64 changes: 64 additions & 0 deletions remoting/host/host_experiment_session_plugin_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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
3 changes: 0 additions & 3 deletions remoting/protocol/session_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8e659c5

Please sign in to comment.