native_message_port.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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 3// found in the LICENSE file. 4 5#include "chrome/browser/extensions/api/messaging/native_message_port.h" 6 7#include "base/bind.h" 8#include "base/json/json_writer.h" 9#include "chrome/browser/extensions/api/messaging/native_message_process_host.h" 10#include "content/public/browser/browser_thread.h" 11 12namespace extensions { 13 14NativeMessagePort::NativeMessagePort(NativeMessageProcessHost* native_process) 15 : native_process_(native_process) { 16} 17 18NativeMessagePort::~NativeMessagePort() { 19 content::BrowserThread::DeleteSoon( 20 content::BrowserThread::IO, FROM_HERE, native_process_); 21} 22 23void NativeMessagePort::DispatchOnMessage(scoped_ptr<base::ListValue> message, 24 int target_port_id) { 25 std::string message_as_json; 26 if (!message->empty()) { 27 DCHECK_EQ(1u, message->GetSize()); 28 base::Value* value = NULL; 29 message->Get(0, &value); 30 base::JSONWriter::Write(value, &message_as_json); 31 } 32 content::BrowserThread::PostTask( 33 content::BrowserThread::IO, FROM_HERE, 34 base::Bind(&NativeMessageProcessHost::Send, 35 base::Unretained(native_process_), 36 message_as_json)); 37} 38 39} // namespace extensions 40