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