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

Side by Side Diff: chrome/browser/extensions/api/messaging/native_process_launcher_win.cc

Issue 10918255: The Windows portion of Native Messagaing (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Less Sleepy Created 8 years, 1 month 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) 2012 The Chromium Authors. All rights reserved. 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 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 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" 5 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 8
9 #include "base/command_line.h"
9 #include "base/logging.h" 10 #include "base/logging.h"
10 #include "base/process_util.h" 11 #include "base/process_util.h"
11 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
12 #include "base/string_number_conversions.h" 13 #include "base/string_number_conversions.h"
14 #include "base/utf_string_conversions.h"
15
16 namespace {
17
18 int pipe_counter = 0;
19
20 } // namespace
13 21
14 namespace extensions { 22 namespace extensions {
15 23
16 bool NativeProcessLauncher::LaunchNativeProcess( 24 bool NativeProcessLauncher::LaunchNativeProcess(
rvargas (doing something else) 2012/11/29 01:19:23 We cannot do this on the IO thread. There's a bunc
eaugusti 2012/12/07 02:21:08 Ok, the NativeProcessHost itself lives on the IO t
17 const FilePath& path, 25 const FilePath& path,
18 base::ProcessHandle* native_process_handle, 26 base::ProcessHandle* native_process_handle,
19 NativeMessageProcessHost::FileHandle* read_file, 27 NativeMessageProcessHost::FileHandle* read_file,
20 NativeMessageProcessHost::FileHandle* write_file) const { 28 NativeMessageProcessHost::FileHandle* write_file) const {
21 NOTREACHED(); 29 SECURITY_ATTRIBUTES sa_attr;
22 return false; 30 // Set the bInheritHandle flag so pipe handles are inherited.
31 sa_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
32 sa_attr.bInheritHandle = TRUE;
33 sa_attr.lpSecurityDescriptor = NULL;
34
35 DWORD pipe_open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED;
36 DWORD pipe_mode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE;
37 DWORD max_instances = 1;
38 DWORD buffer_size = 10 * 1024 * 1024;
39 // TODO(eriq): How long to make the timeout?
40 DWORD timeout = 5000;
41
42 string16 out_pipe_name = ASCIIToWide(base::StringPrintf(
43 "\\\\.\\pipe\\chrome.nativeMessage.out.%d", ++pipe_counter));
44 string16 in_pipe_name = ASCIIToWide(base::StringPrintf(
45 "\\\\.\\pipe\\chrome.nativeMessage.in.%d", ++pipe_counter));
46
47 HANDLE stdin_parent_pipe = NULL;
48 HANDLE stdout_parent_pipe = NULL;
49
50 HANDLE stdin_child_pipe = NULL;
51 HANDLE stdout_child_pipe = NULL;
52
53 // Create the pipes to read and write from.
54 stdout_parent_pipe = CreateNamedPipeW(out_pipe_name.c_str(), pipe_open_mode,
55 pipe_mode, max_instances, buffer_size,
56 buffer_size, timeout, &sa_attr);
57 if (stdout_parent_pipe == INVALID_HANDLE_VALUE) {
58 NOTREACHED() << "Failed to create pipe";
rvargas (doing something else) 2012/11/29 01:19:23 we should not really dcheck on the return value of
Sergey Ulanov 2012/12/01 01:23:00 I think NOTREACHED() is the right thing to do here
rvargas (doing something else) 2012/12/01 01:57:24 This pattern makes the developer who writes the co
eaugusti 2012/12/07 02:21:08 What about using a DPLOG? It makes sense not to us
rvargas (doing something else) 2012/12/12 00:20:22 You may be surprised by the amount of code inserte
59 return false;
60 }
61
62 DWORD desired_access = GENERIC_READ | GENERIC_WRITE;
63 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
64
65 stdout_child_pipe = CreateFileW(out_pipe_name.c_str(), desired_access,
66 share_mode, &sa_attr, OPEN_EXISTING,
67 FILE_FLAG_OVERLAPPED, NULL);
68 if (stdout_child_pipe == INVALID_HANDLE_VALUE) {
69 NOTREACHED() << "Failed to create pipe";
70 return false;
71 }
72
73 stdin_parent_pipe = CreateNamedPipeW(in_pipe_name.c_str(), pipe_open_mode,
74 pipe_mode, max_instances, buffer_size,
75 buffer_size, timeout, &sa_attr);
76 if (stdin_parent_pipe == INVALID_HANDLE_VALUE) {
77 NOTREACHED() << "Failed to create pipe";
78 return false;
79 }
80
81 stdin_child_pipe = CreateFileW(in_pipe_name.c_str(), desired_access,
82 share_mode, &sa_attr, OPEN_EXISTING,
83 FILE_FLAG_OVERLAPPED, NULL);
84 if (stdin_child_pipe == INVALID_HANDLE_VALUE) {
85 NOTREACHED() << "Failed to create pipe";
86 return false;
87 }
88
89 // Don't inherit the parents'
90 if (!SetHandleInformation(stdin_parent_pipe, HANDLE_FLAG_INHERIT, 0) ||
91 !SetHandleInformation(stdout_parent_pipe, HANDLE_FLAG_INHERIT, 0)) {
92 NOTREACHED() << "Failed to enable pipe inheritance";
93 return false;
94 }
95
96 CommandLine line(path);
97 base::LaunchOptions options;
98 // Need to inherit the file handles.
99 options.inherit_handles = true;
rvargas (doing something else) 2012/11/29 01:19:23 am I reading this correctly? it looks like the new
Sergey Ulanov 2012/12/01 01:23:00 I'm not an expect, but according to MSDN ( http://
rvargas (doing something else) 2012/12/01 01:57:24 Correct, only handles marked for inheritance are i
Sergey Ulanov 2012/12/04 23:50:43 Do you have any suggestions on how we can launch a
rvargas (doing something else) 2012/12/05 20:59:05 Yeah, that's a pain in the back. I only see using
rvargas (doing something else) 2012/12/06 19:15:06 Note that a better option is to use another IPC me
100 options.stdin_handle = stdin_child_pipe;
101 options.stdout_handle = stdout_child_pipe;
102 options.start_hidden = true;
103
104 if (!base::LaunchProcess(line.GetCommandLineString(), options,
105 native_process_handle)) {
106 LOG(ERROR) << "Error launching process";
107 return false;
108 }
109
110 *read_file = stdout_parent_pipe;
111 *write_file = stdin_parent_pipe;
112
113 return true;
23 } 114 }
24 115
25 } // namespace extensions 116 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698