summaryrefslogtreecommitdiffstats
path: root/remoting/host/native_messaging
diff options
context:
space:
mode:
authorrobertphillips@google.com <robertphillips@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-01 12:45:36 +0000
committerrobertphillips@google.com <robertphillips@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-01 12:45:36 +0000
commit966224629669f9a2ef62a2e40d3d4b67c1235689 (patch)
tree92dc190252e562be3cccbb76bdec373169d825c7 /remoting/host/native_messaging
parent035213a6e5c49f37dcc7966b02efc2b6f14d2c84 (diff)
downloadchromium_src-966224629669f9a2ef62a2e40d3d4b67c1235689.zip
chromium_src-966224629669f9a2ef62a2e40d3d4b67c1235689.tar.gz
chromium_src-966224629669f9a2ef62a2e40d3d4b67c1235689.tar.bz2
Revert 232331 "It2Me native messaging: GYP and source refactoring"
> It2Me native messaging: GYP and source refactoring > > 1. Add "me2me" to the names of all the me2me specific GYP targets and source files. This makes it easier for my next CL that will add the It2Me native messaging host component. > 2. Create a remoting_native_messaging_base GYP target that contains the common native messaging plumbing code shared between remoting_core, remoting_me2me_native_messaging_host and the to-be-created remoting_it2me_native_messaging_host. > 3. Clean up some dependencies in remoting.gyp: e.g. the native messaging manifest should be depended on by the archive targets, not the native messaging host binary targets. > 4. Rename It2MeImpl to It2MeHost. > > BUG=309844 > > Review URL: https://codereview.chromium.org/49113003 TBR=weitaosu@chromium.org Review URL: https://codereview.chromium.org/50033003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@232341 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/host/native_messaging')
-rw-r--r--remoting/host/native_messaging/native_messaging_channel.cc129
-rw-r--r--remoting/host/native_messaging/native_messaging_channel.h94
-rw-r--r--remoting/host/native_messaging/native_messaging_reader.cc167
-rw-r--r--remoting/host/native_messaging/native_messaging_reader.h76
-rw-r--r--remoting/host/native_messaging/native_messaging_reader_unittest.cc163
-rw-r--r--remoting/host/native_messaging/native_messaging_writer.cc103
-rw-r--r--remoting/host/native_messaging/native_messaging_writer.h37
-rw-r--r--remoting/host/native_messaging/native_messaging_writer_unittest.cc115
8 files changed, 0 insertions, 884 deletions
diff --git a/remoting/host/native_messaging/native_messaging_channel.cc b/remoting/host/native_messaging/native_messaging_channel.cc
deleted file mode 100644
index 3d4cf60..0000000
--- a/remoting/host/native_messaging/native_messaging_channel.cc
+++ /dev/null
@@ -1,129 +0,0 @@
-// Copyright 2013 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/native_messaging/native_messaging_channel.h"
-
-#include "base/basictypes.h"
-#include "base/bind.h"
-#include "base/callback.h"
-#include "base/location.h"
-#include "base/values.h"
-
-#if defined(OS_POSIX)
-#include <unistd.h>
-#endif
-
-namespace {
-
-base::PlatformFile DuplicatePlatformFile(base::PlatformFile handle) {
- base::PlatformFile result;
-#if defined(OS_WIN)
- if (!DuplicateHandle(GetCurrentProcess(),
- handle,
- GetCurrentProcess(),
- &result,
- 0,
- FALSE,
- DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
- PLOG(ERROR) << "Failed to duplicate handle " << handle;
- return base::kInvalidPlatformFileValue;
- }
- return result;
-#elif defined(OS_POSIX)
- result = dup(handle);
- base::ClosePlatformFile(handle);
- return result;
-#else
-#error Not implemented.
-#endif
-}
-
-} // namespace
-
-namespace remoting {
-
-NativeMessagingChannel::NativeMessagingChannel(
- scoped_ptr<Delegate> delegate,
- base::PlatformFile input,
- base::PlatformFile output)
- : native_messaging_reader_(DuplicatePlatformFile(input)),
- native_messaging_writer_(new NativeMessagingWriter(
- DuplicatePlatformFile(output))),
- delegate_(delegate.Pass()),
- pending_requests_(0),
- shutdown_(false),
- weak_factory_(this) {
- weak_ptr_ = weak_factory_.GetWeakPtr();
-}
-
-NativeMessagingChannel::~NativeMessagingChannel() {
-}
-
-void NativeMessagingChannel::Start(const base::Closure& quit_closure) {
- DCHECK(CalledOnValidThread());
- DCHECK(quit_closure_.is_null());
- DCHECK(!quit_closure.is_null());
-
- quit_closure_ = quit_closure;
- native_messaging_reader_.Start(
- base::Bind(&NativeMessagingChannel::ProcessMessage, weak_ptr_),
- base::Bind(&NativeMessagingChannel::Shutdown, weak_ptr_));
-}
-
-void NativeMessagingChannel::ProcessMessage(scoped_ptr<base::Value> message) {
- DCHECK(CalledOnValidThread());
-
- // Don't process any more messages if Shutdown() has been called.
- if (shutdown_)
- return;
-
- if (message->GetType() != base::Value::TYPE_DICTIONARY) {
- LOG(ERROR) << "Expected DictionaryValue";
- Shutdown();
- return;
- }
-
- DCHECK_GE(pending_requests_, 0);
- pending_requests_++;
-
- scoped_ptr<base::DictionaryValue> message_dict(
- static_cast<base::DictionaryValue*>(message.release()));
- delegate_->ProcessMessage(
- message_dict.Pass(),
- base::Bind(&NativeMessagingChannel::SendResponse, weak_ptr_));
-}
-
-void NativeMessagingChannel::SendResponse(
- scoped_ptr<base::DictionaryValue> response) {
- DCHECK(CalledOnValidThread());
-
- bool success = response && native_messaging_writer_;
- if (success)
- success = native_messaging_writer_->WriteMessage(*response);
-
- if (!success) {
- // Close the write pipe so no more responses will be sent.
- native_messaging_writer_.reset();
- Shutdown();
- }
-
- pending_requests_--;
- DCHECK_GE(pending_requests_, 0);
-
- if (shutdown_ && !pending_requests_)
- quit_closure_.Run();
-}
-
-void NativeMessagingChannel::Shutdown() {
- DCHECK(CalledOnValidThread());
-
- if (shutdown_)
- return;
-
- shutdown_ = true;
- if (!pending_requests_)
- quit_closure_.Run();
-}
-
-} // namespace remoting
diff --git a/remoting/host/native_messaging/native_messaging_channel.h b/remoting/host/native_messaging/native_messaging_channel.h
deleted file mode 100644
index f597847..0000000
--- a/remoting/host/native_messaging/native_messaging_channel.h
+++ /dev/null
@@ -1,94 +0,0 @@
-// Copyright 2013 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_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_
-#define REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_
-
-#include "base/callback.h"
-#include "base/memory/ref_counted.h"
-#include "base/memory/scoped_ptr.h"
-#include "base/memory/weak_ptr.h"
-#include "base/platform_file.h"
-#include "base/threading/non_thread_safe.h"
-#include "remoting/host/native_messaging/native_messaging_reader.h"
-#include "remoting/host/native_messaging/native_messaging_writer.h"
-
-namespace base {
-class DictionaryValue;
-class Value;
-} // namespace base
-
-namespace remoting {
-
-// Implements reading messages and sending responses across the native messaging
-// host pipe. Delegates processing of received messages to Delegate.
-//
-// TODO(alexeypa): Add ability to switch between different |delegate_| pointers
-// on the fly. This is useful for implementing UAC-style elevation on Windows -
-// an unprivileged delegate could be replaced with another delegate that
-// forwards messages to the elevated instance of the native messaging host.
-class NativeMessagingChannel : public base::NonThreadSafe {
- public:
- // Used to sends a response back to the client app.
- typedef base::Callback<void(scoped_ptr<base::DictionaryValue> response)>
- SendResponseCallback;
-
- class Delegate {
- public:
- virtual ~Delegate() {}
-
- // Processes a message received from the client app. Invokes |done| to send
- // a response back to the client app.
- virtual void ProcessMessage(scoped_ptr<base::DictionaryValue> message,
- const SendResponseCallback& done) = 0;
- };
-
- // Constructs an object taking the ownership of |input| and |output|. Closes
- // |input| and |output| to prevent the caller from using them.
- NativeMessagingChannel(
- scoped_ptr<Delegate> delegate,
- base::PlatformFile input,
- base::PlatformFile output);
- ~NativeMessagingChannel();
-
- // Starts reading and processing messages.
- void Start(const base::Closure& quit_closure);
-
- private:
- // Processes a message received from the client app.
- void ProcessMessage(scoped_ptr<base::Value> message);
-
- // Sends a response back to the client app.
- void SendResponse(scoped_ptr<base::DictionaryValue> response);
-
- // Initiates shutdown and runs |quit_closure| if there are no pending requests
- // left.
- void Shutdown();
-
- base::Closure quit_closure_;
-
- NativeMessagingReader native_messaging_reader_;
- scoped_ptr<NativeMessagingWriter> native_messaging_writer_;
-
- // |delegate_| may post tasks to this object during destruction (but not
- // afterwards), so it needs to be destroyed before other members of this class
- // (except for |weak_factory_|).
- scoped_ptr<Delegate> delegate_;
-
- // Keeps track of pending requests. Used to delay shutdown until all responses
- // have been sent.
- int pending_requests_;
-
- // True if Shutdown() has been called.
- bool shutdown_;
-
- base::WeakPtr<NativeMessagingChannel> weak_ptr_;
- base::WeakPtrFactory<NativeMessagingChannel> weak_factory_;
-
- DISALLOW_COPY_AND_ASSIGN(NativeMessagingChannel);
-};
-
-} // namespace remoting
-
-#endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_
diff --git a/remoting/host/native_messaging/native_messaging_reader.cc b/remoting/host/native_messaging/native_messaging_reader.cc
deleted file mode 100644
index 031daf7..0000000
--- a/remoting/host/native_messaging/native_messaging_reader.cc
+++ /dev/null
@@ -1,167 +0,0 @@
-// Copyright 2013 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/native_messaging/native_messaging_reader.h"
-
-#include <string>
-
-#include "base/bind.h"
-#include "base/json/json_reader.h"
-#include "base/location.h"
-#include "base/sequenced_task_runner.h"
-#include "base/single_thread_task_runner.h"
-#include "base/stl_util.h"
-#include "base/thread_task_runner_handle.h"
-#include "base/threading/sequenced_worker_pool.h"
-#include "base/values.h"
-#include "net/base/file_stream.h"
-
-namespace {
-
-// uint32 is specified in the protocol as the type for the message header.
-typedef uint32 MessageLengthType;
-
-const int kMessageHeaderSize = sizeof(MessageLengthType);
-
-// Limit the size of received messages, to avoid excessive memory-allocation in
-// this process, and potential overflow issues when casting to a signed 32-bit
-// int.
-const MessageLengthType kMaximumMessageSize = 1024 * 1024;
-
-} // namespace
-
-namespace remoting {
-
-class NativeMessagingReader::Core {
- public:
- Core(base::PlatformFile handle,
- scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
- scoped_refptr<base::SequencedTaskRunner> read_task_runner,
- base::WeakPtr<NativeMessagingReader> reader_);
- ~Core();
-
- // Reads a message from the Native Messaging client and passes it to
- // |message_callback_| on the originating thread. Called on the reader thread.
- void ReadMessage();
-
- private:
- // Notify the reader's EOF callback when an error occurs or EOF is reached.
- void NotifyEof();
-
- net::FileStream read_stream_;
-
- base::WeakPtr<NativeMessagingReader> reader_;
-
- // Used to post the caller-supplied reader callbacks on the caller thread.
- scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
-
- // Used to DCHECK that the reader code executes on the correct thread.
- scoped_refptr<base::SequencedTaskRunner> read_task_runner_;
-
- DISALLOW_COPY_AND_ASSIGN(Core);
-};
-
-NativeMessagingReader::Core::Core(
- base::PlatformFile handle,
- scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
- scoped_refptr<base::SequencedTaskRunner> read_task_runner,
- base::WeakPtr<NativeMessagingReader> reader)
- : read_stream_(handle, base::PLATFORM_FILE_READ, NULL),
- reader_(reader),
- caller_task_runner_(caller_task_runner),
- read_task_runner_(read_task_runner) {
-}
-
-NativeMessagingReader::Core::~Core() {}
-
-void NativeMessagingReader::Core::ReadMessage() {
- DCHECK(read_task_runner_->RunsTasksOnCurrentThread());
-
- // Keep reading messages until the stream is closed or an error occurs.
- while (true) {
- MessageLengthType message_length;
- int read_result = read_stream_.ReadUntilComplete(
- reinterpret_cast<char*>(&message_length), kMessageHeaderSize);
- if (read_result != kMessageHeaderSize) {
- // 0 means EOF which is normal and should not be logged as an error.
- if (read_result != 0) {
- LOG(ERROR) << "Failed to read message header, read returned "
- << read_result;
- }
- NotifyEof();
- return;
- }
-
- if (message_length > kMaximumMessageSize) {
- LOG(ERROR) << "Message size too large: " << message_length;
- NotifyEof();
- return;
- }
-
- std::string message_json(message_length, '\0');
- read_result = read_stream_.ReadUntilComplete(string_as_array(&message_json),
- message_length);
- if (read_result != static_cast<int>(message_length)) {
- LOG(ERROR) << "Failed to read message body, read returned "
- << read_result;
- NotifyEof();
- return;
- }
-
- scoped_ptr<base::Value> message(base::JSONReader::Read(message_json));
- if (!message) {
- LOG(ERROR) << "Failed to parse JSON message: " << message;
- NotifyEof();
- return;
- }
-
- // Notify callback of new message.
- caller_task_runner_->PostTask(
- FROM_HERE, base::Bind(&NativeMessagingReader::InvokeMessageCallback,
- reader_, base::Passed(&message)));
- }
-}
-
-void NativeMessagingReader::Core::NotifyEof() {
- DCHECK(read_task_runner_->RunsTasksOnCurrentThread());
- caller_task_runner_->PostTask(
- FROM_HERE,
- base::Bind(&NativeMessagingReader::InvokeEofCallback, reader_));
-}
-
-NativeMessagingReader::NativeMessagingReader(base::PlatformFile handle)
- : reader_thread_("Reader"),
- weak_factory_(this) {
- reader_thread_.Start();
- read_task_runner_ = reader_thread_.message_loop_proxy();
- core_.reset(new Core(handle, base::ThreadTaskRunnerHandle::Get(),
- read_task_runner_, weak_factory_.GetWeakPtr()));
-}
-
-NativeMessagingReader::~NativeMessagingReader() {
- read_task_runner_->DeleteSoon(FROM_HERE, core_.release());
-}
-
-void NativeMessagingReader::Start(MessageCallback message_callback,
- base::Closure eof_callback) {
- message_callback_ = message_callback;
- eof_callback_ = eof_callback;
-
- // base::Unretained is safe since |core_| is only deleted via the
- // DeleteSoon task which is posted from this class's dtor.
- read_task_runner_->PostTask(
- FROM_HERE, base::Bind(&NativeMessagingReader::Core::ReadMessage,
- base::Unretained(core_.get())));
-}
-
-void NativeMessagingReader::InvokeMessageCallback(
- scoped_ptr<base::Value> message) {
- message_callback_.Run(message.Pass());
-}
-
-void NativeMessagingReader::InvokeEofCallback() {
- eof_callback_.Run();
-}
-
-} // namespace remoting
diff --git a/remoting/host/native_messaging/native_messaging_reader.h b/remoting/host/native_messaging/native_messaging_reader.h
deleted file mode 100644
index 06eb4cc..0000000
--- a/remoting/host/native_messaging/native_messaging_reader.h
+++ /dev/null
@@ -1,76 +0,0 @@
-// Copyright 2013 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_NATIVE_MESSAGING_NATIVE_MESSAGING_READER_H_
-#define REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_READER_H_
-
-#include "base/basictypes.h"
-#include "base/callback.h"
-#include "base/memory/scoped_ptr.h"
-#include "base/memory/weak_ptr.h"
-#include "base/platform_file.h"
-#include "base/threading/thread.h"
-
-namespace base {
-class SequencedTaskRunner;
-class Value;
-} // namespace base
-
-namespace net {
-class FileStream;
-} // namespace net
-
-namespace remoting {
-
-// This class is used for reading messages from the Native Messaging client
-// webapp.
-class NativeMessagingReader {
- public:
- typedef base::Callback<void(scoped_ptr<base::Value>)> MessageCallback;
-
- explicit NativeMessagingReader(base::PlatformFile handle);
- ~NativeMessagingReader();
-
- // Begin reading messages from the Native Messaging client webapp, calling
- // |message_callback| for each received message, or |eof_callback| if
- // EOF or error is encountered. This method is asynchronous - the callbacks
- // will be run on the same thread via PostTask. The caller should be prepared
- // for these callbacks to be invoked right up until this object is destroyed.
- void Start(MessageCallback message_callback, base::Closure eof_callback);
-
- private:
- class Core;
- friend class Core;
-
- // Wrappers posted to by the read thread to trigger the message and EOF
- // callbacks on the caller thread, and have them safely dropped if the reader
- // has been deleted before they are processed.
- void InvokeMessageCallback(scoped_ptr<base::Value> message);
- void InvokeEofCallback();
-
- // Holds the information that the read thread needs to access, such as the
- // FileStream, and the TaskRunner used for posting notifications back to this
- // class.
- scoped_ptr<Core> core_;
-
- // Caller-supplied message and end-of-file callbacks.
- MessageCallback message_callback_;
- base::Closure eof_callback_;
-
- // Separate thread used to read from the stream without blocking the main
- // thread. net::FileStream's async API cannot be used here because, on
- // Windows, it requires the file handle to have been opened for overlapped IO.
- base::Thread reader_thread_;
- scoped_refptr<base::SequencedTaskRunner> read_task_runner_;
-
- // Allows the reader to be deleted safely even when tasks may be pending on
- // it.
- base::WeakPtrFactory<NativeMessagingReader> weak_factory_;
-
- DISALLOW_COPY_AND_ASSIGN(NativeMessagingReader);
-};
-
-} // namespace remoting
-
-#endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_READER_H_
diff --git a/remoting/host/native_messaging/native_messaging_reader_unittest.cc b/remoting/host/native_messaging/native_messaging_reader_unittest.cc
deleted file mode 100644
index 26ed78d..0000000
--- a/remoting/host/native_messaging/native_messaging_reader_unittest.cc
+++ /dev/null
@@ -1,163 +0,0 @@
-// Copyright 2013 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/native_messaging/native_messaging_reader.h"
-
-#include "base/basictypes.h"
-#include "base/bind.h"
-#include "base/memory/scoped_ptr.h"
-#include "base/message_loop/message_loop.h"
-#include "base/platform_file.h"
-#include "base/run_loop.h"
-#include "base/values.h"
-#include "remoting/host/setup/test_util.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace remoting {
-
-class NativeMessagingReaderTest : public testing::Test {
- public:
- NativeMessagingReaderTest();
- virtual ~NativeMessagingReaderTest();
-
- virtual void SetUp() OVERRIDE;
- virtual void TearDown() OVERRIDE;
-
- // Starts the reader and runs the MessageLoop to completion.
- void Run();
-
- // MessageCallback passed to the Reader. Stores |message| so it can be
- // verified by tests.
- void OnMessage(scoped_ptr<base::Value> message);
-
- // Writes a message (header+body) to the write-end of the pipe.
- void WriteMessage(std::string message);
-
- // Writes some data to the write-end of the pipe.
- void WriteData(const char* data, int length);
-
- protected:
- scoped_ptr<NativeMessagingReader> reader_;
- base::PlatformFile read_handle_;
- base::PlatformFile write_handle_;
- scoped_ptr<base::Value> message_;
-
- private:
- // MessageLoop declared here, since the NativeMessageReader ctor requires a
- // MessageLoop to have been created.
- base::MessageLoop message_loop_;
- base::RunLoop run_loop_;
-};
-
-NativeMessagingReaderTest::NativeMessagingReaderTest()
- : message_loop_(base::MessageLoop::TYPE_IO) {
-}
-
-NativeMessagingReaderTest::~NativeMessagingReaderTest() {}
-
-void NativeMessagingReaderTest::SetUp() {
- ASSERT_TRUE(MakePipe(&read_handle_, &write_handle_));
- reader_.reset(new NativeMessagingReader(read_handle_));
-}
-
-void NativeMessagingReaderTest::TearDown() {
- // |read_handle_| is owned by NativeMessagingReader's FileStream, so don't
- // try to close it here. Also, |write_handle_| gets closed by Run().
-}
-
-void NativeMessagingReaderTest::Run() {
- // Close the write-end, so the reader doesn't block waiting for more data.
- base::ClosePlatformFile(write_handle_);
-
- // base::Unretained is safe since no further tasks can run after
- // RunLoop::Run() returns.
- reader_->Start(
- base::Bind(&NativeMessagingReaderTest::OnMessage, base::Unretained(this)),
- run_loop_.QuitClosure());
- run_loop_.Run();
-}
-
-void NativeMessagingReaderTest::OnMessage(scoped_ptr<base::Value> message) {
- message_ = message.Pass();
-}
-
-void NativeMessagingReaderTest::WriteMessage(std::string message) {
- uint32 length = message.length();
- WriteData(reinterpret_cast<char*>(&length), 4);
- WriteData(message.data(), length);
-}
-
-void NativeMessagingReaderTest::WriteData(const char* data, int length) {
- int written = base::WritePlatformFileAtCurrentPos(write_handle_, data,
- length);
- ASSERT_EQ(length, written);
-}
-
-TEST_F(NativeMessagingReaderTest, GoodMessage) {
- WriteMessage("{\"foo\": 42}");
- Run();
- EXPECT_TRUE(message_);
- base::DictionaryValue* message_dict;
- EXPECT_TRUE(message_->GetAsDictionary(&message_dict));
- int result;
- EXPECT_TRUE(message_dict->GetInteger("foo", &result));
- EXPECT_EQ(42, result);
-}
-
-TEST_F(NativeMessagingReaderTest, InvalidLength) {
- uint32 length = 0xffffffff;
- WriteData(reinterpret_cast<char*>(&length), 4);
- Run();
- EXPECT_FALSE(message_);
-}
-
-TEST_F(NativeMessagingReaderTest, EmptyFile) {
- Run();
- EXPECT_FALSE(message_);
-}
-
-TEST_F(NativeMessagingReaderTest, ShortHeader) {
- // Write only 3 bytes - the message length header is supposed to be 4 bytes.
- WriteData("xxx", 3);
- Run();
- EXPECT_FALSE(message_);
-}
-
-TEST_F(NativeMessagingReaderTest, EmptyBody) {
- uint32 length = 1;
- WriteData(reinterpret_cast<char*>(&length), 4);
- Run();
- EXPECT_FALSE(message_);
-}
-
-TEST_F(NativeMessagingReaderTest, ShortBody) {
- uint32 length = 2;
- WriteData(reinterpret_cast<char*>(&length), 4);
-
- // Only write 1 byte, where the header indicates there should be 2 bytes.
- WriteData("x", 1);
- Run();
- EXPECT_FALSE(message_);
-}
-
-TEST_F(NativeMessagingReaderTest, InvalidJSON) {
- std::string text = "{";
- WriteMessage(text);
- Run();
- EXPECT_FALSE(message_);
-}
-
-TEST_F(NativeMessagingReaderTest, SecondMessage) {
- WriteMessage("{}");
- WriteMessage("{\"foo\": 42}");
- Run();
- EXPECT_TRUE(message_);
- base::DictionaryValue* message_dict;
- EXPECT_TRUE(message_->GetAsDictionary(&message_dict));
- int result;
- EXPECT_TRUE(message_dict->GetInteger("foo", &result));
- EXPECT_EQ(42, result);
-}
-
-} // namespace remoting
diff --git a/remoting/host/native_messaging/native_messaging_writer.cc b/remoting/host/native_messaging/native_messaging_writer.cc
deleted file mode 100644
index 9788f21..0000000
--- a/remoting/host/native_messaging/native_messaging_writer.cc
+++ /dev/null
@@ -1,103 +0,0 @@
-// Copyright 2013 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/native_messaging/native_messaging_writer.h"
-
-#include <string>
-
-#include "base/basictypes.h"
-#include "base/json/json_writer.h"
-
-namespace {
-
-// 4-byte type used for the message header.
-typedef uint32 MessageLengthType;
-
-// Defined as an int, for passing to APIs that take an int, to avoid
-// signed/unsigned warnings about implicit cast.
-const int kMessageHeaderSize = sizeof(MessageLengthType);
-
-// Limit the size of sent messages, since Chrome will not accept messages
-// larger than 1MB, and this helps deal with the problem of integer overflow
-// when passing sizes to net::FileStream APIs that take |int| parameters.
-// This is defined as size_t (unsigned type) so it can be compared with the
-// result of std::string::length() without compiler warnings.
-const size_t kMaximumMessageSize = 1024 * 1024;
-
-// Performs the same task as FileStream::WriteSync(), but ensures that exactly
-// |buffer_length| bytes are written. Unlike WriteSync(), a partial write may
-// only occur as a result of end-of-file or fatal error. Returns the number of
-// bytes written (buffer_length) or an error-code <= 0.
-//
-// TODO(lambroslambrou): Add this method to net::FileStream, with unit-tests.
-// See http://crbug.com/232202.
-int WriteUntilComplete(net::FileStream* out,
- const char* buffer, int buffer_length) {
- int written = 0;
- while (written < buffer_length) {
- int result = out->WriteSync(buffer + written, buffer_length - written);
- if (result <= 0) {
- return result;
- }
- DCHECK_LE(result, buffer_length - written);
- written += result;
- }
- return written;
-}
-
-} // namespace
-
-namespace remoting {
-
-NativeMessagingWriter::NativeMessagingWriter(base::PlatformFile handle)
- : write_stream_(handle, base::PLATFORM_FILE_WRITE, NULL),
- fail_(false) {
-}
-
-NativeMessagingWriter::~NativeMessagingWriter() {
-}
-
-bool NativeMessagingWriter::WriteMessage(const base::Value& message) {
- if (fail_) {
- LOG(ERROR) << "Stream marked as corrupt.";
- return false;
- }
-
- std::string message_json;
- base::JSONWriter::Write(&message, &message_json);
-
- CHECK_LE(message_json.length(), kMaximumMessageSize);
-
- // Cast from size_t to the proper header type. The check above ensures this
- // won't overflow.
- MessageLengthType message_length =
- static_cast<MessageLengthType>(message_json.length());
-
- int result = WriteUntilComplete(
- &write_stream_, reinterpret_cast<char*>(&message_length),
- kMessageHeaderSize);
- if (result != kMessageHeaderSize) {
- LOG(ERROR) << "Failed to send message header, write returned " << result;
- fail_ = true;
- return false;
- }
-
- // The length check above ensures that the cast won't overflow a signed
- // 32-bit int.
- int message_length_as_int = message_length;
-
- // CHECK needed since data() is undefined on an empty std::string.
- CHECK(!message_json.empty());
- result = WriteUntilComplete(&write_stream_, message_json.data(),
- message_length_as_int);
- if (result != message_length_as_int) {
- LOG(ERROR) << "Failed to send message body, write returned " << result;
- fail_ = true;
- return false;
- }
-
- return true;
-}
-
-} // namespace remoting
diff --git a/remoting/host/native_messaging/native_messaging_writer.h b/remoting/host/native_messaging/native_messaging_writer.h
deleted file mode 100644
index 6208f91..0000000
--- a/remoting/host/native_messaging/native_messaging_writer.h
+++ /dev/null
@@ -1,37 +0,0 @@
-// Copyright 2013 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_NATIVE_MESSAGING_NATIVE_MESSAGING_WRITER_H_
-#define REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_WRITER_H_
-
-#include "base/platform_file.h"
-#include "net/base/file_stream.h"
-
-namespace base {
-class Value;
-} // namespace base
-
-namespace remoting {
-
-// This class is used for sending messages to the Native Messaging client
-// webapp.
-class NativeMessagingWriter {
- public:
- explicit NativeMessagingWriter(base::PlatformFile handle);
- ~NativeMessagingWriter();
-
- // Sends a message to the Native Messaging client, returning true if
- // successful.
- bool WriteMessage(const base::Value& message);
-
- private:
- net::FileStream write_stream_;
- bool fail_;
-
- DISALLOW_COPY_AND_ASSIGN(NativeMessagingWriter);
-};
-
-} // namespace remoting
-
-#endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_WRITER_H_
diff --git a/remoting/host/native_messaging/native_messaging_writer_unittest.cc b/remoting/host/native_messaging/native_messaging_writer_unittest.cc
deleted file mode 100644
index 52bdb1f..0000000
--- a/remoting/host/native_messaging/native_messaging_writer_unittest.cc
+++ /dev/null
@@ -1,115 +0,0 @@
-// Copyright 2013 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/native_messaging/native_messaging_writer.h"
-
-#include "base/basictypes.h"
-#include "base/json/json_reader.h"
-#include "base/memory/scoped_ptr.h"
-#include "base/platform_file.h"
-#include "base/stl_util.h"
-#include "base/values.h"
-#include "remoting/host/setup/test_util.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace remoting {
-
-class NativeMessagingWriterTest : public testing::Test {
- public:
- NativeMessagingWriterTest();
- virtual ~NativeMessagingWriterTest();
-
- virtual void SetUp() OVERRIDE;
- virtual void TearDown() OVERRIDE;
-
- protected:
- scoped_ptr<NativeMessagingWriter> writer_;
- base::PlatformFile read_handle_;
- base::PlatformFile write_handle_;
- bool read_handle_open_;
-};
-
-NativeMessagingWriterTest::NativeMessagingWriterTest() {}
-
-NativeMessagingWriterTest::~NativeMessagingWriterTest() {}
-
-void NativeMessagingWriterTest::SetUp() {
- ASSERT_TRUE(MakePipe(&read_handle_, &write_handle_));
- writer_.reset(new NativeMessagingWriter(write_handle_));
- read_handle_open_ = true;
-}
-
-void NativeMessagingWriterTest::TearDown() {
- // |write_handle_| is owned by NativeMessagingWriter's FileStream, so don't
- // try to close it here. And close |read_handle_| only if it hasn't
- // already been closed.
- if (read_handle_open_)
- base::ClosePlatformFile(read_handle_);
-}
-
-TEST_F(NativeMessagingWriterTest, GoodMessage) {
- base::DictionaryValue message;
- message.SetInteger("foo", 42);
- EXPECT_TRUE(writer_->WriteMessage(message));
-
- // Read from the pipe and verify the content.
- uint32 length;
- int read = base::ReadPlatformFileAtCurrentPos(
- read_handle_, reinterpret_cast<char*>(&length), 4);
- EXPECT_EQ(4, read);
- std::string content(length, '\0');
- read = base::ReadPlatformFileAtCurrentPos(read_handle_,
- string_as_array(&content), length);
- EXPECT_EQ(static_cast<int>(length), read);
-
- // |content| should now contain serialized |message|.
- scoped_ptr<base::Value> written_message(base::JSONReader::Read(content));
- EXPECT_TRUE(message.Equals(written_message.get()));
-
- // Nothing more should have been written. Close the write-end of the pipe,
- // and verify the read end immediately hits EOF.
- writer_.reset(NULL);
- char unused;
- read = base::ReadPlatformFileAtCurrentPos(read_handle_, &unused, 1);
- EXPECT_LE(read, 0);
-}
-
-TEST_F(NativeMessagingWriterTest, SecondMessage) {
- base::DictionaryValue message1;
- base::DictionaryValue message2;
- message2.SetInteger("foo", 42);
- EXPECT_TRUE(writer_->WriteMessage(message1));
- EXPECT_TRUE(writer_->WriteMessage(message2));
- writer_.reset(NULL);
-
- // Read two messages.
- uint32 length;
- int read;
- std::string content;
- for (int i = 0; i < 2; i++) {
- read = base::ReadPlatformFileAtCurrentPos(
- read_handle_, reinterpret_cast<char*>(&length), 4);
- EXPECT_EQ(4, read) << "i = " << i;
- content.resize(length);
- read = base::ReadPlatformFileAtCurrentPos(read_handle_,
- string_as_array(&content),
- length);
- EXPECT_EQ(static_cast<int>(length), read) << "i = " << i;
- }
-
- // |content| should now contain serialized |message2|.
- scoped_ptr<base::Value> written_message2(base::JSONReader::Read(content));
- EXPECT_TRUE(message2.Equals(written_message2.get()));
-}
-
-TEST_F(NativeMessagingWriterTest, FailedWrite) {
- // Close the read end so that writing fails immediately.
- base::ClosePlatformFile(read_handle_);
- read_handle_open_ = false;
-
- base::DictionaryValue message;
- EXPECT_FALSE(writer_->WriteMessage(message));
-}
-
-} // namespace remoting