OLD | NEW |
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_message_process_host.h" | 5 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" | |
15 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 14 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
16 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/chrome_version_info.h" | 16 #include "chrome/common/chrome_version_info.h" |
18 #include "chrome/common/extensions/features/feature.h" | 17 #include "chrome/common/extensions/features/feature.h" |
19 #include "extensions/common/constants.h" | |
20 #include "googleurl/src/gurl.h" | |
21 #include "net/base/file_stream.h" | 18 #include "net/base/file_stream.h" |
22 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
23 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
24 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
25 | 22 |
26 namespace { | 23 namespace { |
27 | 24 |
28 const int kExitTimeoutMS = 5000; | 25 const int kExitTimeoutMS = 5000; |
29 const uint32 kMaxMessageDataLength = 10 * 1024 * 1024; | 26 const uint32 kMaxMessageDataLength = 10 * 1024 * 1024; |
30 | 27 |
31 // Message header contains 4-byte integer size of the message. | 28 // Message header contains 4-byte integer size of the message. |
32 const size_t kMessageHeaderSize = 4; | 29 const size_t kMessageHeaderSize = 4; |
33 | 30 |
34 // Size of the buffer to be allocated for each read. | 31 // Size of the buffer to be allocated for each read. |
35 const size_t kReadBufferSize = 4096; | 32 const size_t kReadBufferSize = 4096; |
36 | 33 |
37 } // namespace | 34 } // namespace |
38 | 35 |
39 namespace extensions { | 36 namespace extensions { |
40 | 37 |
41 NativeMessageProcessHost::NativeMessageProcessHost( | 38 NativeMessageProcessHost::NativeMessageProcessHost( |
42 base::WeakPtr<Client> weak_client_ui, | 39 base::WeakPtr<Client> weak_client_ui, |
43 const std::string& source_extension_id, | |
44 const std::string& native_host_name, | 40 const std::string& native_host_name, |
45 int destination_port, | 41 int destination_port, |
46 scoped_ptr<NativeProcessLauncher> launcher) | 42 scoped_ptr<NativeProcessLauncher> launcher) |
47 : weak_client_ui_(weak_client_ui), | 43 : weak_client_ui_(weak_client_ui), |
48 source_extension_id_(source_extension_id), | |
49 native_host_name_(native_host_name), | 44 native_host_name_(native_host_name), |
50 destination_port_(destination_port), | 45 destination_port_(destination_port), |
51 launcher_(launcher.Pass()), | 46 launcher_(launcher.Pass()), |
52 closed_(false), | 47 closed_(false), |
53 read_pending_(false), | 48 read_pending_(false), |
54 read_eof_(false), | 49 read_eof_(false), |
55 write_pending_(false) { | 50 write_pending_(false) { |
56 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 51 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
57 | 52 |
58 // It's safe to use base::Unretained() here because NativeMessagePort always | 53 // It's safe to use base::Unretained() here because NativeMessagePort always |
59 // deletes us on the IO thread. | 54 // deletes us on the IO thread. |
60 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 55 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, |
61 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, | 56 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, |
62 base::Unretained(this))); | 57 base::Unretained(this))); |
63 } | 58 } |
64 | 59 |
65 NativeMessageProcessHost::~NativeMessageProcessHost() { | 60 NativeMessageProcessHost::~NativeMessageProcessHost() { |
66 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
67 Close(); | 62 Close(); |
68 } | 63 } |
69 | 64 |
70 // static | 65 // static |
71 scoped_ptr<NativeMessageProcessHost> NativeMessageProcessHost::Create( | 66 scoped_ptr<NativeMessageProcessHost> NativeMessageProcessHost::Create( |
72 base::WeakPtr<Client> weak_client_ui, | 67 base::WeakPtr<Client> weak_client_ui, |
73 const std::string& source_extension_id, | |
74 const std::string& native_host_name, | 68 const std::string& native_host_name, |
75 int destination_port) { | 69 int destination_port) { |
76 return CreateWithLauncher(weak_client_ui, source_extension_id, | 70 return CreateWithLauncher(weak_client_ui, native_host_name, destination_port, |
77 native_host_name, destination_port, | |
78 NativeProcessLauncher::CreateDefault()); | 71 NativeProcessLauncher::CreateDefault()); |
79 } | 72 } |
80 | 73 |
81 // static | 74 // static |
82 scoped_ptr<NativeMessageProcessHost> | 75 scoped_ptr<NativeMessageProcessHost> |
83 NativeMessageProcessHost::CreateWithLauncher( | 76 NativeMessageProcessHost::CreateWithLauncher( |
84 base::WeakPtr<Client> weak_client_ui, | 77 base::WeakPtr<Client> weak_client_ui, |
85 const std::string& source_extension_id, | |
86 const std::string& native_host_name, | 78 const std::string& native_host_name, |
87 int destination_port, | 79 int destination_port, |
88 scoped_ptr<NativeProcessLauncher> launcher) { | 80 scoped_ptr<NativeProcessLauncher> launcher) { |
89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 81 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
90 | 82 |
91 scoped_ptr<NativeMessageProcessHost> process; | 83 scoped_ptr<NativeMessageProcessHost> process; |
92 if (Feature::GetCurrentChannel() > chrome::VersionInfo::CHANNEL_DEV || | 84 if (Feature::GetCurrentChannel() > chrome::VersionInfo::CHANNEL_DEV || |
93 !CommandLine::ForCurrentProcess()->HasSwitch( | 85 !CommandLine::ForCurrentProcess()->HasSwitch( |
94 switches::kEnableNativeMessaging)) { | 86 switches::kEnableNativeMessaging)) { |
95 return process.Pass(); | 87 return process.Pass(); |
96 } | 88 } |
97 | 89 |
98 process.reset(new NativeMessageProcessHost( | 90 process.reset(new NativeMessageProcessHost( |
99 weak_client_ui, source_extension_id, native_host_name, | 91 weak_client_ui, native_host_name, destination_port, launcher.Pass())); |
100 destination_port, launcher.Pass())); | |
101 | 92 |
102 return process.Pass(); | 93 return process.Pass(); |
103 } | 94 } |
104 | 95 |
105 void NativeMessageProcessHost::LaunchHostProcess() { | 96 void NativeMessageProcessHost::LaunchHostProcess() { |
106 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 97 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
107 | 98 |
108 GURL origin(std::string(kExtensionScheme) + "://" + source_extension_id_); | 99 launcher_->Launch( |
109 launcher_->Launch(origin, native_host_name_, | 100 native_host_name_, base::Bind( |
110 base::Bind(&NativeMessageProcessHost::OnHostProcessLaunched, | 101 &NativeMessageProcessHost::OnHostProcessLaunched, |
111 base::Unretained(this))); | 102 base::Unretained(this))); |
112 } | 103 } |
113 | 104 |
114 void NativeMessageProcessHost::OnHostProcessLaunched( | 105 void NativeMessageProcessHost::OnHostProcessLaunched( |
115 bool result, | 106 bool result, |
116 base::PlatformFile read_file, | 107 base::PlatformFile read_file, |
117 base::PlatformFile write_file) { | 108 base::PlatformFile write_file) { |
118 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 109 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
119 | 110 |
120 if (!result) { | 111 if (!result) { |
121 OnError(); | 112 OnError(); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 | 304 |
314 closed_ = true; | 305 closed_ = true; |
315 read_stream_.reset(); | 306 read_stream_.reset(); |
316 write_stream_.reset(); | 307 write_stream_.reset(); |
317 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 308 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
318 base::Bind(&Client::CloseChannel, weak_client_ui_, | 309 base::Bind(&Client::CloseChannel, weak_client_ui_, |
319 destination_port_, true)); | 310 destination_port_, true)); |
320 } | 311 } |
321 | 312 |
322 } // namespace extensions | 313 } // namespace extensions |
OLD | NEW |