Commit f8174e29 authored by Himanshu Jaju's avatar Himanshu Jaju Committed by Commit Bot

Create IncomingFramesReader to parse incoming frames from remote devices.

IncomingShareTargetInfo is used to encapsulate remote device connection
details, and IncomingFramesReader is used to read incoming frames from
this target. Supports timeout for reading frames as well as cache for
unused frames.

Bug: 1085068
Change-Id: I94d6f6453031f1c3172d4fecc7370ca15b0c37c4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2306032
Commit-Queue: Himanshu Jaju <himanshujaju@chromium.org>
Reviewed-by: default avatarAlex Chau <alexchau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792183}
parent e8ccc7d7
...@@ -3306,6 +3306,8 @@ static_library("browser") { ...@@ -3306,6 +3306,8 @@ static_library("browser") {
"nearby_sharing/fast_initiation_manager.h", "nearby_sharing/fast_initiation_manager.h",
"nearby_sharing/file_attachment.cc", "nearby_sharing/file_attachment.cc",
"nearby_sharing/file_attachment.h", "nearby_sharing/file_attachment.h",
"nearby_sharing/incoming_frames_reader.cc",
"nearby_sharing/incoming_frames_reader.h",
"nearby_sharing/incoming_share_target_info.cc", "nearby_sharing/incoming_share_target_info.cc",
"nearby_sharing/incoming_share_target_info.h", "nearby_sharing/incoming_share_target_info.h",
"nearby_sharing/instantmessaging/constants.h", "nearby_sharing/instantmessaging/constants.h",
......
...@@ -27,7 +27,7 @@ void FakeNearbyConnectionsManager::FakeNearbyConnection::Close() { ...@@ -27,7 +27,7 @@ void FakeNearbyConnectionsManager::FakeNearbyConnection::Close() {
std::move(disconnection_callback_).Run(); std::move(disconnection_callback_).Run();
} }
bool FakeNearbyConnectionsManager::FakeNearbyConnection::IsClosed() { bool FakeNearbyConnectionsManager::FakeNearbyConnection::IsClosed() const {
return is_closed_; return is_closed_;
} }
......
...@@ -24,7 +24,7 @@ class FakeNearbyConnectionsManager : public NearbyConnectionsManager { ...@@ -24,7 +24,7 @@ class FakeNearbyConnectionsManager : public NearbyConnectionsManager {
void Read(ReadCallback callback) override; void Read(ReadCallback callback) override;
void Write(std::vector<uint8_t> bytes, WriteCallback callback) override; void Write(std::vector<uint8_t> bytes, WriteCallback callback) override;
void Close() override; void Close() override;
bool IsClosed() override; bool IsClosed() const override;
void RegisterForDisconnection(base::OnceClosure callback) override; void RegisterForDisconnection(base::OnceClosure callback) override;
private: private:
......
// Copyright 2020 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 "chrome/browser/nearby_sharing/incoming_frames_reader.h"
#include <type_traits>
#include "base/threading/sequenced_task_runner_handle.h"
#include "chrome/browser/nearby_sharing/logging/logging.h"
#include "chrome/browser/nearby_sharing/nearby_connection.h"
#include "chrome/browser/nearby_sharing/nearby_process_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/services/sharing/public/mojom/nearby_decoder.mojom.h"
namespace {
std::ostream& operator<<(std::ostream& out,
const sharing::mojom::V1Frame::Tag& obj) {
out << static_cast<std::underlying_type<sharing::mojom::V1Frame::Tag>::type>(
obj);
return out;
}
} // namespace
IncomingFramesReader::IncomingFramesReader(
NearbyProcessManager* process_manager,
Profile* profile,
NearbyConnection* connection)
: process_manager_(process_manager),
profile_(profile),
connection_(connection) {
DCHECK(process_manager_);
DCHECK(profile_);
DCHECK(connection_);
nearby_process_observer_.Add(process_manager);
}
IncomingFramesReader::~IncomingFramesReader() = default;
void IncomingFramesReader::ReadFrame(
sharing::mojom::V1Frame::Tag frame_type,
base::OnceCallback<void(base::Optional<sharing::mojom::V1FramePtr>)>
callback,
base::TimeDelta timeout) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!callback_);
DCHECK(!is_process_stopped_);
callback_ = std::move(callback);
frame_type_ = frame_type;
timeout_callback_.Reset(base::BindOnce(&IncomingFramesReader::OnTimeout,
weak_ptr_factory_.GetWeakPtr()));
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::BindOnce(timeout_callback_.callback()), timeout);
// Check in cache for frame of type |frame_type|.
auto iter = cached_frames_.find(frame_type);
if (iter != cached_frames_.end()) {
NS_LOG(VERBOSE) << __func__ << ": Successfully read cached frame of type "
<< frame_type;
sharing::mojom::V1FramePtr frame = std::move(iter->second);
cached_frames_.erase(iter);
Done(std::move(frame));
return;
}
ReadNextFrame();
}
void IncomingFramesReader::OnNearbyProfileChanged(Profile* profile) {}
void IncomingFramesReader::OnNearbyProcessStarted() {}
void IncomingFramesReader::OnNearbyProcessStopped() {
is_process_stopped_ = true;
Done(base::nullopt);
}
void IncomingFramesReader::ReadNextFrame() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
connection_->Read(
base::BindOnce(&IncomingFramesReader::OnDataReadFromConnection,
weak_ptr_factory_.GetWeakPtr()));
}
void IncomingFramesReader::OnTimeout() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!callback_)
return;
NS_LOG(WARNING) << __func__ << ": Timed out reading from NearbyConnection.";
connection_->Close();
}
void IncomingFramesReader::OnDataReadFromConnection(
base::Optional<std::vector<uint8_t>> bytes) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!callback_) {
return;
}
if (!bytes) {
NS_LOG(WARNING) << __func__ << ": Failed to read frame of type "
<< frame_type_;
Done(base::nullopt);
return;
}
process_manager_->GetOrStartNearbySharingDecoder(profile_)->DecodeFrame(
*bytes, base::BindOnce(&IncomingFramesReader::OnFrameDecoded,
weak_ptr_factory_.GetWeakPtr()));
}
void IncomingFramesReader::OnFrameDecoded(sharing::mojom::FramePtr frame) {
if (!frame) {
ReadNextFrame();
return;
}
if (!frame->is_v1()) {
NS_LOG(VERBOSE) << __func__ << ": Frame read does not have V1Frame";
ReadNextFrame();
return;
}
sharing::mojom::V1FramePtr v1_frame(std::move(frame->get_v1()));
sharing::mojom::V1Frame::Tag v1_frame_type = v1_frame->which();
if (frame_type_ != v1_frame_type) {
NS_LOG(WARNING) << __func__ << ": Failed to read frame of type "
<< frame_type_ << ", but got frame of type "
<< v1_frame_type << ". Cached for later.";
cached_frames_.insert({v1_frame_type, std::move(v1_frame)});
ReadNextFrame();
return;
}
NS_LOG(VERBOSE) << __func__ << ": Successfully read frame of type "
<< v1_frame_type;
Done(std::move(v1_frame));
}
void IncomingFramesReader::Done(
base::Optional<sharing::mojom::V1FramePtr> frame) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
timeout_callback_.Cancel();
if (callback_) {
std::move(callback_).Run(std::move(frame));
}
}
// Copyright 2020 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 CHROME_BROWSER_NEARBY_SHARING_INCOMING_FRAMES_READER_H_
#define CHROME_BROWSER_NEARBY_SHARING_INCOMING_FRAMES_READER_H_
#include <map>
#include <vector>
#include "base/callback_forward.h"
#include "base/cancelable_callback.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "chrome/browser/nearby_sharing/nearby_process_manager.h"
#include "chrome/services/sharing/public/mojom/nearby_decoder_types.mojom.h"
class NearbyConnection;
class Profile;
// Helper class to read incoming frames from Nearby devices.
class IncomingFramesReader : public NearbyProcessManager::Observer {
public:
IncomingFramesReader(NearbyProcessManager* process_manager,
Profile* profile,
NearbyConnection* connection);
~IncomingFramesReader() override;
// Reads a frame of type |frame_type| from |connection|. |callback| is called
// with the frame read from connection or nullopt if connection socket is
// closed or |timeout| units of time has passed.
//
// Note: Callers are expected wait for |callback| to be run before scheduling
// subsequent calls.
void ReadFrame(
sharing::mojom::V1Frame::Tag frame_type,
base::OnceCallback<void(base::Optional<sharing::mojom::V1FramePtr>)>
callback,
base::TimeDelta timeout);
private:
// NearbyProcessManager::Observer:
void OnNearbyProfileChanged(Profile* profile) override;
void OnNearbyProcessStarted() override;
void OnNearbyProcessStopped() override;
void ReadNextFrame();
void OnDataReadFromConnection(base::Optional<std::vector<uint8_t>> bytes);
void OnFrameDecoded(sharing::mojom::FramePtr mojo_frame);
void OnTimeout();
void Done(base::Optional<sharing::mojom::V1FramePtr> frame);
NearbyProcessManager* process_manager_;
Profile* profile_;
NearbyConnection* connection_;
sharing::mojom::V1Frame::Tag frame_type_;
base::OnceCallback<void(base::Optional<sharing::mojom::V1FramePtr>)>
callback_;
base::CancelableOnceClosure timeout_callback_;
// Caches frames read from NearbyConnection which are not used immediately.
std::map<sharing::mojom::V1Frame::Tag, sharing::mojom::V1FramePtr>
cached_frames_;
bool is_process_stopped_ = false;
ScopedObserver<NearbyProcessManager, NearbyProcessManager::Observer>
nearby_process_observer_{this};
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<IncomingFramesReader> weak_ptr_factory_{this};
};
#endif // CHROME_BROWSER_NEARBY_SHARING_INCOMING_FRAMES_READER_H_
// Copyright 2020 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 "chrome/browser/nearby_sharing/incoming_frames_reader.h"
#include <queue>
#include <vector>
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "base/time/time.h"
#include "chrome/browser/nearby_sharing/mock_nearby_process_manager.h"
#include "chrome/browser/nearby_sharing/mock_nearby_sharing_decoder.h"
#include "chrome/browser/nearby_sharing/nearby_connection.h"
#include "chrome/services/sharing/public/proto/wire_format.pb.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr base::TimeDelta kTimeout = base::TimeDelta::FromMilliseconds(1000);
std::vector<uint8_t> GetIntroductionFrame() {
sharing::nearby::Frame frame = sharing::nearby::Frame();
sharing::nearby::V1Frame* v1frame = frame.mutable_v1();
v1frame->set_type(sharing::nearby::V1Frame_FrameType_INTRODUCTION);
v1frame->mutable_introduction();
std::vector<uint8_t> data;
data.resize(frame.ByteSize());
EXPECT_TRUE(frame.SerializeToArray(&data[0], frame.ByteSize()));
return data;
}
std::vector<uint8_t> GetCancelFrame() {
sharing::nearby::Frame frame = sharing::nearby::Frame();
sharing::nearby::V1Frame* v1frame = frame.mutable_v1();
v1frame->set_type(sharing::nearby::V1Frame_FrameType_CANCEL);
std::vector<uint8_t> data;
data.resize(frame.ByteSize());
EXPECT_TRUE(frame.SerializeToArray(&data[0], frame.ByteSize()));
return data;
}
void ExpectIntroductionFrame(
const base::Optional<sharing::mojom::V1FramePtr>& frame) {
ASSERT_TRUE(frame);
EXPECT_TRUE((*frame)->is_introduction());
}
} // namespace
class FakeNearbyConnection : public NearbyConnection {
public:
FakeNearbyConnection() = default;
~FakeNearbyConnection() override = default;
void Read(ReadCallback callback) override {
callback_ = std::move(callback);
MaybeRunCallback();
}
void Write(std::vector<uint8_t> bytes, WriteCallback callback) override {
NOTIMPLEMENTED();
}
void Close() override {
closed_ = true;
if (callback_)
std::move(callback_).Run(base::nullopt);
}
bool IsClosed() const override { return closed_; }
void RegisterForDisconnection(base::OnceClosure callback) override {
NOTIMPLEMENTED();
}
void AppendReadableData(std::vector<uint8_t> bytes) {
data_.push(std::move(bytes));
MaybeRunCallback();
}
private:
void MaybeRunCallback() {
if (!callback_ || data_.empty())
return;
auto item = std::move(data_.front());
data_.pop();
std::move(callback_).Run(std::move(item));
}
bool closed_ = false;
ReadCallback callback_;
std::queue<std::vector<uint8_t>> data_;
};
class IncomingFramesReaderTest : public testing::Test {
public:
IncomingFramesReaderTest()
: frames_reader_(&mock_process_manager_,
&profile_,
&mock_nearby_connection_) {}
~IncomingFramesReaderTest() override = default;
void SetUp() override {
EXPECT_CALL(mock_process_manager_,
GetOrStartNearbySharingDecoder(testing::Eq(&profile_)))
.WillRepeatedly(testing::Return(&mock_decoder_));
}
FakeNearbyConnection& connection() { return mock_nearby_connection_; }
testing::StrictMock<MockNearbySharingDecoder>& decoder() {
return mock_decoder_;
}
IncomingFramesReader& frames_reader() { return frames_reader_; }
private:
content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_;
FakeNearbyConnection mock_nearby_connection_;
testing::StrictMock<MockNearbyProcessManager> mock_process_manager_;
testing::StrictMock<MockNearbySharingDecoder> mock_decoder_;
IncomingFramesReader frames_reader_;
};
TEST_F(IncomingFramesReaderTest, ReadTimedOut) {
EXPECT_CALL(decoder(), DecodeFrame(testing::_, testing::_)).Times(0);
base::RunLoop run_loop;
frames_reader().ReadFrame(
sharing::mojom::V1Frame::Tag::INTRODUCTION,
base::BindLambdaForTesting(
[&](base::Optional<sharing::mojom::V1FramePtr> frame) {
EXPECT_FALSE(frame);
run_loop.Quit();
}),
kTimeout);
run_loop.Run();
}
TEST_F(IncomingFramesReaderTest, ReadSuccessful) {
std::vector<uint8_t> introduction_frame = GetIntroductionFrame();
connection().AppendReadableData(introduction_frame);
EXPECT_CALL(decoder(),
DecodeFrame(testing::Eq(introduction_frame), testing::_))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeFrameCallback callback) {
sharing::mojom::V1FramePtr mojo_v1frame =
sharing::mojom::V1Frame::New();
mojo_v1frame->set_introduction(
sharing::mojom::IntroductionFrame::New());
sharing::mojom::FramePtr mojo_frame = sharing::mojom::Frame::New();
mojo_frame->set_v1(std::move(mojo_v1frame));
std::move(callback).Run(std::move(mojo_frame));
}));
base::RunLoop run_loop;
frames_reader().ReadFrame(
sharing::mojom::V1Frame::Tag::INTRODUCTION,
base::BindLambdaForTesting(
[&](base::Optional<sharing::mojom::V1FramePtr> frame) {
ExpectIntroductionFrame(frame);
run_loop.Quit();
}),
kTimeout);
run_loop.Run();
}
TEST_F(IncomingFramesReaderTest, ReadSuccessful_JumbledFramesOrdering) {
std::vector<uint8_t> cancel_frame = GetCancelFrame();
connection().AppendReadableData(cancel_frame);
std::vector<uint8_t> introduction_frame = GetIntroductionFrame();
connection().AppendReadableData(introduction_frame);
EXPECT_CALL(decoder(), DecodeFrame(testing::_, testing::_))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeFrameCallback callback) {
EXPECT_EQ(cancel_frame, data);
sharing::mojom::V1FramePtr mojo_v1frame =
sharing::mojom::V1Frame::New();
mojo_v1frame->set_cancel_frame(sharing::mojom::CancelFrame::New());
sharing::mojom::FramePtr mojo_frame = sharing::mojom::Frame::New();
mojo_frame->set_v1(std::move(mojo_v1frame));
std::move(callback).Run(std::move(mojo_frame));
}))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeFrameCallback callback) {
EXPECT_EQ(introduction_frame, data);
sharing::mojom::V1FramePtr mojo_v1frame =
sharing::mojom::V1Frame::New();
mojo_v1frame->set_introduction(
sharing::mojom::IntroductionFrame::New());
sharing::mojom::FramePtr mojo_frame = sharing::mojom::Frame::New();
mojo_frame->set_v1(std::move(mojo_v1frame));
std::move(callback).Run(std::move(mojo_frame));
}));
base::RunLoop run_loop_introduction;
frames_reader().ReadFrame(
sharing::mojom::V1Frame::Tag::INTRODUCTION,
base::BindLambdaForTesting(
[&](base::Optional<sharing::mojom::V1FramePtr> frame) {
ExpectIntroductionFrame(frame);
run_loop_introduction.Quit();
}),
kTimeout);
run_loop_introduction.Run();
}
TEST_F(IncomingFramesReaderTest, ReadAfterTimeout) {
EXPECT_CALL(decoder(), DecodeFrame(testing::_, testing::_)).Times(0);
base::RunLoop run_loop_timeout;
frames_reader().ReadFrame(
sharing::mojom::V1Frame::Tag::INTRODUCTION,
base::BindLambdaForTesting(
[&](base::Optional<sharing::mojom::V1FramePtr> frame) {
EXPECT_FALSE(frame);
run_loop_timeout.Quit();
}),
kTimeout);
run_loop_timeout.Run();
std::vector<uint8_t> introduction_frame = GetIntroductionFrame();
connection().AppendReadableData(introduction_frame);
EXPECT_CALL(decoder(),
DecodeFrame(testing::Eq(introduction_frame), testing::_))
.WillOnce(testing::Invoke(
[&](const std::vector<uint8_t>& data,
MockNearbySharingDecoder::DecodeFrameCallback callback) {
sharing::mojom::V1FramePtr mojo_v1frame =
sharing::mojom::V1Frame::New();
mojo_v1frame->set_introduction(
sharing::mojom::IntroductionFrame::New());
sharing::mojom::FramePtr mojo_frame = sharing::mojom::Frame::New();
mojo_frame->set_v1(std::move(mojo_v1frame));
std::move(callback).Run(std::move(mojo_frame));
}));
base::RunLoop run_loop;
frames_reader().ReadFrame(
sharing::mojom::V1Frame::Tag::INTRODUCTION,
base::BindLambdaForTesting(
[&](base::Optional<sharing::mojom::V1FramePtr> frame) {
ExpectIntroductionFrame(frame);
run_loop.Quit();
}),
kTimeout);
run_loop.Run();
}
// Copyright 2020 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 "chrome/browser/nearby_sharing/mock_nearby_sharing_decoder.h"
MockNearbySharingDecoder::MockNearbySharingDecoder() = default;
MockNearbySharingDecoder::~MockNearbySharingDecoder() = default;
// Copyright 2020 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 CHROME_BROWSER_NEARBY_SHARING_MOCK_NEARBY_SHARING_DECODER_H_
#define CHROME_BROWSER_NEARBY_SHARING_MOCK_NEARBY_SHARING_DECODER_H_
#include "chrome/services/sharing/public/mojom/nearby_decoder.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
class MockNearbySharingDecoder : public sharing::mojom::NearbySharingDecoder {
public:
MockNearbySharingDecoder();
explicit MockNearbySharingDecoder(const MockNearbySharingDecoder&) = delete;
MockNearbySharingDecoder& operator=(const MockNearbySharingDecoder&) = delete;
~MockNearbySharingDecoder() override;
// sharing::mojom::NearbySharingDecoder:
MOCK_METHOD(void,
DecodeAdvertisement,
(const std::vector<uint8_t>& data,
DecodeAdvertisementCallback callback),
(override));
MOCK_METHOD(void,
DecodeFrame,
(const std::vector<uint8_t>& data, DecodeFrameCallback callback),
(override));
};
#endif // CHROME_BROWSER_NEARBY_SHARING_MOCK_NEARBY_SHARING_DECODER_H_
...@@ -35,7 +35,7 @@ class NearbyConnection { ...@@ -35,7 +35,7 @@ class NearbyConnection {
virtual void Close() = 0; virtual void Close() = 0;
// Return True if the socket is closed, False otherwise. // Return True if the socket is closed, False otherwise.
virtual bool IsClosed() = 0; virtual bool IsClosed() const = 0;
// Listens to the socket being closed. Invoke |callback| when the socket is // Listens to the socket being closed. Invoke |callback| when the socket is
// closed. // closed.
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "chrome/browser/nearby_sharing/mock_nearby_connections.h" #include "chrome/browser/nearby_sharing/mock_nearby_connections.h"
#include "chrome/browser/nearby_sharing/mock_nearby_sharing_decoder.h"
#include "chrome/browser/profiles/profile_attributes_entry.h" #include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h" #include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h"
#include "chrome/services/sharing/public/mojom/nearby_connections_types.mojom.h" #include "chrome/services/sharing/public/mojom/nearby_connections_types.mojom.h"
...@@ -39,27 +40,6 @@ using NearbySharingDecoderMojom = sharing::mojom::NearbySharingDecoder; ...@@ -39,27 +40,6 @@ using NearbySharingDecoderMojom = sharing::mojom::NearbySharingDecoder;
namespace { namespace {
class MockNearbySharingDecoder : public NearbySharingDecoderMojom {
public:
MockNearbySharingDecoder() = default;
explicit MockNearbySharingDecoder(
const sharing::mojom::NearbySharingDecoder&) = delete;
MockNearbySharingDecoder& operator=(
const sharing::mojom::NearbySharingDecoder&) = delete;
~MockNearbySharingDecoder() override = default;
// sharing::mojom::NearbySharingDecoder:
MOCK_METHOD(void,
DecodeAdvertisement,
(const std::vector<uint8_t>& data,
DecodeAdvertisementCallback callback),
(override));
MOCK_METHOD(void,
DecodeFrame,
(const std::vector<uint8_t>& data, DecodeFrameCallback callback),
(override));
};
class FakeSharingMojoService : public sharing::mojom::Sharing { class FakeSharingMojoService : public sharing::mojom::Sharing {
public: public:
FakeSharingMojoService() = default; FakeSharingMojoService() = default;
......
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
namespace { namespace {
constexpr base::TimeDelta kReadFramesTimeout = base::TimeDelta::FromSeconds(15);
std::string ReceiveSurfaceStateToString( std::string ReceiveSurfaceStateToString(
NearbySharingService::ReceiveSurfaceState state) { NearbySharingService::ReceiveSurfaceState state) {
switch (state) { switch (state) {
...@@ -655,6 +657,45 @@ void NearbySharingServiceImpl::OnIncomingTransferUpdate( ...@@ -655,6 +657,45 @@ void NearbySharingServiceImpl::OnIncomingTransferUpdate(
// TODO(himanshujaju) - Implement. // TODO(himanshujaju) - Implement.
} }
void NearbySharingServiceImpl::ReceiveIntroduction(
const ShareTarget& share_target,
const std::string& token) {
NS_LOG(INFO) << __func__ << ": Receiving introduction from "
<< share_target.device_name;
NearbyConnection* connection = GetIncomingConnection(share_target);
if (!connection) {
NS_LOG(WARNING)
<< __func__
<< ": Ignore introduction, due to no connection established.";
return;
}
auto frames_reader = std::make_unique<IncomingFramesReader>(
&NearbyProcessManager::GetInstance(), profile_, connection);
frames_reader->ReadFrame(
sharing::mojom::V1Frame::Tag::INTRODUCTION,
base::BindOnce(&NearbySharingServiceImpl::OnReceivedIntroduction,
weak_ptr_factory_.GetWeakPtr(), connection,
std::move(frames_reader)),
kReadFramesTimeout);
}
void NearbySharingServiceImpl::OnReceivedIntroduction(
NearbyConnection* connection,
std::unique_ptr<IncomingFramesReader> frames_reader,
base::Optional<sharing::mojom::V1FramePtr> frame) {
if (!frame) {
connection->Close();
NS_LOG(WARNING) << __func__ << ": Invalid introduction frame";
return;
}
NS_LOG(INFO) << __func__ << ": Successfully read the introduction frame.";
// TODO(himanshujaju) - Implement OnReceiveIntroduction
}
IncomingShareTargetInfo& NearbySharingServiceImpl::GetIncomingShareTargetInfo( IncomingShareTargetInfo& NearbySharingServiceImpl::GetIncomingShareTargetInfo(
const ShareTarget& share_target) { const ShareTarget& share_target) {
return incoming_share_target_info_map_[share_target.id]; return incoming_share_target_info_map_[share_target.id];
......
...@@ -18,12 +18,15 @@ ...@@ -18,12 +18,15 @@
#include "base/unguessable_token.h" #include "base/unguessable_token.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_http_notifier.h" #include "chrome/browser/nearby_sharing/client/nearby_share_http_notifier.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_enums.h" #include "chrome/browser/nearby_sharing/common/nearby_share_enums.h"
#include "chrome/browser/nearby_sharing/incoming_frames_reader.h"
#include "chrome/browser/nearby_sharing/incoming_share_target_info.h" #include "chrome/browser/nearby_sharing/incoming_share_target_info.h"
#include "chrome/browser/nearby_sharing/nearby_connections_manager.h" #include "chrome/browser/nearby_sharing/nearby_connections_manager.h"
#include "chrome/browser/nearby_sharing/nearby_notification_manager.h" #include "chrome/browser/nearby_sharing/nearby_notification_manager.h"
#include "chrome/browser/nearby_sharing/nearby_process_manager.h" #include "chrome/browser/nearby_sharing/nearby_process_manager.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_service.h" #include "chrome/browser/nearby_sharing/nearby_sharing_service.h"
#include "chrome/browser/nearby_sharing/outgoing_share_target_info.h" #include "chrome/browser/nearby_sharing/outgoing_share_target_info.h"
#include "chrome/browser/nearby_sharing/share_target.h"
#include "chrome/services/sharing/public/mojom/nearby_decoder_types.mojom.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h" #include "components/prefs/pref_change_registrar.h"
...@@ -108,6 +111,12 @@ class NearbySharingServiceImpl ...@@ -108,6 +111,12 @@ class NearbySharingServiceImpl
void StopAdvertising(); void StopAdvertising();
void OnIncomingTransferUpdate(const ShareTarget& share_target, void OnIncomingTransferUpdate(const ShareTarget& share_target,
TransferMetadata metadata); TransferMetadata metadata);
void ReceiveIntroduction(const ShareTarget& share_target,
const std::string& token);
void OnReceivedIntroduction(
NearbyConnection* connection,
std::unique_ptr<IncomingFramesReader> frames_reader,
base::Optional<sharing::mojom::V1FramePtr> frame);
IncomingShareTargetInfo& GetIncomingShareTargetInfo( IncomingShareTargetInfo& GetIncomingShareTargetInfo(
const ShareTarget& share_target); const ShareTarget& share_target);
......
...@@ -3664,6 +3664,7 @@ test("unit_tests") { ...@@ -3664,6 +3664,7 @@ test("unit_tests") {
"../browser/nearby_sharing/fake_nearby_connections_manager.cc", "../browser/nearby_sharing/fake_nearby_connections_manager.cc",
"../browser/nearby_sharing/fake_nearby_connections_manager.h", "../browser/nearby_sharing/fake_nearby_connections_manager.h",
"../browser/nearby_sharing/fast_initiation_manager_unittest.cc", "../browser/nearby_sharing/fast_initiation_manager_unittest.cc",
"../browser/nearby_sharing/incoming_frames_reader_unittest.cc",
"../browser/nearby_sharing/instantmessaging/fake_token_fetcher.cc", "../browser/nearby_sharing/instantmessaging/fake_token_fetcher.cc",
"../browser/nearby_sharing/instantmessaging/fake_token_fetcher.h", "../browser/nearby_sharing/instantmessaging/fake_token_fetcher.h",
"../browser/nearby_sharing/instantmessaging/receive_messages_express_unittest.cc", "../browser/nearby_sharing/instantmessaging/receive_messages_express_unittest.cc",
...@@ -3673,6 +3674,8 @@ test("unit_tests") { ...@@ -3673,6 +3674,8 @@ test("unit_tests") {
"../browser/nearby_sharing/mock_nearby_connections.h", "../browser/nearby_sharing/mock_nearby_connections.h",
"../browser/nearby_sharing/mock_nearby_process_manager.cc", "../browser/nearby_sharing/mock_nearby_process_manager.cc",
"../browser/nearby_sharing/mock_nearby_process_manager.h", "../browser/nearby_sharing/mock_nearby_process_manager.h",
"../browser/nearby_sharing/mock_nearby_sharing_decoder.cc",
"../browser/nearby_sharing/mock_nearby_sharing_decoder.h",
"../browser/nearby_sharing/mock_nearby_sharing_service.cc", "../browser/nearby_sharing/mock_nearby_sharing_service.cc",
"../browser/nearby_sharing/mock_nearby_sharing_service.h", "../browser/nearby_sharing/mock_nearby_sharing_service.h",
"../browser/nearby_sharing/nearby_confirmation_manager_unittest.cc", "../browser/nearby_sharing/nearby_confirmation_manager_unittest.cc",
......
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