|
Line 0
a/Source/WebKit2/Platform/CoreIPC/qt/ConnectionSymbian.cpp_sec1
|
|
|
1 |
#include "config.h" |
| 2 |
#if PLATFORM(QT) && OS(SYMBIAN) |
| 3 |
#include "Connection.h" |
| 4 |
|
| 5 |
#include "ArgumentEncoder.h" |
| 6 |
#include "WorkItem.h" |
| 7 |
#include <QVector> |
| 8 |
#include <qt/QSymbianPipeNotifier.h> |
| 9 |
#include <wtf/Assertions.h> |
| 10 |
namespace CoreIPC { |
| 11 |
|
| 12 |
static const uint32_t trailer = 0xDEADCAFE; |
| 13 |
|
| 14 |
void Connection::platformInitialize(Identifier identifier) |
| 15 |
{ |
| 16 |
|
| 17 |
if (m_isServer) { |
| 18 |
m_readPipe = static_cast<RPipe*>(identifier)[0]; |
| 19 |
m_writePipe = static_cast<RPipe*>(identifier)[1]; |
| 20 |
} else { |
| 21 |
TInt error; |
| 22 |
// Open pipes via RProcess' "environment data slots" number 3 & 4 |
| 23 |
// This is the Symbian OS way of passing handles from parent to child |
| 24 |
error = m_readPipe.Open(3); |
| 25 |
error|= m_writePipe.Open(4); |
| 26 |
if (error) { |
| 27 |
m_isConnected = false; |
| 28 |
return; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
m_readBuffer.resize(CoreIPC::Connection::MaxPipeCapacity); |
| 33 |
m_isConnected = true; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
void Connection::platformInvalidate() |
| 38 |
{ |
| 39 |
m_readBuffer.shrink(0); |
| 40 |
|
| 41 |
delete m_pipeReadNotifier; |
| 42 |
m_readPipe.Close(); |
| 43 |
m_writePipe.Close(); |
| 44 |
// TODO: delete the memory for the RPipe[2] array |
| 45 |
m_isConnected = false; |
| 46 |
} |
| 47 |
|
| 48 |
// Check if the data matches the trailer pattern |
| 49 |
static inline bool isMessageTrailer(uint8_t *data) |
| 50 |
{ |
| 51 |
return ( trailer == *(reinterpret_cast<uint32_t*>(data)) ); |
| 52 |
} |
| 53 |
|
| 54 |
// Handler for "bytes available to read" event on pipe |
| 55 |
void Connection::readyReadHandler() |
| 56 |
{ |
| 57 |
RProcess proc; |
| 58 |
|
| 59 |
m_pipeReadNotifier->setEnabled(false); |
| 60 |
|
| 61 |
// Make a temporary Symbian descriptor to point to existing buffer, with 0 current size, and a max size |
| 62 |
TPtr8 readPtr(m_readBuffer.data(), 0, m_readBuffer.size()); |
| 63 |
|
| 64 |
// Read all the bytes we can. Note that multiple messages can be read in one read operation due to the stream-oriented channel |
| 65 |
TInt bytesRead = m_readPipe.Read(readPtr, readPtr.MaxSize()); |
| 66 |
|
| 67 |
if (bytesRead > 0) { |
| 68 |
|
| 69 |
__ASSERT_ALWAYS(bytesRead == readPtr.Size(), User::Invariant()); |
| 70 |
|
| 71 |
// Start parsing from the end of the buffer, where we expect to see a trailer and the size of this message |
| 72 |
uint8_t* messageStart = m_readBuffer.data() + bytesRead; |
| 73 |
|
| 74 |
// Place to keep track of parsed messages |
| 75 |
QVector<ParsedMessage> messsagePointers; |
| 76 |
|
| 77 |
while (true) { |
| 78 |
|
| 79 |
messageStart -= sizeof(uint32_t); // move back pointer to read trailer |
| 80 |
|
| 81 |
if (!isMessageTrailer(messageStart)) // bail if no trailer |
| 82 |
break; |
| 83 |
|
| 84 |
messageStart -= sizeof(uint32_t); // move back pointer to read messageSize |
| 85 |
uint32_t messageSize = *reinterpret_cast<uint32_t*>(messageStart); |
| 86 |
|
| 87 |
messageStart -= sizeof(uint32_t); // move back to read messageID |
| 88 |
uint32_t messageID = *reinterpret_cast<uint32_t*>(messageStart); |
| 89 |
|
| 90 |
messageStart -= (messageSize - sizeof(uint32_t)); |
| 91 |
|
| 92 |
// Keep a note of message boundary |
| 93 |
const ParsedMessage aMessage = {messageStart, messageSize - sizeof(uint32_t), messageID}; |
| 94 |
messsagePointers.append(aMessage); |
| 95 |
|
| 96 |
__ASSERT_ALWAYS(messageStart >= m_readBuffer.data(), User::Invariant()); |
| 97 |
|
| 98 |
if (messageStart == m_readBuffer.data()) |
| 99 |
break; |
| 100 |
|
| 101 |
} // while loop |
| 102 |
|
| 103 |
// Now dispatch parsed messages in the order they were written by the sender |
| 104 |
for (int i = messsagePointers.size()-1; i >=0 ; i--) { |
| 105 |
const ParsedMessage aMessage = messsagePointers.at(i); |
| 106 |
processIncomingMessage(MessageID::fromInt(aMessage.messageID), adoptPtr(new ArgumentDecoder(aMessage.start, aMessage.size))); |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
m_pipeReadNotifier->setEnabled(true); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
bool Connection::open() |
| 117 |
{ |
| 118 |
// platformInitialize() should have taken care of the connection by now |
| 119 |
if (!m_isConnected) |
| 120 |
return false; |
| 121 |
|
| 122 |
// Start listening for read |
| 123 |
m_pipeReadNotifier = m_connectionQueue.registerPipeEventHandler(m_readPipe, QSymbianPipeNotifier::PipeReadDataAvailable, WorkItem::create(this, &Connection::readyReadHandler)); |
| 124 |
|
| 125 |
// Schedule a read. |
| 126 |
m_connectionQueue.scheduleWork(WorkItem::create(this, &Connection::readyReadHandler)); |
| 127 |
|
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
bool Connection::platformCanSendOutgoingMessages() const |
| 132 |
{ |
| 133 |
// Seems adequate for now |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
bool Connection::sendOutgoingMessage(MessageID messageID, PassOwnPtr<ArgumentEncoder> arguments) |
| 138 |
{ |
| 139 |
// Encode messageID. |
| 140 |
arguments->encodeUInt32(messageID.toInt()); |
| 141 |
|
| 142 |
// Encode size of message excluding this 4-byte field and the 4-byte trailer which follows. |
| 143 |
arguments->encodeUInt32(arguments->bufferSize()); |
| 144 |
|
| 145 |
// a trailer marker (magic number) which makes it easier to spot mesage boundary at the receiver |
| 146 |
arguments->encodeUInt32(trailer); |
| 147 |
|
| 148 |
// Write to pipe using a temporary Symbian descriptor |
| 149 |
TPtr8 writeBuffer(arguments->buffer(), arguments->bufferSize(), arguments->bufferSize() ); |
| 150 |
TInt bytesWritten = m_writePipe.Write(writeBuffer, writeBuffer.Size() ); |
| 151 |
|
| 152 |
return (bytesWritten == writeBuffer.Size()); |
| 153 |
} |
| 154 |
|
| 155 |
} // namespace CoreIPC |
| 156 |
#endif |