Index: content/common/common_param_traits2.cc |
=================================================================== |
--- content/common/common_param_traits2.cc (revision 0) |
+++ content/common/common_param_traits2.cc (revision 0) |
@@ -0,0 +1,301 @@ |
+// Copyright (c) 2011 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. |
+ |
+// NOTE: New trait definitions that will only be used by tests (and not by |
+// Chrome Frame) must be placed in common_param_traits.cc. |
+ |
+#include "content/common/common_param_traits.h" |
+ |
+#include "content/common/content_constants.h" |
+#include "net/base/host_port_pair.h" |
+#include "net/base/upload_data.h" |
+#include "ui/gfx/rect.h" |
+ |
+namespace IPC { |
+ |
+void ParamTraits<GURL>::Write(Message* m, const GURL& p) { |
+ m->WriteString(p.possibly_invalid_spec()); |
+ // TODO(brettw) bug 684583: Add encoding for query params. |
+} |
+ |
+bool ParamTraits<GURL>::Read(const Message* m, void** iter, GURL* p) { |
+ std::string s; |
+ if (!m->ReadString(iter, &s) || s.length() > content::kMaxURLChars) { |
+ *p = GURL(); |
+ return false; |
+ } |
+ *p = GURL(s); |
+ return true; |
+} |
+ |
+void ParamTraits<GURL>::Log(const GURL& p, std::string* l) { |
+ l->append(p.spec()); |
+} |
+ |
+void ParamTraits<net::URLRequestStatus>::Write(Message* m, |
+ const param_type& p) { |
+ WriteParam(m, static_cast<int>(p.status())); |
+ WriteParam(m, p.os_error()); |
+} |
+ |
+bool ParamTraits<net::URLRequestStatus>::Read(const Message* m, void** iter, |
+ param_type* r) { |
+ int status, os_error; |
+ if (!ReadParam(m, iter, &status) || |
+ !ReadParam(m, iter, &os_error)) |
+ return false; |
+ r->set_status(static_cast<net::URLRequestStatus::Status>(status)); |
+ r->set_os_error(os_error); |
+ return true; |
+} |
+ |
+void ParamTraits<net::URLRequestStatus>::Log(const param_type& p, |
+ std::string* l) { |
+ std::string status; |
+ switch (p.status()) { |
+ case net::URLRequestStatus::SUCCESS: |
+ status = "SUCCESS"; |
+ break; |
+ case net::URLRequestStatus::IO_PENDING: |
+ status = "IO_PENDING "; |
+ break; |
+ case net::URLRequestStatus::HANDLED_EXTERNALLY: |
+ status = "HANDLED_EXTERNALLY"; |
+ break; |
+ case net::URLRequestStatus::CANCELED: |
+ status = "CANCELED"; |
+ break; |
+ case net::URLRequestStatus::FAILED: |
+ status = "FAILED"; |
+ break; |
+ default: |
+ status = "UNKNOWN"; |
+ break; |
+ } |
+ if (p.status() == net::URLRequestStatus::FAILED) |
+ l->append("("); |
+ |
+ LogParam(status, l); |
+ |
+ if (p.status() == net::URLRequestStatus::FAILED) { |
+ l->append(", "); |
+ LogParam(p.os_error(), l); |
+ l->append(")"); |
+ } |
+} |
+ |
+// Only the net::UploadData ParamTraits<> definition needs this definition, so |
+// keep this in the implementation file so we can forward declare UploadData in |
+// the header. |
+template <> |
+struct ParamTraits<net::UploadData::Element> { |
+ typedef net::UploadData::Element param_type; |
+ static void Write(Message* m, const param_type& p) { |
+ WriteParam(m, static_cast<int>(p.type())); |
+ switch (p.type()) { |
+ case net::UploadData::TYPE_BYTES: { |
+ m->WriteData(&p.bytes()[0], static_cast<int>(p.bytes().size())); |
+ break; |
+ } |
+ case net::UploadData::TYPE_CHUNK: { |
+ std::string chunk_length = StringPrintf( |
+ "%X\r\n", static_cast<unsigned int>(p.bytes().size())); |
+ std::vector<char> bytes; |
+ bytes.insert(bytes.end(), chunk_length.data(), |
+ chunk_length.data() + chunk_length.length()); |
+ const char* data = &p.bytes()[0]; |
+ bytes.insert(bytes.end(), data, data + p.bytes().size()); |
+ const char* crlf = "\r\n"; |
+ bytes.insert(bytes.end(), crlf, crlf + strlen(crlf)); |
+ if (p.is_last_chunk()) { |
+ const char* end_of_data = "0\r\n\r\n"; |
+ bytes.insert(bytes.end(), end_of_data, |
+ end_of_data + strlen(end_of_data)); |
+ } |
+ m->WriteData(&bytes[0], static_cast<int>(bytes.size())); |
+ // If this element is part of a chunk upload then send over information |
+ // indicating if this is the last chunk. |
+ WriteParam(m, p.is_last_chunk()); |
+ break; |
+ } |
+ case net::UploadData::TYPE_FILE: { |
+ WriteParam(m, p.file_path()); |
+ WriteParam(m, p.file_range_offset()); |
+ WriteParam(m, p.file_range_length()); |
+ WriteParam(m, p.expected_file_modification_time()); |
+ break; |
+ } |
+ default: { |
+ WriteParam(m, p.blob_url()); |
+ break; |
+ } |
+ } |
+ } |
+ static bool Read(const Message* m, void** iter, param_type* r) { |
+ int type; |
+ if (!ReadParam(m, iter, &type)) |
+ return false; |
+ switch (type) { |
+ case net::UploadData::TYPE_BYTES: { |
+ const char* data; |
+ int len; |
+ if (!m->ReadData(iter, &data, &len)) |
+ return false; |
+ r->SetToBytes(data, len); |
+ break; |
+ } |
+ case net::UploadData::TYPE_CHUNK: { |
+ const char* data; |
+ int len; |
+ if (!m->ReadData(iter, &data, &len)) |
+ return false; |
+ r->SetToBytes(data, len); |
+ // If this element is part of a chunk upload then we need to explicitly |
+ // set the type of the element and whether it is the last chunk. |
+ bool is_last_chunk = false; |
+ if (!ReadParam(m, iter, &is_last_chunk)) |
+ return false; |
+ r->set_type(net::UploadData::TYPE_CHUNK); |
+ r->set_is_last_chunk(is_last_chunk); |
+ break; |
+ } |
+ case net::UploadData::TYPE_FILE: { |
+ FilePath file_path; |
+ uint64 offset, length; |
+ base::Time expected_modification_time; |
+ if (!ReadParam(m, iter, &file_path)) |
+ return false; |
+ if (!ReadParam(m, iter, &offset)) |
+ return false; |
+ if (!ReadParam(m, iter, &length)) |
+ return false; |
+ if (!ReadParam(m, iter, &expected_modification_time)) |
+ return false; |
+ r->SetToFilePathRange(file_path, offset, length, |
+ expected_modification_time); |
+ break; |
+ } |
+ default: { |
+ DCHECK(type == net::UploadData::TYPE_BLOB); |
+ GURL blob_url; |
+ if (!ReadParam(m, iter, &blob_url)) |
+ return false; |
+ r->SetToBlobUrl(blob_url); |
+ break; |
+ } |
+ } |
+ return true; |
+ } |
+ static void Log(const param_type& p, std::string* l) { |
+ l->append("<net::UploadData::Element>"); |
+ } |
+}; |
+ |
+void ParamTraits<scoped_refptr<net::UploadData> >::Write(Message* m, |
+ const param_type& p) { |
+ WriteParam(m, p.get() != NULL); |
+ if (p) { |
+ WriteParam(m, *p->elements()); |
+ WriteParam(m, p->identifier()); |
+ WriteParam(m, p->is_chunked()); |
+ } |
+} |
+ |
+bool ParamTraits<scoped_refptr<net::UploadData> >::Read(const Message* m, |
+ void** iter, |
+ param_type* r) { |
+ bool has_object; |
+ if (!ReadParam(m, iter, &has_object)) |
+ return false; |
+ if (!has_object) |
+ return true; |
+ std::vector<net::UploadData::Element> elements; |
+ if (!ReadParam(m, iter, &elements)) |
+ return false; |
+ int64 identifier; |
+ if (!ReadParam(m, iter, &identifier)) |
+ return false; |
+ bool is_chunked = false; |
+ if (!ReadParam(m, iter, &is_chunked)) |
+ return false; |
+ *r = new net::UploadData; |
+ (*r)->swap_elements(&elements); |
+ (*r)->set_identifier(identifier); |
+ (*r)->set_is_chunked(is_chunked); |
+ return true; |
+} |
+ |
+void ParamTraits<scoped_refptr<net::UploadData> >::Log(const param_type& p, |
+ std::string* l) { |
+ l->append("<net::UploadData>"); |
+} |
+ |
+void ParamTraits<net::HostPortPair>::Write(Message* m, const param_type& p) { |
+ WriteParam(m, p.host()); |
+ WriteParam(m, p.port()); |
+} |
+ |
+bool ParamTraits<net::HostPortPair>::Read(const Message* m, void** iter, |
+ param_type* r) { |
+ std::string host; |
+ uint16 port; |
+ if (!ReadParam(m, iter, &host) || !ReadParam(m, iter, &port)) |
+ return false; |
+ |
+ r->set_host(host); |
+ r->set_port(port); |
+ return true; |
+} |
+ |
+void ParamTraits<net::HostPortPair>::Log(const param_type& p, std::string* l) { |
+ l->append(p.ToString()); |
+} |
+ |
+void ParamTraits<gfx::Point>::Write(Message* m, const gfx::Point& p) { |
+ m->WriteInt(p.x()); |
+ m->WriteInt(p.y()); |
+} |
+ |
+bool ParamTraits<gfx::Point>::Read(const Message* m, void** iter, |
+ gfx::Point* r) { |
+ int x, y; |
+ if (!m->ReadInt(iter, &x) || |
+ !m->ReadInt(iter, &y)) |
+ return false; |
+ r->set_x(x); |
+ r->set_y(y); |
+ return true; |
+} |
+ |
+void ParamTraits<gfx::Point>::Log(const gfx::Point& p, std::string* l) { |
+ l->append(base::StringPrintf("(%d, %d)", p.x(), p.y())); |
+} |
+ |
+void ParamTraits<gfx::Rect>::Write(Message* m, const gfx::Rect& p) { |
+ m->WriteInt(p.x()); |
+ m->WriteInt(p.y()); |
+ m->WriteInt(p.width()); |
+ m->WriteInt(p.height()); |
+} |
+ |
+bool ParamTraits<gfx::Rect>::Read(const Message* m, void** iter, gfx::Rect* r) { |
+ int x, y, w, h; |
+ if (!m->ReadInt(iter, &x) || |
+ !m->ReadInt(iter, &y) || |
+ !m->ReadInt(iter, &w) || |
+ !m->ReadInt(iter, &h)) |
+ return false; |
+ r->set_x(x); |
+ r->set_y(y); |
+ r->set_width(w); |
+ r->set_height(h); |
+ return true; |
+} |
+ |
+void ParamTraits<gfx::Rect>::Log(const gfx::Rect& p, std::string* l) { |
+ l->append(base::StringPrintf("(%d, %d, %d, %d)", p.x(), p.y(), |
+ p.width(), p.height())); |
+} |
+ |
+} // namespace IPC |
Property changes on: content\common\common_param_traits2.cc |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |