| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/messaging/native_message_process_host.h" | 5 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 |
| 10 #include <memory> |
| 9 #include <utility> | 11 #include <utility> |
| 10 | 12 |
| 11 #include "base/bind.h" | 13 #include "base/bind.h" |
| 12 #include "base/files/file.h" | 14 #include "base/files/file.h" |
| 13 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 15 #include "base/files/scoped_file.h" | 17 #include "base/files/scoped_file.h" |
| 16 #include "base/files/scoped_temp_dir.h" | 18 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/json/json_reader.h" | 19 #include "base/json/json_reader.h" |
| 18 #include "base/memory/scoped_ptr.h" | |
| 19 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 20 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 21 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
| 22 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
| 23 #include "base/test/test_timeouts.h" | 24 #include "base/test/test_timeouts.h" |
| 24 #include "base/threading/platform_thread.h" | 25 #include "base/threading/platform_thread.h" |
| 25 #include "base/threading/sequenced_worker_pool.h" | 26 #include "base/threading/sequenced_worker_pool.h" |
| 26 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 27 #include "build/build_config.h" | 28 #include "build/build_config.h" |
| 28 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" | 29 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 49 | 50 |
| 50 } // namespace | 51 } // namespace |
| 51 | 52 |
| 52 namespace extensions { | 53 namespace extensions { |
| 53 | 54 |
| 54 class FakeLauncher : public NativeProcessLauncher { | 55 class FakeLauncher : public NativeProcessLauncher { |
| 55 public: | 56 public: |
| 56 FakeLauncher(base::File read_file, base::File write_file) | 57 FakeLauncher(base::File read_file, base::File write_file) |
| 57 : read_file_(std::move(read_file)), write_file_(std::move(write_file)) {} | 58 : read_file_(std::move(read_file)), write_file_(std::move(write_file)) {} |
| 58 | 59 |
| 59 static scoped_ptr<NativeProcessLauncher> Create(base::FilePath read_file, | 60 static std::unique_ptr<NativeProcessLauncher> Create( |
| 60 base::FilePath write_file) { | 61 base::FilePath read_file, |
| 62 base::FilePath write_file) { |
| 61 int read_flags = base::File::FLAG_OPEN | base::File::FLAG_READ; | 63 int read_flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 62 int write_flags = base::File::FLAG_CREATE | base::File::FLAG_WRITE; | 64 int write_flags = base::File::FLAG_CREATE | base::File::FLAG_WRITE; |
| 63 #if !defined(OS_POSIX) | 65 #if !defined(OS_POSIX) |
| 64 read_flags |= base::File::FLAG_ASYNC; | 66 read_flags |= base::File::FLAG_ASYNC; |
| 65 write_flags |= base::File::FLAG_ASYNC; | 67 write_flags |= base::File::FLAG_ASYNC; |
| 66 #endif | 68 #endif |
| 67 return scoped_ptr<NativeProcessLauncher>(new FakeLauncher( | 69 return std::unique_ptr<NativeProcessLauncher>( |
| 68 base::File(read_file, read_flags), | 70 new FakeLauncher(base::File(read_file, read_flags), |
| 69 base::File(write_file, write_flags))); | 71 base::File(write_file, write_flags))); |
| 70 } | 72 } |
| 71 | 73 |
| 72 static scoped_ptr<NativeProcessLauncher> CreateWithPipeInput( | 74 static std::unique_ptr<NativeProcessLauncher> CreateWithPipeInput( |
| 73 base::File read_pipe, | 75 base::File read_pipe, |
| 74 base::FilePath write_file) { | 76 base::FilePath write_file) { |
| 75 int write_flags = base::File::FLAG_CREATE | base::File::FLAG_WRITE; | 77 int write_flags = base::File::FLAG_CREATE | base::File::FLAG_WRITE; |
| 76 #if !defined(OS_POSIX) | 78 #if !defined(OS_POSIX) |
| 77 write_flags |= base::File::FLAG_ASYNC; | 79 write_flags |= base::File::FLAG_ASYNC; |
| 78 #endif | 80 #endif |
| 79 | 81 |
| 80 return scoped_ptr<NativeProcessLauncher>(new FakeLauncher( | 82 return std::unique_ptr<NativeProcessLauncher>(new FakeLauncher( |
| 81 std::move(read_pipe), base::File(write_file, write_flags))); | 83 std::move(read_pipe), base::File(write_file, write_flags))); |
| 82 } | 84 } |
| 83 | 85 |
| 84 void Launch(const GURL& origin, | 86 void Launch(const GURL& origin, |
| 85 const std::string& native_host_name, | 87 const std::string& native_host_name, |
| 86 const LaunchedCallback& callback) const override { | 88 const LaunchedCallback& callback) const override { |
| 87 callback.Run(NativeProcessLauncher::RESULT_SUCCESS, base::Process(), | 89 callback.Run(NativeProcessLauncher::RESULT_SUCCESS, base::Process(), |
| 88 std::move(read_file_), std::move(write_file_)); | 90 std::move(read_file_), std::move(write_file_)); |
| 89 } | 91 } |
| 90 | 92 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 109 BrowserThread::DeleteSoon( | 111 BrowserThread::DeleteSoon( |
| 110 BrowserThread::IO, FROM_HERE, native_message_host_.release()); | 112 BrowserThread::IO, FROM_HERE, native_message_host_.release()); |
| 111 } | 113 } |
| 112 base::RunLoop().RunUntilIdle(); | 114 base::RunLoop().RunUntilIdle(); |
| 113 } | 115 } |
| 114 | 116 |
| 115 void PostMessageFromNativeHost(const std::string& message) override { | 117 void PostMessageFromNativeHost(const std::string& message) override { |
| 116 last_message_ = message; | 118 last_message_ = message; |
| 117 | 119 |
| 118 // Parse the message. | 120 // Parse the message. |
| 119 scoped_ptr<base::DictionaryValue> dict_value = | 121 std::unique_ptr<base::DictionaryValue> dict_value = |
| 120 base::DictionaryValue::From(base::JSONReader::Read(message)); | 122 base::DictionaryValue::From(base::JSONReader::Read(message)); |
| 121 if (dict_value) { | 123 if (dict_value) { |
| 122 last_message_parsed_ = std::move(dict_value); | 124 last_message_parsed_ = std::move(dict_value); |
| 123 } else { | 125 } else { |
| 124 LOG(ERROR) << "Failed to parse " << message; | 126 LOG(ERROR) << "Failed to parse " << message; |
| 125 last_message_parsed_.reset(); | 127 last_message_parsed_.reset(); |
| 126 } | 128 } |
| 127 | 129 |
| 128 if (run_loop_) | 130 if (run_loop_) |
| 129 run_loop_->Quit(); | 131 run_loop_->Quit(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 152 if (bytes_written < 0 || | 154 if (bytes_written < 0 || |
| 153 (message_with_header.size() != static_cast<size_t>(bytes_written))) { | 155 (message_with_header.size() != static_cast<size_t>(bytes_written))) { |
| 154 return base::FilePath(); | 156 return base::FilePath(); |
| 155 } | 157 } |
| 156 return filename; | 158 return filename; |
| 157 } | 159 } |
| 158 | 160 |
| 159 base::ScopedTempDir temp_dir_; | 161 base::ScopedTempDir temp_dir_; |
| 160 // Force the channel to be dev. | 162 // Force the channel to be dev. |
| 161 ScopedCurrentChannel current_channel_; | 163 ScopedCurrentChannel current_channel_; |
| 162 scoped_ptr<NativeMessageHost> native_message_host_; | 164 std::unique_ptr<NativeMessageHost> native_message_host_; |
| 163 scoped_ptr<base::RunLoop> run_loop_; | 165 std::unique_ptr<base::RunLoop> run_loop_; |
| 164 content::TestBrowserThreadBundle thread_bundle_; | 166 content::TestBrowserThreadBundle thread_bundle_; |
| 165 std::string last_message_; | 167 std::string last_message_; |
| 166 scoped_ptr<base::DictionaryValue> last_message_parsed_; | 168 std::unique_ptr<base::DictionaryValue> last_message_parsed_; |
| 167 bool channel_closed_; | 169 bool channel_closed_; |
| 168 }; | 170 }; |
| 169 | 171 |
| 170 // Read a single message from a local file. | 172 // Read a single message from a local file. |
| 171 TEST_F(NativeMessagingTest, SingleSendMessageRead) { | 173 TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
| 172 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 174 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
| 173 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); | 175 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); |
| 174 ASSERT_FALSE(temp_input_file.empty()); | 176 ASSERT_FALSE(temp_input_file.empty()); |
| 175 | 177 |
| 176 scoped_ptr<NativeProcessLauncher> launcher = | 178 std::unique_ptr<NativeProcessLauncher> launcher = |
| 177 FakeLauncher::Create(temp_input_file, temp_output_file); | 179 FakeLauncher::Create(temp_input_file, temp_output_file); |
| 178 native_message_host_ = NativeMessageProcessHost::CreateWithLauncher( | 180 native_message_host_ = NativeMessageProcessHost::CreateWithLauncher( |
| 179 ScopedTestNativeMessagingHost::kExtensionId, "empty_app.py", | 181 ScopedTestNativeMessagingHost::kExtensionId, "empty_app.py", |
| 180 std::move(launcher)); | 182 std::move(launcher)); |
| 181 native_message_host_->Start(this); | 183 native_message_host_->Start(this); |
| 182 ASSERT_TRUE(native_message_host_.get()); | 184 ASSERT_TRUE(native_message_host_.get()); |
| 183 run_loop_.reset(new base::RunLoop()); | 185 run_loop_.reset(new base::RunLoop()); |
| 184 run_loop_->RunUntilIdle(); | 186 run_loop_->RunUntilIdle(); |
| 185 | 187 |
| 186 if (last_message_.empty()) { | 188 if (last_message_.empty()) { |
| 187 run_loop_.reset(new base::RunLoop()); | 189 run_loop_.reset(new base::RunLoop()); |
| 188 scoped_ptr<NativeMessageProcessHost> native_message_process_host_( | 190 std::unique_ptr<NativeMessageProcessHost> native_message_process_host_( |
| 189 static_cast<NativeMessageProcessHost*>(native_message_host_.release())); | 191 static_cast<NativeMessageProcessHost*>(native_message_host_.release())); |
| 190 native_message_process_host_->ReadNowForTesting(); | 192 native_message_process_host_->ReadNowForTesting(); |
| 191 run_loop_->Run(); | 193 run_loop_->Run(); |
| 192 } | 194 } |
| 193 EXPECT_EQ(kTestMessage, last_message_); | 195 EXPECT_EQ(kTestMessage, last_message_); |
| 194 } | 196 } |
| 195 | 197 |
| 196 // Tests sending a single message. The message should get written to | 198 // Tests sending a single message. The message should get written to |
| 197 // |temp_file| and should match the contents of single_message_request.msg. | 199 // |temp_file| and should match the contents of single_message_request.msg. |
| 198 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { | 200 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 214 ASSERT_TRUE(read_handle.IsValid()); | 216 ASSERT_TRUE(read_handle.IsValid()); |
| 215 | 217 |
| 216 read_file = std::move(read_handle); | 218 read_file = std::move(read_handle); |
| 217 #else // defined(OS_WIN) | 219 #else // defined(OS_WIN) |
| 218 base::PlatformFile pipe_handles[2]; | 220 base::PlatformFile pipe_handles[2]; |
| 219 ASSERT_EQ(0, pipe(pipe_handles)); | 221 ASSERT_EQ(0, pipe(pipe_handles)); |
| 220 read_file = base::File(pipe_handles[0]); | 222 read_file = base::File(pipe_handles[0]); |
| 221 base::File write_file(pipe_handles[1]); | 223 base::File write_file(pipe_handles[1]); |
| 222 #endif // !defined(OS_WIN) | 224 #endif // !defined(OS_WIN) |
| 223 | 225 |
| 224 scoped_ptr<NativeProcessLauncher> launcher = | 226 std::unique_ptr<NativeProcessLauncher> launcher = |
| 225 FakeLauncher::CreateWithPipeInput(std::move(read_file), temp_output_file); | 227 FakeLauncher::CreateWithPipeInput(std::move(read_file), temp_output_file); |
| 226 native_message_host_ = NativeMessageProcessHost::CreateWithLauncher( | 228 native_message_host_ = NativeMessageProcessHost::CreateWithLauncher( |
| 227 ScopedTestNativeMessagingHost::kExtensionId, "empty_app.py", | 229 ScopedTestNativeMessagingHost::kExtensionId, "empty_app.py", |
| 228 std::move(launcher)); | 230 std::move(launcher)); |
| 229 native_message_host_->Start(this); | 231 native_message_host_->Start(this); |
| 230 ASSERT_TRUE(native_message_host_.get()); | 232 ASSERT_TRUE(native_message_host_.get()); |
| 231 base::RunLoop().RunUntilIdle(); | 233 base::RunLoop().RunUntilIdle(); |
| 232 | 234 |
| 233 native_message_host_->OnMessage(kTestMessage); | 235 native_message_host_->OnMessage(kTestMessage); |
| 234 base::RunLoop().RunUntilIdle(); | 236 base::RunLoop().RunUntilIdle(); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 native_message_host_->Start(this); | 326 native_message_host_->Start(this); |
| 325 ASSERT_TRUE(native_message_host_.get()); | 327 ASSERT_TRUE(native_message_host_.get()); |
| 326 run_loop_.reset(new base::RunLoop()); | 328 run_loop_.reset(new base::RunLoop()); |
| 327 run_loop_->Run(); | 329 run_loop_->Run(); |
| 328 | 330 |
| 329 // The host should fail to start. | 331 // The host should fail to start. |
| 330 ASSERT_TRUE(channel_closed_); | 332 ASSERT_TRUE(channel_closed_); |
| 331 } | 333 } |
| 332 | 334 |
| 333 } // namespace extensions | 335 } // namespace extensions |
| OLD | NEW |