Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: chrome/common/ipc_message_utils.h

Issue 39208: POSIX: Rewrite IPC's interaction with FileDescriptor (Closed)
Patch Set: Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 #ifndef CHROME_COMMON_IPC_MESSAGE_UTILS_H_ 5 #ifndef CHROME_COMMON_IPC_MESSAGE_UTILS_H_
6 #define CHROME_COMMON_IPC_MESSAGE_UTILS_H_ 6 #define CHROME_COMMON_IPC_MESSAGE_UTILS_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 #include <map> 10 #include <map>
11 11
12 #include "base/file_path.h" 12 #include "base/file_path.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/string16.h" 14 #include "base/string16.h"
15 #include "base/tuple.h" 15 #include "base/tuple.h"
16 #if defined(OS_POSIX) 16 #if defined(OS_POSIX)
17 #include "chrome/common/file_descriptor_set_posix.h" 17 #include "chrome/common/file_descriptor_set_posix.h"
18 #endif 18 #endif
19 #include "chrome/common/ipc_maybe.h"
20 #include "chrome/common/ipc_sync_message.h" 19 #include "chrome/common/ipc_sync_message.h"
21 #include "chrome/common/thumbnail_score.h" 20 #include "chrome/common/thumbnail_score.h"
22 #include "chrome/common/transport_dib.h" 21 #include "chrome/common/transport_dib.h"
23 #include "webkit/glue/cache_manager.h" 22 #include "webkit/glue/cache_manager.h"
24 #include "webkit/glue/console_message_level.h" 23 #include "webkit/glue/console_message_level.h"
25 #include "webkit/glue/find_in_page_request.h" 24 #include "webkit/glue/find_in_page_request.h"
26 #include "webkit/glue/webcursor.h" 25 #include "webkit/glue/webcursor.h"
27 #include "webkit/glue/window_open_disposition.h" 26 #include "webkit/glue/window_open_disposition.h"
28 27
29 // Forward declarations. 28 // Forward declarations.
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 686
688 template <> 687 template <>
689 struct ParamTraits<gfx::Size> { 688 struct ParamTraits<gfx::Size> {
690 typedef gfx::Size param_type; 689 typedef gfx::Size param_type;
691 static void Write(Message* m, const param_type& p); 690 static void Write(Message* m, const param_type& p);
692 static bool Read(const Message* m, void** iter, param_type* r); 691 static bool Read(const Message* m, void** iter, param_type* r);
693 static void Log(const param_type& p, std::wstring* l); 692 static void Log(const param_type& p, std::wstring* l);
694 }; 693 };
695 694
696 #if defined(OS_POSIX) 695 #if defined(OS_POSIX)
696 // FileDescriptors may be serialised over IPC channels on POSIX. On the
697 // receiving side, the FileDescriptor is a valid duplicate of the file
698 // descriptor which was transmitted: *it is not just a copy of the integer like
699 // HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In
700 // this case, the receiving end will see a value of -1. *Zero is a valid file
701 // descriptor*.
702 //
703 // The received file descriptor will have the |auto_close| flag set to true. The
704 // code which handles the message is responsible for taking ownership of it.
705 // File descriptors are OS resources and must be closed when no longer needed.
706 //
707 // When sending a file descriptor, the file descriptor must be valid at the time
708 // of transmission. Since transmission is not synchronous, one should consider
709 // dup()ing any file descriptors to be transmitted and setting the |auto_close|
710 // flag, which causes the file descriptor to be closed after writing.
697 template<> 711 template<>
698 struct ParamTraits<base::FileDescriptor> { 712 struct ParamTraits<base::FileDescriptor> {
699 typedef base::FileDescriptor param_type; 713 typedef base::FileDescriptor param_type;
700 static void Write(Message* m, const param_type& p) { 714 static void Write(Message* m, const param_type& p) {
701 if (!m->WriteFileDescriptor(p)) 715 const bool valid = p.fd >= 0;
702 NOTREACHED(); 716 WriteParam(m, valid);
717
718 if (valid) {
719 if (!m->WriteFileDescriptor(p))
720 NOTREACHED();
721 }
703 } 722 }
704 static bool Read(const Message* m, void** iter, param_type* r) { 723 static bool Read(const Message* m, void** iter, param_type* r) {
705 return m->ReadFileDescriptor(iter, r); 724 bool valid;
725 if (!ReadParam(m, iter, &valid))
726 return false;
727
728 if (valid) {
729 return m->ReadFileDescriptor(iter, r);
730 } else {
darin (slow to review) 2009/03/05 19:42:12 nit: no need for else after return
731 r->fd = -1;
732 r->auto_close = false;
733 return true;
734 }
706 } 735 }
707 static void Log(const param_type& p, std::wstring* l) { 736 static void Log(const param_type& p, std::wstring* l) {
708 if (p.auto_close) { 737 if (p.auto_close) {
709 l->append(StringPrintf(L"FD(%d auto-close)", p.fd)); 738 l->append(StringPrintf(L"FD(%d auto-close)", p.fd));
710 } else { 739 } else {
711 l->append(StringPrintf(L"FD(%d)", p.fd)); 740 l->append(StringPrintf(L"FD(%d)", p.fd));
712 } 741 }
713 } 742 }
714 }; 743 };
715 #endif // defined(OS_POSIX) 744 #endif // defined(OS_POSIX)
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
940 static void Log(const param_type& p, std::wstring* l) { 969 static void Log(const param_type& p, std::wstring* l) {
941 l->append(L"TransportDIB("); 970 l->append(L"TransportDIB(");
942 LogParam(p.handle, l); 971 LogParam(p.handle, l);
943 l->append(L", "); 972 l->append(L", ");
944 LogParam(p.sequence_num, l); 973 LogParam(p.sequence_num, l);
945 l->append(L")"); 974 l->append(L")");
946 } 975 }
947 }; 976 };
948 #endif 977 #endif
949 978
950 template<typename A>
951 struct ParamTraits<Maybe<A> > {
952 typedef struct Maybe<A> param_type;
953 static void Write(Message* m, const param_type& p) {
954 WriteParam(m, p.valid);
955 if (p.valid)
956 WriteParam(m, p.value);
957 }
958 static bool Read(const Message* m, void** iter, param_type* r) {
959 if (!ReadParam(m, iter, &r->valid))
960 return false;
961
962 if (r->valid)
963 return ReadParam(m, iter, &r->value);
964 return true;
965 }
966 static void Log(const param_type& p, std::wstring* l) {
967 if (p.valid) {
968 l->append(L"Just ");
969 ParamTraits<A>::Log(p.value, l);
970 } else {
971 l->append(L"Nothing");
972 }
973
974 }
975 };
976
977
978 template <> 979 template <>
979 struct ParamTraits<Message> { 980 struct ParamTraits<Message> {
980 static void Write(Message* m, const Message& p) { 981 static void Write(Message* m, const Message& p) {
981 m->WriteInt(p.size()); 982 m->WriteInt(p.size());
982 m->WriteData(reinterpret_cast<const char*>(p.data()), p.size()); 983 m->WriteData(reinterpret_cast<const char*>(p.data()), p.size());
983 } 984 }
984 static bool Read(const Message* m, void** iter, Message* r) { 985 static bool Read(const Message* m, void** iter, Message* r) {
985 int size; 986 int size;
986 if (!m->ReadInt(iter, &size)) 987 if (!m->ReadInt(iter, &size))
987 return false; 988 return false;
988 const char* data; 989 const char* data;
989 if (!m->ReadData(iter, &data, &size)) 990 if (!m->ReadData(iter, &data, &size))
990 return false; 991 return false;
991 *r = Message(data, size); 992 *r = Message(data, size);
992 return true; 993 return true;
993 } 994 }
994 static void Log(const Message& p, std::wstring* l) { 995 static void Log(const Message& p, std::wstring* l) {
995 l->append(L"<IPC::Message>"); 996 l->append(L"<IPC::Message>");
996 } 997 }
997 }; 998 };
998 999
999
1000 template <> 1000 template <>
1001 struct ParamTraits<Tuple0> { 1001 struct ParamTraits<Tuple0> {
1002 typedef Tuple0 param_type; 1002 typedef Tuple0 param_type;
1003 static void Write(Message* m, const param_type& p) { 1003 static void Write(Message* m, const param_type& p) {
1004 } 1004 }
1005 static bool Read(const Message* m, void** iter, param_type* r) { 1005 static bool Read(const Message* m, void** iter, param_type* r) {
1006 return true; 1006 return true;
1007 } 1007 }
1008 static void Log(const param_type& p, std::wstring* l) { 1008 static void Log(const param_type& p, std::wstring* l) {
1009 } 1009 }
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
1467 l->append(L"<FindInPageRequest>"); 1467 l->append(L"<FindInPageRequest>");
1468 } 1468 }
1469 }; 1469 };
1470 1470
1471 //----------------------------------------------------------------------------- 1471 //-----------------------------------------------------------------------------
1472 1472
1473 } // namespace IPC 1473 } // namespace IPC
1474 1474
1475 #endif // CHROME_COMMON_IPC_MESSAGE_UTILS_H_ 1475 #endif // CHROME_COMMON_IPC_MESSAGE_UTILS_H_
1476 1476
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698