ipc_message_utils.cc 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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. #include "ipc/ipc_message_utils.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "base/files/file_path.h"
  8. #include "base/json/json_writer.h"
  9. #include "base/logging.h"
  10. #include "base/memory/ptr_util.h"
  11. #include "base/notreached.h"
  12. #include "base/strings/string_number_conversions.h"
  13. #include "base/strings/stringprintf.h"
  14. #include "base/strings/utf_string_conversions.h"
  15. #include "base/time/time.h"
  16. #include "base/unguessable_token.h"
  17. #include "build/build_config.h"
  18. #include "ipc/ipc_channel_handle.h"
  19. #include "ipc/ipc_message.h"
  20. #include "ipc/ipc_message_attachment.h"
  21. #include "ipc/ipc_message_attachment_set.h"
  22. #include "ipc/ipc_mojo_param_traits.h"
  23. #if BUILDFLAG(IS_APPLE)
  24. #include "ipc/mach_port_mac.h"
  25. #endif
  26. #if BUILDFLAG(IS_WIN)
  27. #include <tchar.h>
  28. #include "ipc/handle_win.h"
  29. #include "ipc/ipc_platform_file.h"
  30. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  31. #include "base/file_descriptor_posix.h"
  32. #include "ipc/ipc_platform_file_attachment_posix.h"
  33. #endif
  34. #if BUILDFLAG(IS_FUCHSIA)
  35. #include "base/fuchsia/fuchsia_logging.h"
  36. #include "ipc/handle_attachment_fuchsia.h"
  37. #endif
  38. #if BUILDFLAG(IS_ANDROID)
  39. #include "base/android/scoped_hardware_buffer_handle.h"
  40. #include "ipc/ipc_mojo_handle_attachment.h"
  41. #include "mojo/public/cpp/system/message_pipe.h"
  42. #include "mojo/public/cpp/system/scope_to_message_pipe.h"
  43. #endif
  44. namespace IPC {
  45. namespace {
  46. const int kMaxRecursionDepth = 200;
  47. template<typename CharType>
  48. void LogBytes(const std::vector<CharType>& data, std::string* out) {
  49. #if BUILDFLAG(IS_WIN)
  50. // Windows has a GUI for logging, which can handle arbitrary binary data.
  51. for (size_t i = 0; i < data.size(); ++i)
  52. out->push_back(data[i]);
  53. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  54. // On POSIX, we log to stdout, which we assume can display ASCII.
  55. static const size_t kMaxBytesToLog = 100;
  56. for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) {
  57. if (isprint(data[i]))
  58. out->push_back(data[i]);
  59. else
  60. out->append(
  61. base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i])));
  62. }
  63. if (data.size() > kMaxBytesToLog) {
  64. out->append(base::StringPrintf(
  65. " and %u more bytes",
  66. static_cast<unsigned>(data.size() - kMaxBytesToLog)));
  67. }
  68. #endif
  69. }
  70. void WriteValue(const base::Value& value, int recursion, base::Pickle* pickle);
  71. void WriteDictValue(const base::Value::Dict& value,
  72. int recursion,
  73. base::Pickle* pickle) {
  74. WriteParam(pickle, base::checked_cast<int>(value.size()));
  75. for (const auto entry : value) {
  76. WriteParam(pickle, entry.first);
  77. WriteValue(entry.second, recursion + 1, pickle);
  78. }
  79. }
  80. void WriteListValue(const base::Value::List& value,
  81. int recursion,
  82. base::Pickle* pickle) {
  83. WriteParam(pickle, base::checked_cast<int>(value.size()));
  84. for (const auto& entry : value) {
  85. WriteValue(entry, recursion + 1, pickle);
  86. }
  87. }
  88. void WriteValue(const base::Value& value, int recursion, base::Pickle* pickle) {
  89. bool result;
  90. if (recursion > kMaxRecursionDepth) {
  91. LOG(ERROR) << "Max recursion depth hit in WriteValue.";
  92. return;
  93. }
  94. pickle->WriteInt(static_cast<int>(value.type()));
  95. switch (value.type()) {
  96. case base::Value::Type::NONE:
  97. break;
  98. case base::Value::Type::BOOLEAN: {
  99. WriteParam(pickle, value.GetBool());
  100. break;
  101. }
  102. case base::Value::Type::INTEGER: {
  103. DCHECK(value.is_int());
  104. WriteParam(pickle, value.GetInt());
  105. break;
  106. }
  107. case base::Value::Type::DOUBLE: {
  108. DCHECK(value.is_int() || value.is_double());
  109. WriteParam(pickle, value.GetDouble());
  110. break;
  111. }
  112. case base::Value::Type::STRING: {
  113. const std::string* val = value.GetIfString();
  114. result = !!val;
  115. DCHECK(result);
  116. WriteParam(pickle, *val);
  117. break;
  118. }
  119. case base::Value::Type::BINARY: {
  120. pickle->WriteData(reinterpret_cast<const char*>(value.GetBlob().data()),
  121. value.GetBlob().size());
  122. break;
  123. }
  124. case base::Value::Type::DICT: {
  125. DCHECK(value.is_dict());
  126. WriteDictValue(value.GetDict(), recursion, pickle);
  127. break;
  128. }
  129. case base::Value::Type::LIST: {
  130. DCHECK(value.is_list());
  131. WriteListValue(value.GetList(), recursion, pickle);
  132. break;
  133. }
  134. }
  135. }
  136. bool ReadValue(const base::Pickle* pickle,
  137. base::PickleIterator* iter,
  138. int recursion,
  139. base::Value* value);
  140. // Helper for ReadValue that reads a Value::Dict into a pre-allocated object.
  141. bool ReadDictValue(const base::Pickle* pickle,
  142. base::PickleIterator* iter,
  143. int recursion,
  144. base::Value::Dict* value) {
  145. int size;
  146. if (!ReadParam(pickle, iter, &size))
  147. return false;
  148. for (int i = 0; i < size; i++) {
  149. std::string key;
  150. base::Value subvalue;
  151. if (!ReadParam(pickle, iter, &key) ||
  152. !ReadValue(pickle, iter, recursion + 1, &subvalue)) {
  153. return false;
  154. }
  155. value->Set(key, std::move(subvalue));
  156. }
  157. return true;
  158. }
  159. // Helper for ReadValue that reads a Value::List into a pre-allocated object.
  160. bool ReadListValue(const base::Pickle* pickle,
  161. base::PickleIterator* iter,
  162. int recursion,
  163. base::Value::List* value) {
  164. int size;
  165. if (!ReadParam(pickle, iter, &size))
  166. return false;
  167. value->reserve(size);
  168. for (int i = 0; i < size; i++) {
  169. base::Value subval;
  170. if (!ReadValue(pickle, iter, recursion + 1, &subval))
  171. return false;
  172. value->Append(std::move(subval));
  173. }
  174. return true;
  175. }
  176. bool ReadValue(const base::Pickle* pickle,
  177. base::PickleIterator* iter,
  178. int recursion,
  179. base::Value* value) {
  180. if (recursion > kMaxRecursionDepth) {
  181. LOG(ERROR) << "Max recursion depth hit in ReadValue.";
  182. return false;
  183. }
  184. int type;
  185. if (!ReadParam(pickle, iter, &type))
  186. return false;
  187. constexpr int kMinValueType = static_cast<int>(base::Value::Type::NONE);
  188. constexpr int kMaxValueType = static_cast<int>(base::Value::Type::LIST);
  189. if (type > kMaxValueType || type < kMinValueType)
  190. return false;
  191. switch (static_cast<base::Value::Type>(type)) {
  192. case base::Value::Type::NONE:
  193. *value = base::Value();
  194. break;
  195. case base::Value::Type::BOOLEAN: {
  196. bool val;
  197. if (!ReadParam(pickle, iter, &val))
  198. return false;
  199. *value = base::Value(val);
  200. break;
  201. }
  202. case base::Value::Type::INTEGER: {
  203. int val;
  204. if (!ReadParam(pickle, iter, &val))
  205. return false;
  206. *value = base::Value(val);
  207. break;
  208. }
  209. case base::Value::Type::DOUBLE: {
  210. double val;
  211. if (!ReadParam(pickle, iter, &val))
  212. return false;
  213. *value = base::Value(val);
  214. break;
  215. }
  216. case base::Value::Type::STRING: {
  217. std::string val;
  218. if (!ReadParam(pickle, iter, &val))
  219. return false;
  220. *value = base::Value(std::move(val));
  221. break;
  222. }
  223. case base::Value::Type::BINARY: {
  224. base::span<const uint8_t> data;
  225. if (!iter->ReadData(&data))
  226. return false;
  227. *value = base::Value(data);
  228. break;
  229. }
  230. case base::Value::Type::DICTIONARY: {
  231. base::Value::Dict val;
  232. if (!ReadDictValue(pickle, iter, recursion, &val))
  233. return false;
  234. *value = base::Value(std::move(val));
  235. break;
  236. }
  237. case base::Value::Type::LIST: {
  238. base::Value::List val;
  239. if (!ReadListValue(pickle, iter, recursion, &val))
  240. return false;
  241. *value = base::Value(std::move(val));
  242. break;
  243. }
  244. default:
  245. NOTREACHED();
  246. return false;
  247. }
  248. return true;
  249. }
  250. } // namespace
  251. // -----------------------------------------------------------------------------
  252. LogData::LogData()
  253. : routing_id(0),
  254. type(0),
  255. sent(0),
  256. receive(0),
  257. dispatch(0) {
  258. }
  259. LogData::LogData(const LogData& other) = default;
  260. LogData::~LogData() = default;
  261. void ParamTraits<bool>::Log(const param_type& p, std::string* l) {
  262. l->append(p ? "true" : "false");
  263. }
  264. void ParamTraits<signed char>::Write(base::Pickle* m, const param_type& p) {
  265. m->WriteBytes(&p, sizeof(param_type));
  266. }
  267. bool ParamTraits<signed char>::Read(const base::Pickle* m,
  268. base::PickleIterator* iter,
  269. param_type* r) {
  270. const char* data;
  271. if (!iter->ReadBytes(&data, sizeof(param_type)))
  272. return false;
  273. memcpy(r, data, sizeof(param_type));
  274. return true;
  275. }
  276. void ParamTraits<signed char>::Log(const param_type& p, std::string* l) {
  277. l->append(base::NumberToString(p));
  278. }
  279. void ParamTraits<unsigned char>::Write(base::Pickle* m, const param_type& p) {
  280. m->WriteBytes(&p, sizeof(param_type));
  281. }
  282. bool ParamTraits<unsigned char>::Read(const base::Pickle* m,
  283. base::PickleIterator* iter,
  284. param_type* r) {
  285. const char* data;
  286. if (!iter->ReadBytes(&data, sizeof(param_type)))
  287. return false;
  288. memcpy(r, data, sizeof(param_type));
  289. return true;
  290. }
  291. void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) {
  292. l->append(base::NumberToString(p));
  293. }
  294. void ParamTraits<unsigned short>::Write(base::Pickle* m, const param_type& p) {
  295. m->WriteBytes(&p, sizeof(param_type));
  296. }
  297. bool ParamTraits<unsigned short>::Read(const base::Pickle* m,
  298. base::PickleIterator* iter,
  299. param_type* r) {
  300. const char* data;
  301. if (!iter->ReadBytes(&data, sizeof(param_type)))
  302. return false;
  303. memcpy(r, data, sizeof(param_type));
  304. return true;
  305. }
  306. void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) {
  307. l->append(base::NumberToString(p));
  308. }
  309. void ParamTraits<int>::Log(const param_type& p, std::string* l) {
  310. l->append(base::NumberToString(p));
  311. }
  312. void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) {
  313. l->append(base::NumberToString(p));
  314. }
  315. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
  316. BUILDFLAG(IS_FUCHSIA) || \
  317. (BUILDFLAG(IS_ANDROID) && defined(ARCH_CPU_64_BITS))
  318. void ParamTraits<long>::Log(const param_type& p, std::string* l) {
  319. l->append(base::NumberToString(p));
  320. }
  321. void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) {
  322. l->append(base::NumberToString(p));
  323. }
  324. #endif
  325. void ParamTraits<long long>::Log(const param_type& p, std::string* l) {
  326. l->append(base::NumberToString(p));
  327. }
  328. void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) {
  329. l->append(base::NumberToString(p));
  330. }
  331. void ParamTraits<float>::Log(const param_type& p, std::string* l) {
  332. l->append(base::StringPrintf("%e", p));
  333. }
  334. void ParamTraits<double>::Write(base::Pickle* m, const param_type& p) {
  335. m->WriteBytes(reinterpret_cast<const char*>(&p), sizeof(param_type));
  336. }
  337. bool ParamTraits<double>::Read(const base::Pickle* m,
  338. base::PickleIterator* iter,
  339. param_type* r) {
  340. const char *data;
  341. if (!iter->ReadBytes(&data, sizeof(*r))) {
  342. NOTREACHED();
  343. return false;
  344. }
  345. memcpy(r, data, sizeof(param_type));
  346. return true;
  347. }
  348. void ParamTraits<double>::Log(const param_type& p, std::string* l) {
  349. l->append(base::StringPrintf("%e", p));
  350. }
  351. void ParamTraits<std::string>::Log(const param_type& p, std::string* l) {
  352. l->append(p);
  353. }
  354. void ParamTraits<std::u16string>::Log(const param_type& p, std::string* l) {
  355. l->append(base::UTF16ToUTF8(p));
  356. }
  357. #if BUILDFLAG(IS_WIN)
  358. bool ParamTraits<std::wstring>::Read(const base::Pickle* m,
  359. base::PickleIterator* iter,
  360. param_type* r) {
  361. base::StringPiece16 piece16;
  362. if (!iter->ReadStringPiece16(&piece16))
  363. return false;
  364. *r = base::AsWString(piece16);
  365. return true;
  366. }
  367. void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) {
  368. l->append(base::WideToUTF8(p));
  369. }
  370. #endif
  371. void ParamTraits<std::vector<char>>::Write(base::Pickle* m,
  372. const param_type& p) {
  373. if (p.empty()) {
  374. m->WriteData(NULL, 0);
  375. } else {
  376. m->WriteData(&p.front(), p.size());
  377. }
  378. }
  379. bool ParamTraits<std::vector<char>>::Read(const base::Pickle* m,
  380. base::PickleIterator* iter,
  381. param_type* r) {
  382. const char *data;
  383. size_t data_size = 0;
  384. if (!iter->ReadData(&data, &data_size))
  385. return false;
  386. r->resize(data_size);
  387. if (data_size)
  388. memcpy(&r->front(), data, data_size);
  389. return true;
  390. }
  391. void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) {
  392. LogBytes(p, l);
  393. }
  394. void ParamTraits<std::vector<unsigned char>>::Write(base::Pickle* m,
  395. const param_type& p) {
  396. if (p.empty()) {
  397. m->WriteData(NULL, 0);
  398. } else {
  399. m->WriteData(reinterpret_cast<const char*>(&p.front()), p.size());
  400. }
  401. }
  402. bool ParamTraits<std::vector<unsigned char>>::Read(const base::Pickle* m,
  403. base::PickleIterator* iter,
  404. param_type* r) {
  405. const char *data;
  406. size_t data_size = 0;
  407. if (!iter->ReadData(&data, &data_size))
  408. return false;
  409. r->resize(data_size);
  410. if (data_size)
  411. memcpy(&r->front(), data, data_size);
  412. return true;
  413. }
  414. void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
  415. std::string* l) {
  416. LogBytes(p, l);
  417. }
  418. void ParamTraits<std::vector<bool>>::Write(base::Pickle* m,
  419. const param_type& p) {
  420. WriteParam(m, base::checked_cast<int>(p.size()));
  421. // Cast to bool below is required because libc++'s
  422. // vector<bool>::const_reference is different from bool, and we want to avoid
  423. // writing an extra specialization of ParamTraits for it.
  424. for (size_t i = 0; i < p.size(); i++)
  425. WriteParam(m, static_cast<bool>(p[i]));
  426. }
  427. bool ParamTraits<std::vector<bool>>::Read(const base::Pickle* m,
  428. base::PickleIterator* iter,
  429. param_type* r) {
  430. size_t size;
  431. if (!iter->ReadLength(&size))
  432. return false;
  433. r->resize(size);
  434. for (size_t i = 0; i < size; i++) {
  435. bool value;
  436. if (!ReadParam(m, iter, &value))
  437. return false;
  438. (*r)[i] = value;
  439. }
  440. return true;
  441. }
  442. void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) {
  443. for (size_t i = 0; i < p.size(); ++i) {
  444. if (i != 0)
  445. l->push_back(' ');
  446. LogParam(static_cast<bool>(p[i]), l);
  447. }
  448. }
  449. void ParamTraits<base::DictionaryValue>::Write(base::Pickle* m,
  450. const param_type& p) {
  451. WriteDictValue(p.GetDict(), 0, m);
  452. }
  453. bool ParamTraits<base::DictionaryValue>::Read(const base::Pickle* m,
  454. base::PickleIterator* iter,
  455. param_type* r) {
  456. return ReadDictValue(m, iter, 0, &(r->GetDict()));
  457. }
  458. void ParamTraits<base::DictionaryValue>::Log(const param_type& p,
  459. std::string* l) {
  460. std::string json;
  461. base::JSONWriter::Write(p, &json);
  462. l->append(json);
  463. }
  464. void ParamTraits<base::Value::Dict>::Write(base::Pickle* m,
  465. const param_type& p) {
  466. WriteDictValue(p, 0, m);
  467. }
  468. bool ParamTraits<base::Value::Dict>::Read(const base::Pickle* m,
  469. base::PickleIterator* iter,
  470. param_type* r) {
  471. return ReadDictValue(m, iter, 0, r);
  472. }
  473. void ParamTraits<base::Value::Dict>::Log(const param_type& p, std::string* l) {
  474. std::string json;
  475. base::JSONWriter::Write(p, &json);
  476. l->append(json);
  477. }
  478. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  479. void ParamTraits<base::FileDescriptor>::Write(base::Pickle* m,
  480. const param_type& p) {
  481. // This serialization must be kept in sync with
  482. // nacl_message_scanner.cc:WriteHandle().
  483. const bool valid = p.fd >= 0;
  484. WriteParam(m, valid);
  485. if (!valid)
  486. return;
  487. if (p.auto_close) {
  488. if (!m->WriteAttachment(
  489. new internal::PlatformFileAttachment(base::ScopedFD(p.fd))))
  490. NOTREACHED();
  491. } else {
  492. if (!m->WriteAttachment(new internal::PlatformFileAttachment(p.fd)))
  493. NOTREACHED();
  494. }
  495. }
  496. bool ParamTraits<base::FileDescriptor>::Read(const base::Pickle* m,
  497. base::PickleIterator* iter,
  498. param_type* r) {
  499. *r = base::FileDescriptor();
  500. bool valid;
  501. if (!ReadParam(m, iter, &valid))
  502. return false;
  503. if (!valid)
  504. return true;
  505. scoped_refptr<base::Pickle::Attachment> attachment;
  506. if (!m->ReadAttachment(iter, &attachment))
  507. return false;
  508. if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
  509. MessageAttachment::Type::PLATFORM_FILE) {
  510. return false;
  511. }
  512. *r = base::FileDescriptor(
  513. static_cast<internal::PlatformFileAttachment*>(attachment.get())
  514. ->TakePlatformFile(),
  515. true);
  516. return true;
  517. }
  518. void ParamTraits<base::FileDescriptor>::Log(const param_type& p,
  519. std::string* l) {
  520. if (p.auto_close) {
  521. l->append(base::StringPrintf("FD(%d auto-close)", p.fd));
  522. } else {
  523. l->append(base::StringPrintf("FD(%d)", p.fd));
  524. }
  525. }
  526. void ParamTraits<base::ScopedFD>::Write(base::Pickle* m, const param_type& p) {
  527. // This serialization must be kept in sync with
  528. // nacl_message_scanner.cc:WriteHandle().
  529. const bool valid = p.is_valid();
  530. WriteParam(m, valid);
  531. if (!valid)
  532. return;
  533. if (!m->WriteAttachment(new internal::PlatformFileAttachment(
  534. std::move(const_cast<param_type&>(p))))) {
  535. NOTREACHED();
  536. }
  537. }
  538. bool ParamTraits<base::ScopedFD>::Read(const base::Pickle* m,
  539. base::PickleIterator* iter,
  540. param_type* r) {
  541. r->reset();
  542. bool valid;
  543. if (!ReadParam(m, iter, &valid))
  544. return false;
  545. if (!valid)
  546. return true;
  547. scoped_refptr<base::Pickle::Attachment> attachment;
  548. if (!m->ReadAttachment(iter, &attachment))
  549. return false;
  550. if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
  551. MessageAttachment::Type::PLATFORM_FILE) {
  552. return false;
  553. }
  554. *r = base::ScopedFD(
  555. static_cast<internal::PlatformFileAttachment*>(attachment.get())
  556. ->TakePlatformFile());
  557. return true;
  558. }
  559. void ParamTraits<base::ScopedFD>::Log(const param_type& p, std::string* l) {
  560. l->append(base::StringPrintf("ScopedFD(%d)", p.get()));
  561. }
  562. #endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  563. #if BUILDFLAG(IS_WIN)
  564. void ParamTraits<base::win::ScopedHandle>::Write(base::Pickle* m,
  565. const param_type& p) {
  566. const bool valid = p.IsValid();
  567. WriteParam(m, valid);
  568. if (!valid)
  569. return;
  570. HandleWin handle(p.Get());
  571. WriteParam(m, handle);
  572. }
  573. bool ParamTraits<base::win::ScopedHandle>::Read(const base::Pickle* m,
  574. base::PickleIterator* iter,
  575. param_type* r) {
  576. r->Close();
  577. bool valid;
  578. if (!ReadParam(m, iter, &valid))
  579. return false;
  580. if (!valid)
  581. return true;
  582. HandleWin handle;
  583. if (!ReadParam(m, iter, &handle))
  584. return false;
  585. r->Set(handle.get_handle());
  586. return true;
  587. }
  588. void ParamTraits<base::win::ScopedHandle>::Log(const param_type& p,
  589. std::string* l) {
  590. l->append(base::StringPrintf("ScopedHandle(%p)", p.Get()));
  591. }
  592. #endif // BUILDFLAG(IS_WIN)
  593. #if BUILDFLAG(IS_FUCHSIA)
  594. void ParamTraits<zx::vmo>::Write(base::Pickle* m, const param_type& p) {
  595. // This serialization must be kept in sync with
  596. // nacl_message_scanner.cc:WriteHandle().
  597. const bool valid = p.is_valid();
  598. WriteParam(m, valid);
  599. if (!valid)
  600. return;
  601. if (!m->WriteAttachment(new internal::HandleAttachmentFuchsia(
  602. std::move(const_cast<param_type&>(p))))) {
  603. NOTREACHED();
  604. }
  605. }
  606. bool ParamTraits<zx::vmo>::Read(const base::Pickle* m,
  607. base::PickleIterator* iter,
  608. param_type* r) {
  609. r->reset();
  610. bool valid;
  611. if (!ReadParam(m, iter, &valid))
  612. return false;
  613. if (!valid)
  614. return true;
  615. scoped_refptr<base::Pickle::Attachment> attachment;
  616. if (!m->ReadAttachment(iter, &attachment))
  617. return false;
  618. if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
  619. MessageAttachment::Type::FUCHSIA_HANDLE) {
  620. return false;
  621. }
  622. *r = zx::vmo(static_cast<internal::HandleAttachmentFuchsia*>(attachment.get())
  623. ->Take());
  624. return true;
  625. }
  626. void ParamTraits<zx::vmo>::Log(const param_type& p, std::string* l) {
  627. l->append("ZirconVMO");
  628. }
  629. void ParamTraits<zx::channel>::Write(base::Pickle* m, const param_type& p) {
  630. // This serialization must be kept in sync with
  631. // nacl_message_scanner.cc:WriteHandle().
  632. const bool valid = p.is_valid();
  633. WriteParam(m, valid);
  634. if (!valid)
  635. return;
  636. if (!m->WriteAttachment(new internal::HandleAttachmentFuchsia(
  637. std::move(const_cast<param_type&>(p))))) {
  638. NOTREACHED();
  639. }
  640. }
  641. bool ParamTraits<zx::channel>::Read(const base::Pickle* m,
  642. base::PickleIterator* iter,
  643. param_type* r) {
  644. r->reset();
  645. bool valid;
  646. if (!ReadParam(m, iter, &valid))
  647. return false;
  648. if (!valid)
  649. return true;
  650. scoped_refptr<base::Pickle::Attachment> attachment;
  651. if (!m->ReadAttachment(iter, &attachment))
  652. return false;
  653. if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
  654. MessageAttachment::Type::FUCHSIA_HANDLE) {
  655. return false;
  656. }
  657. *r = zx::channel(
  658. static_cast<internal::HandleAttachmentFuchsia*>(attachment.get())
  659. ->Take());
  660. return true;
  661. }
  662. void ParamTraits<zx::channel>::Log(const param_type& p, std::string* l) {
  663. l->append("ZirconChannel");
  664. }
  665. #endif // BUILDFLAG(IS_FUCHSIA)
  666. #if BUILDFLAG(IS_ANDROID)
  667. void ParamTraits<base::android::ScopedHardwareBufferHandle>::Write(
  668. base::Pickle* m,
  669. const param_type& p) {
  670. const bool is_valid = p.is_valid();
  671. WriteParam(m, is_valid);
  672. if (!is_valid)
  673. return;
  674. // We must keep a ref to the AHardwareBuffer alive until the receiver has
  675. // acquired its own reference. We do this by sending a message pipe handle
  676. // along with the buffer. When the receiver deserializes (or even if they
  677. // die without ever reading the message) their end of the pipe will be
  678. // closed. We will eventually detect this and release the AHB reference.
  679. mojo::MessagePipe tracking_pipe;
  680. m->WriteAttachment(new internal::MojoHandleAttachment(
  681. mojo::ScopedHandle::From(std::move(tracking_pipe.handle0))));
  682. WriteParam(m, base::FileDescriptor(p.SerializeAsFileDescriptor().release(),
  683. true /* auto_close */));
  684. // Pass ownership of the input handle to our tracking pipe to keep the AHB
  685. // alive long enough to be deserialized by the receiver.
  686. mojo::ScopeToMessagePipe(std::move(const_cast<param_type&>(p)),
  687. std::move(tracking_pipe.handle1));
  688. }
  689. bool ParamTraits<base::android::ScopedHardwareBufferHandle>::Read(
  690. const base::Pickle* m,
  691. base::PickleIterator* iter,
  692. param_type* r) {
  693. *r = base::android::ScopedHardwareBufferHandle();
  694. bool is_valid;
  695. if (!ReadParam(m, iter, &is_valid))
  696. return false;
  697. if (!is_valid)
  698. return true;
  699. scoped_refptr<base::Pickle::Attachment> tracking_pipe_attachment;
  700. if (!m->ReadAttachment(iter, &tracking_pipe_attachment))
  701. return false;
  702. // We keep this alive until the AHB is safely deserialized below. When this
  703. // goes out of scope, the sender holding the other end of this pipe will treat
  704. // this handle closure as a signal that it's safe to release their AHB
  705. // keepalive ref.
  706. mojo::ScopedHandle tracking_pipe =
  707. static_cast<MessageAttachment*>(tracking_pipe_attachment.get())
  708. ->TakeMojoHandle();
  709. base::FileDescriptor descriptor;
  710. if (!ReadParam(m, iter, &descriptor))
  711. return false;
  712. // NOTE: It is valid to deserialize an invalid FileDescriptor, so the success
  713. // of |ReadParam()| above does not imply that |descriptor| is valid.
  714. base::ScopedFD scoped_fd(descriptor.fd);
  715. if (!scoped_fd.is_valid())
  716. return false;
  717. *r = base::android::ScopedHardwareBufferHandle::DeserializeFromFileDescriptor(
  718. std::move(scoped_fd));
  719. return true;
  720. }
  721. void ParamTraits<base::android::ScopedHardwareBufferHandle>::Log(
  722. const param_type& p,
  723. std::string* l) {
  724. l->append(base::StringPrintf("base::android::ScopedHardwareBufferHandle(%p)",
  725. p.get()));
  726. }
  727. #endif // BUILDFLAG(IS_ANDROID)
  728. void ParamTraits<base::ReadOnlySharedMemoryRegion>::Write(base::Pickle* m,
  729. const param_type& p) {
  730. base::subtle::PlatformSharedMemoryRegion handle =
  731. base::ReadOnlySharedMemoryRegion::TakeHandleForSerialization(
  732. std::move(const_cast<param_type&>(p)));
  733. WriteParam(m, std::move(handle));
  734. }
  735. bool ParamTraits<base::ReadOnlySharedMemoryRegion>::Read(
  736. const base::Pickle* m,
  737. base::PickleIterator* iter,
  738. param_type* r) {
  739. base::subtle::PlatformSharedMemoryRegion handle;
  740. if (!ReadParam(m, iter, &handle))
  741. return false;
  742. *r = base::ReadOnlySharedMemoryRegion::Deserialize(std::move(handle));
  743. return true;
  744. }
  745. void ParamTraits<base::ReadOnlySharedMemoryRegion>::Log(const param_type& p,
  746. std::string* l) {
  747. *l = "<base::ReadOnlySharedMemoryRegion>";
  748. // TODO(alexilin): currently there is no way to access underlying handle
  749. // without destructing a ReadOnlySharedMemoryRegion instance.
  750. }
  751. void ParamTraits<base::WritableSharedMemoryRegion>::Write(base::Pickle* m,
  752. const param_type& p) {
  753. base::subtle::PlatformSharedMemoryRegion handle =
  754. base::WritableSharedMemoryRegion::TakeHandleForSerialization(
  755. std::move(const_cast<param_type&>(p)));
  756. WriteParam(m, std::move(handle));
  757. }
  758. bool ParamTraits<base::WritableSharedMemoryRegion>::Read(
  759. const base::Pickle* m,
  760. base::PickleIterator* iter,
  761. param_type* r) {
  762. base::subtle::PlatformSharedMemoryRegion handle;
  763. if (!ReadParam(m, iter, &handle))
  764. return false;
  765. *r = base::WritableSharedMemoryRegion::Deserialize(std::move(handle));
  766. return true;
  767. }
  768. void ParamTraits<base::WritableSharedMemoryRegion>::Log(const param_type& p,
  769. std::string* l) {
  770. *l = "<base::WritableSharedMemoryRegion>";
  771. // TODO(alexilin): currently there is no way to access underlying handle
  772. // without destructing a ReadOnlySharedMemoryRegion instance.
  773. }
  774. void ParamTraits<base::UnsafeSharedMemoryRegion>::Write(base::Pickle* m,
  775. const param_type& p) {
  776. base::subtle::PlatformSharedMemoryRegion handle =
  777. base::UnsafeSharedMemoryRegion::TakeHandleForSerialization(
  778. std::move(const_cast<param_type&>(p)));
  779. WriteParam(m, std::move(handle));
  780. }
  781. bool ParamTraits<base::UnsafeSharedMemoryRegion>::Read(
  782. const base::Pickle* m,
  783. base::PickleIterator* iter,
  784. param_type* r) {
  785. base::subtle::PlatformSharedMemoryRegion handle;
  786. if (!ReadParam(m, iter, &handle))
  787. return false;
  788. *r = base::UnsafeSharedMemoryRegion::Deserialize(std::move(handle));
  789. return true;
  790. }
  791. void ParamTraits<base::UnsafeSharedMemoryRegion>::Log(const param_type& p,
  792. std::string* l) {
  793. *l = "<base::UnsafeSharedMemoryRegion>";
  794. // TODO(alexilin): currently there is no way to access underlying handle
  795. // without destructing a ReadOnlySharedMemoryRegion instance.
  796. }
  797. void ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Write(
  798. base::Pickle* m,
  799. const param_type& p) {
  800. // This serialization must be kept in sync with
  801. // nacl_message_scanner.cc::WriteHandle().
  802. const bool valid = p.IsValid();
  803. WriteParam(m, valid);
  804. if (!valid)
  805. return;
  806. WriteParam(m, p.GetMode());
  807. WriteParam(m, static_cast<uint64_t>(p.GetSize()));
  808. WriteParam(m, p.GetGUID());
  809. #if BUILDFLAG(IS_WIN)
  810. base::win::ScopedHandle h = const_cast<param_type&>(p).PassPlatformHandle();
  811. HandleWin handle_win(h.Get());
  812. WriteParam(m, handle_win);
  813. #elif BUILDFLAG(IS_FUCHSIA)
  814. zx::vmo vmo = const_cast<param_type&>(p).PassPlatformHandle();
  815. WriteParam(m, vmo);
  816. #elif BUILDFLAG(IS_APPLE)
  817. base::mac::ScopedMachSendRight h =
  818. const_cast<param_type&>(p).PassPlatformHandle();
  819. MachPortMac mach_port_mac(h.get());
  820. WriteParam(m, mach_port_mac);
  821. #elif BUILDFLAG(IS_ANDROID)
  822. m->WriteAttachment(new internal::PlatformFileAttachment(
  823. base::ScopedFD(const_cast<param_type&>(p).PassPlatformHandle())));
  824. #elif BUILDFLAG(IS_POSIX)
  825. base::subtle::ScopedFDPair h =
  826. const_cast<param_type&>(p).PassPlatformHandle();
  827. m->WriteAttachment(new internal::PlatformFileAttachment(std::move(h.fd)));
  828. if (p.GetMode() ==
  829. base::subtle::PlatformSharedMemoryRegion::Mode::kWritable) {
  830. m->WriteAttachment(
  831. new internal::PlatformFileAttachment(std::move(h.readonly_fd)));
  832. }
  833. #endif
  834. }
  835. bool ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Read(
  836. const base::Pickle* m,
  837. base::PickleIterator* iter,
  838. param_type* r) {
  839. bool valid;
  840. if (!ReadParam(m, iter, &valid))
  841. return false;
  842. if (!valid) {
  843. *r = base::subtle::PlatformSharedMemoryRegion();
  844. return true;
  845. }
  846. base::subtle::PlatformSharedMemoryRegion::Mode mode;
  847. uint64_t shm_size;
  848. base::UnguessableToken guid;
  849. if (!ReadParam(m, iter, &mode) || !ReadParam(m, iter, &shm_size) ||
  850. !base::IsValueInRangeForNumericType<size_t>(shm_size) ||
  851. !ReadParam(m, iter, &guid)) {
  852. return false;
  853. }
  854. size_t size = static_cast<size_t>(shm_size);
  855. #if BUILDFLAG(IS_WIN)
  856. HandleWin handle_win;
  857. if (!ReadParam(m, iter, &handle_win))
  858. return false;
  859. *r = base::subtle::PlatformSharedMemoryRegion::Take(
  860. base::win::ScopedHandle(handle_win.get_handle()), mode, size, guid);
  861. #elif BUILDFLAG(IS_FUCHSIA)
  862. zx::vmo vmo;
  863. if (!ReadParam(m, iter, &vmo))
  864. return false;
  865. *r = base::subtle::PlatformSharedMemoryRegion::Take(std::move(vmo), mode,
  866. size, guid);
  867. #elif BUILDFLAG(IS_APPLE)
  868. MachPortMac mach_port_mac;
  869. if (!ReadParam(m, iter, &mach_port_mac))
  870. return false;
  871. *r = base::subtle::PlatformSharedMemoryRegion::Take(
  872. base::mac::ScopedMachSendRight(mach_port_mac.get_mach_port()), mode, size,
  873. guid);
  874. #elif BUILDFLAG(IS_POSIX)
  875. scoped_refptr<base::Pickle::Attachment> attachment;
  876. if (!m->ReadAttachment(iter, &attachment))
  877. return false;
  878. if (static_cast<MessageAttachment*>(attachment.get())->GetType() !=
  879. MessageAttachment::Type::PLATFORM_FILE) {
  880. return false;
  881. }
  882. #if BUILDFLAG(IS_ANDROID)
  883. *r = base::subtle::PlatformSharedMemoryRegion::Take(
  884. base::ScopedFD(
  885. static_cast<internal::PlatformFileAttachment*>(attachment.get())
  886. ->TakePlatformFile()),
  887. mode, size, guid);
  888. #else
  889. scoped_refptr<base::Pickle::Attachment> readonly_attachment;
  890. if (mode == base::subtle::PlatformSharedMemoryRegion::Mode::kWritable) {
  891. if (!m->ReadAttachment(iter, &readonly_attachment))
  892. return false;
  893. if (static_cast<MessageAttachment*>(readonly_attachment.get())->GetType() !=
  894. MessageAttachment::Type::PLATFORM_FILE) {
  895. return false;
  896. }
  897. }
  898. *r = base::subtle::PlatformSharedMemoryRegion::Take(
  899. base::subtle::ScopedFDPair(
  900. base::ScopedFD(
  901. static_cast<internal::PlatformFileAttachment*>(attachment.get())
  902. ->TakePlatformFile()),
  903. readonly_attachment
  904. ? base::ScopedFD(static_cast<internal::PlatformFileAttachment*>(
  905. readonly_attachment.get())
  906. ->TakePlatformFile())
  907. : base::ScopedFD()),
  908. mode, size, guid);
  909. #endif // BUILDFLAG(IS_ANDROID)
  910. #endif
  911. return true;
  912. }
  913. void ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Log(
  914. const param_type& p,
  915. std::string* l) {
  916. #if BUILDFLAG(IS_FUCHSIA)
  917. l->append("Handle: ");
  918. LogParam(p.GetPlatformHandle()->get(), l);
  919. #elif BUILDFLAG(IS_WIN)
  920. l->append("Handle: ");
  921. LogParam(p.GetPlatformHandle(), l);
  922. #elif BUILDFLAG(IS_APPLE)
  923. l->append("Mach port: ");
  924. LogParam(p.GetPlatformHandle(), l);
  925. #elif BUILDFLAG(IS_ANDROID)
  926. l->append("FD: ");
  927. LogParam(p.GetPlatformHandle(), l);
  928. #elif BUILDFLAG(IS_POSIX)
  929. base::subtle::FDPair h = p.GetPlatformHandle();
  930. l->append("FD: ");
  931. LogParam(h.fd, l);
  932. l->append("Read-only FD: ");
  933. LogParam(h.readonly_fd, l);
  934. #endif
  935. l->append("Mode: ");
  936. LogParam(p.GetMode(), l);
  937. l->append("size: ");
  938. LogParam(static_cast<uint64_t>(p.GetSize()), l);
  939. l->append("GUID: ");
  940. LogParam(p.GetGUID(), l);
  941. }
  942. void ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Write(
  943. base::Pickle* m,
  944. const param_type& value) {
  945. DCHECK(static_cast<int>(value) >= 0 &&
  946. static_cast<int>(value) <= static_cast<int>(param_type::kMaxValue));
  947. m->WriteInt(static_cast<int>(value));
  948. }
  949. bool ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Read(
  950. const base::Pickle* m,
  951. base::PickleIterator* iter,
  952. param_type* p) {
  953. int value;
  954. if (!iter->ReadInt(&value))
  955. return false;
  956. if (!(static_cast<int>(value) >= 0 &&
  957. static_cast<int>(value) <= static_cast<int>(param_type::kMaxValue))) {
  958. return false;
  959. }
  960. *p = static_cast<param_type>(value);
  961. return true;
  962. }
  963. void ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Log(
  964. const param_type& p,
  965. std::string* l) {
  966. LogParam(static_cast<int>(p), l);
  967. }
  968. #if BUILDFLAG(IS_WIN)
  969. void ParamTraits<PlatformFileForTransit>::Write(base::Pickle* m,
  970. const param_type& p) {
  971. m->WriteBool(p.IsValid());
  972. if (p.IsValid()) {
  973. HandleWin handle_win(p.GetHandle());
  974. ParamTraits<HandleWin>::Write(m, handle_win);
  975. ::CloseHandle(p.GetHandle());
  976. }
  977. }
  978. bool ParamTraits<PlatformFileForTransit>::Read(const base::Pickle* m,
  979. base::PickleIterator* iter,
  980. param_type* r) {
  981. bool is_valid;
  982. if (!iter->ReadBool(&is_valid))
  983. return false;
  984. if (!is_valid) {
  985. *r = PlatformFileForTransit();
  986. return true;
  987. }
  988. HandleWin handle_win;
  989. if (!ParamTraits<HandleWin>::Read(m, iter, &handle_win))
  990. return false;
  991. *r = PlatformFileForTransit(handle_win.get_handle());
  992. return true;
  993. }
  994. void ParamTraits<PlatformFileForTransit>::Log(const param_type& p,
  995. std::string* l) {
  996. LogParam(p.GetHandle(), l);
  997. }
  998. #endif // BUILDFLAG(IS_WIN)
  999. void ParamTraits<base::FilePath>::Write(base::Pickle* m, const param_type& p) {
  1000. p.WriteToPickle(m);
  1001. }
  1002. bool ParamTraits<base::FilePath>::Read(const base::Pickle* m,
  1003. base::PickleIterator* iter,
  1004. param_type* r) {
  1005. return r->ReadFromPickle(iter);
  1006. }
  1007. void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) {
  1008. ParamTraits<base::FilePath::StringType>::Log(p.value(), l);
  1009. }
  1010. void ParamTraits<base::ListValue>::Write(base::Pickle* m, const param_type& p) {
  1011. WriteListValue(p.GetList(), 0, m);
  1012. }
  1013. bool ParamTraits<base::ListValue>::Read(const base::Pickle* m,
  1014. base::PickleIterator* iter,
  1015. param_type* r) {
  1016. return ReadListValue(m, iter, 0, &(r->GetList()));
  1017. }
  1018. void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) {
  1019. std::string json;
  1020. base::JSONWriter::Write(p, &json);
  1021. l->append(json);
  1022. }
  1023. void ParamTraits<base::Value::List>::Write(base::Pickle* m,
  1024. const param_type& p) {
  1025. WriteListValue(p, 0, m);
  1026. }
  1027. bool ParamTraits<base::Value::List>::Read(const base::Pickle* m,
  1028. base::PickleIterator* iter,
  1029. param_type* r) {
  1030. return ReadListValue(m, iter, 0, r);
  1031. }
  1032. void ParamTraits<base::Value::List>::Log(const param_type& p, std::string* l) {
  1033. std::string json;
  1034. base::JSONWriter::Write(p, &json);
  1035. l->append(json);
  1036. }
  1037. void ParamTraits<base::Value>::Write(base::Pickle* m, const param_type& p) {
  1038. WriteValue(p, 0, m);
  1039. }
  1040. bool ParamTraits<base::Value>::Read(const base::Pickle* m,
  1041. base::PickleIterator* iter,
  1042. param_type* r) {
  1043. return ReadValue(m, iter, 0, r);
  1044. }
  1045. void ParamTraits<base::Value>::Log(const param_type& p, std::string* l) {
  1046. std::string json;
  1047. base::JSONWriter::Write(p, &json);
  1048. l->append(json);
  1049. }
  1050. void ParamTraits<base::File::Info>::Write(base::Pickle* m,
  1051. const param_type& p) {
  1052. WriteParam(m, p.size);
  1053. WriteParam(m, p.is_directory);
  1054. WriteParam(m, p.last_modified.ToDoubleT());
  1055. WriteParam(m, p.last_accessed.ToDoubleT());
  1056. WriteParam(m, p.creation_time.ToDoubleT());
  1057. }
  1058. bool ParamTraits<base::File::Info>::Read(const base::Pickle* m,
  1059. base::PickleIterator* iter,
  1060. param_type* p) {
  1061. double last_modified, last_accessed, creation_time;
  1062. if (!ReadParam(m, iter, &p->size) ||
  1063. !ReadParam(m, iter, &p->is_directory) ||
  1064. !ReadParam(m, iter, &last_modified) ||
  1065. !ReadParam(m, iter, &last_accessed) ||
  1066. !ReadParam(m, iter, &creation_time))
  1067. return false;
  1068. p->last_modified = base::Time::FromDoubleT(last_modified);
  1069. p->last_accessed = base::Time::FromDoubleT(last_accessed);
  1070. p->creation_time = base::Time::FromDoubleT(creation_time);
  1071. return true;
  1072. }
  1073. void ParamTraits<base::File::Info>::Log(const param_type& p,
  1074. std::string* l) {
  1075. l->append("(");
  1076. LogParam(p.size, l);
  1077. l->append(",");
  1078. LogParam(p.is_directory, l);
  1079. l->append(",");
  1080. LogParam(p.last_modified.ToDoubleT(), l);
  1081. l->append(",");
  1082. LogParam(p.last_accessed.ToDoubleT(), l);
  1083. l->append(",");
  1084. LogParam(p.creation_time.ToDoubleT(), l);
  1085. l->append(")");
  1086. }
  1087. void ParamTraits<base::Time>::Write(base::Pickle* m, const param_type& p) {
  1088. ParamTraits<int64_t>::Write(m, p.ToInternalValue());
  1089. }
  1090. bool ParamTraits<base::Time>::Read(const base::Pickle* m,
  1091. base::PickleIterator* iter,
  1092. param_type* r) {
  1093. int64_t value;
  1094. if (!ParamTraits<int64_t>::Read(m, iter, &value))
  1095. return false;
  1096. *r = base::Time::FromInternalValue(value);
  1097. return true;
  1098. }
  1099. void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) {
  1100. ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
  1101. }
  1102. void ParamTraits<base::TimeDelta>::Write(base::Pickle* m, const param_type& p) {
  1103. ParamTraits<int64_t>::Write(m, p.ToInternalValue());
  1104. }
  1105. bool ParamTraits<base::TimeDelta>::Read(const base::Pickle* m,
  1106. base::PickleIterator* iter,
  1107. param_type* r) {
  1108. int64_t value;
  1109. bool ret = ParamTraits<int64_t>::Read(m, iter, &value);
  1110. if (ret)
  1111. *r = base::TimeDelta::FromInternalValue(value);
  1112. return ret;
  1113. }
  1114. void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) {
  1115. ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
  1116. }
  1117. void ParamTraits<base::TimeTicks>::Write(base::Pickle* m, const param_type& p) {
  1118. ParamTraits<int64_t>::Write(m, p.ToInternalValue());
  1119. }
  1120. bool ParamTraits<base::TimeTicks>::Read(const base::Pickle* m,
  1121. base::PickleIterator* iter,
  1122. param_type* r) {
  1123. int64_t value;
  1124. bool ret = ParamTraits<int64_t>::Read(m, iter, &value);
  1125. if (ret)
  1126. *r = base::TimeTicks::FromInternalValue(value);
  1127. return ret;
  1128. }
  1129. void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) {
  1130. ParamTraits<int64_t>::Log(p.ToInternalValue(), l);
  1131. }
  1132. // If base::UnguessableToken is no longer 128 bits, the IPC serialization logic
  1133. // below should be updated.
  1134. static_assert(sizeof(base::UnguessableToken) == 2 * sizeof(uint64_t),
  1135. "base::UnguessableToken should be of size 2 * sizeof(uint64_t).");
  1136. void ParamTraits<base::UnguessableToken>::Write(base::Pickle* m,
  1137. const param_type& p) {
  1138. DCHECK(!p.is_empty());
  1139. ParamTraits<uint64_t>::Write(m, p.GetHighForSerialization());
  1140. ParamTraits<uint64_t>::Write(m, p.GetLowForSerialization());
  1141. }
  1142. bool ParamTraits<base::UnguessableToken>::Read(const base::Pickle* m,
  1143. base::PickleIterator* iter,
  1144. param_type* r) {
  1145. uint64_t high, low;
  1146. if (!ParamTraits<uint64_t>::Read(m, iter, &high) ||
  1147. !ParamTraits<uint64_t>::Read(m, iter, &low))
  1148. return false;
  1149. // Receiving a zeroed UnguessableToken is a security issue.
  1150. if (high == 0 && low == 0)
  1151. return false;
  1152. *r = base::UnguessableToken::Deserialize(high, low);
  1153. return true;
  1154. }
  1155. void ParamTraits<base::UnguessableToken>::Log(const param_type& p,
  1156. std::string* l) {
  1157. l->append(p.ToString());
  1158. }
  1159. void ParamTraits<IPC::ChannelHandle>::Write(base::Pickle* m,
  1160. const param_type& p) {
  1161. #if BUILDFLAG(IS_NACL)
  1162. WriteParam(m, p.socket);
  1163. #else
  1164. WriteParam(m, p.mojo_handle);
  1165. #endif
  1166. }
  1167. bool ParamTraits<IPC::ChannelHandle>::Read(const base::Pickle* m,
  1168. base::PickleIterator* iter,
  1169. param_type* r) {
  1170. #if BUILDFLAG(IS_NACL)
  1171. return ReadParam(m, iter, &r->socket);
  1172. #else
  1173. return ReadParam(m, iter, &r->mojo_handle);
  1174. #endif
  1175. }
  1176. void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p,
  1177. std::string* l) {
  1178. l->append("ChannelHandle(");
  1179. #if BUILDFLAG(IS_NACL)
  1180. ParamTraits<base::FileDescriptor>::Log(p.socket, l);
  1181. #else
  1182. LogParam(p.mojo_handle, l);
  1183. #endif
  1184. l->append(")");
  1185. }
  1186. void ParamTraits<LogData>::Write(base::Pickle* m, const param_type& p) {
  1187. WriteParam(m, p.channel);
  1188. WriteParam(m, p.routing_id);
  1189. WriteParam(m, p.type);
  1190. WriteParam(m, p.flags);
  1191. WriteParam(m, p.sent);
  1192. WriteParam(m, p.receive);
  1193. WriteParam(m, p.dispatch);
  1194. WriteParam(m, p.message_name);
  1195. WriteParam(m, p.params);
  1196. }
  1197. bool ParamTraits<LogData>::Read(const base::Pickle* m,
  1198. base::PickleIterator* iter,
  1199. param_type* r) {
  1200. return
  1201. ReadParam(m, iter, &r->channel) &&
  1202. ReadParam(m, iter, &r->routing_id) &&
  1203. ReadParam(m, iter, &r->type) &&
  1204. ReadParam(m, iter, &r->flags) &&
  1205. ReadParam(m, iter, &r->sent) &&
  1206. ReadParam(m, iter, &r->receive) &&
  1207. ReadParam(m, iter, &r->dispatch) &&
  1208. ReadParam(m, iter, &r->message_name) &&
  1209. ReadParam(m, iter, &r->params);
  1210. }
  1211. void ParamTraits<LogData>::Log(const param_type& p, std::string* l) {
  1212. // Doesn't make sense to implement this!
  1213. }
  1214. void ParamTraits<Message>::Write(base::Pickle* m, const Message& p) {
  1215. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  1216. // We don't serialize the file descriptors in the nested message, so there
  1217. // better not be any.
  1218. DCHECK(!p.HasAttachments());
  1219. #endif
  1220. // Don't just write out the message. This is used to send messages between
  1221. // NaCl (Posix environment) and the browser (could be on Windows). The message
  1222. // header formats differ between these systems (so does handle sharing, but
  1223. // we already asserted we don't have any handles). So just write out the
  1224. // parts of the header we use.
  1225. //
  1226. // Be careful also to use only explicitly-sized types. The NaCl environment
  1227. // could be 64-bit and the host browser could be 32-bits. The nested message
  1228. // may or may not be safe to send between 32-bit and 64-bit systems, but we
  1229. // leave that up to the code sending the message to ensure.
  1230. m->WriteUInt32(static_cast<uint32_t>(p.routing_id()));
  1231. m->WriteUInt32(p.type());
  1232. m->WriteUInt32(p.flags());
  1233. m->WriteData(p.payload(), p.payload_size());
  1234. }
  1235. bool ParamTraits<Message>::Read(const base::Pickle* m,
  1236. base::PickleIterator* iter,
  1237. Message* r) {
  1238. uint32_t routing_id, type, flags;
  1239. if (!iter->ReadUInt32(&routing_id) ||
  1240. !iter->ReadUInt32(&type) ||
  1241. !iter->ReadUInt32(&flags))
  1242. return false;
  1243. size_t payload_size;
  1244. const char* payload;
  1245. if (!iter->ReadData(&payload, &payload_size))
  1246. return false;
  1247. r->SetHeaderValues(static_cast<int32_t>(routing_id), type, flags);
  1248. r->WriteBytes(payload, payload_size);
  1249. return true;
  1250. }
  1251. void ParamTraits<Message>::Log(const Message& p, std::string* l) {
  1252. l->append("<IPC::Message>");
  1253. }
  1254. #if BUILDFLAG(IS_WIN)
  1255. // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64
  1256. // bit systems. That's why we use the Windows macros to convert to 32 bits.
  1257. void ParamTraits<HANDLE>::Write(base::Pickle* m, const param_type& p) {
  1258. m->WriteInt(HandleToLong(p));
  1259. }
  1260. bool ParamTraits<HANDLE>::Read(const base::Pickle* m,
  1261. base::PickleIterator* iter,
  1262. param_type* r) {
  1263. int32_t temp;
  1264. if (!iter->ReadInt(&temp))
  1265. return false;
  1266. *r = LongToHandle(temp);
  1267. return true;
  1268. }
  1269. void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) {
  1270. l->append(base::StringPrintf("0x%p", p));
  1271. }
  1272. void ParamTraits<MSG>::Write(base::Pickle* m, const param_type& p) {
  1273. m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG));
  1274. }
  1275. bool ParamTraits<MSG>::Read(const base::Pickle* m,
  1276. base::PickleIterator* iter,
  1277. param_type* r) {
  1278. const char *data;
  1279. size_t data_size = 0;
  1280. bool result = iter->ReadData(&data, &data_size);
  1281. if (result && data_size == sizeof(MSG)) {
  1282. memcpy(r, data, sizeof(MSG));
  1283. } else {
  1284. result = false;
  1285. NOTREACHED();
  1286. }
  1287. return result;
  1288. }
  1289. void ParamTraits<MSG>::Log(const param_type& p, std::string* l) {
  1290. l->append("<MSG>");
  1291. }
  1292. #endif // BUILDFLAG(IS_WIN)
  1293. } // namespace IPC