| 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/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/prefs/pref_service.h" | |
| 11 #include "base/process/kill.h" | 10 #include "base/process/kill.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" | 12 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" |
| 15 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 13 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
| 16 #include "chrome/common/chrome_version_info.h" | 14 #include "chrome/common/chrome_version_info.h" |
| 17 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 18 #include "extensions/browser/pref_names.h" | |
| 19 #include "extensions/common/constants.h" | 16 #include "extensions/common/constants.h" |
| 20 #include "extensions/common/features/feature.h" | 17 #include "extensions/common/features/feature.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 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 26 | 23 |
| 27 namespace { | 24 namespace { |
| 28 | 25 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 44 const char kNotFoundError[] = "Specified native messaging host not found."; | 41 const char kNotFoundError[] = "Specified native messaging host not found."; |
| 45 const char kForbiddenError[] = | 42 const char kForbiddenError[] = |
| 46 "Access to the specified native messaging host is forbidden."; | 43 "Access to the specified native messaging host is forbidden."; |
| 47 const char kHostInputOuputError[] = | 44 const char kHostInputOuputError[] = |
| 48 "Error when communicating with the native messaging host."; | 45 "Error when communicating with the native messaging host."; |
| 49 | 46 |
| 50 } // namespace | 47 } // namespace |
| 51 | 48 |
| 52 namespace extensions { | 49 namespace extensions { |
| 53 | 50 |
| 54 // static | |
| 55 NativeMessageProcessHost::PolicyPermission | |
| 56 NativeMessageProcessHost::IsHostAllowed(const PrefService* pref_service, | |
| 57 const std::string& native_host_name) { | |
| 58 NativeMessageProcessHost::PolicyPermission allow_result = ALLOW_ALL; | |
| 59 if (pref_service->IsManagedPreference( | |
| 60 pref_names::kNativeMessagingUserLevelHosts)) { | |
| 61 if (!pref_service->GetBoolean(pref_names::kNativeMessagingUserLevelHosts)) | |
| 62 allow_result = ALLOW_SYSTEM_ONLY; | |
| 63 } | |
| 64 | |
| 65 // All native messaging hosts are allowed if there is no blacklist. | |
| 66 if (!pref_service->IsManagedPreference(pref_names::kNativeMessagingBlacklist)) | |
| 67 return allow_result; | |
| 68 const base::ListValue* blacklist = | |
| 69 pref_service->GetList(pref_names::kNativeMessagingBlacklist); | |
| 70 if (!blacklist) | |
| 71 return allow_result; | |
| 72 | |
| 73 // Check if the name or the wildcard is in the blacklist. | |
| 74 base::StringValue name_value(native_host_name); | |
| 75 base::StringValue wildcard_value("*"); | |
| 76 if (blacklist->Find(name_value) == blacklist->end() && | |
| 77 blacklist->Find(wildcard_value) == blacklist->end()) { | |
| 78 return allow_result; | |
| 79 } | |
| 80 | |
| 81 // The native messaging host is blacklisted. Check the whitelist. | |
| 82 if (pref_service->IsManagedPreference( | |
| 83 pref_names::kNativeMessagingWhitelist)) { | |
| 84 const base::ListValue* whitelist = | |
| 85 pref_service->GetList(pref_names::kNativeMessagingWhitelist); | |
| 86 if (whitelist && whitelist->Find(name_value) != whitelist->end()) | |
| 87 return allow_result; | |
| 88 } | |
| 89 | |
| 90 return DISALLOW; | |
| 91 } | |
| 92 | |
| 93 NativeMessageProcessHost::NativeMessageProcessHost( | 51 NativeMessageProcessHost::NativeMessageProcessHost( |
| 94 base::WeakPtr<Client> weak_client_ui, | 52 base::WeakPtr<Client> weak_client_ui, |
| 95 const std::string& source_extension_id, | 53 const std::string& source_extension_id, |
| 96 const std::string& native_host_name, | 54 const std::string& native_host_name, |
| 97 int destination_port, | 55 int destination_port, |
| 98 scoped_ptr<NativeProcessLauncher> launcher) | 56 scoped_ptr<NativeProcessLauncher> launcher) |
| 99 : weak_client_ui_(weak_client_ui), | 57 : weak_client_ui_(weak_client_ui), |
| 100 source_extension_id_(source_extension_id), | 58 source_extension_id_(source_extension_id), |
| 101 native_host_name_(native_host_name), | 59 native_host_name_(native_host_name), |
| 102 destination_port_(destination_port), | 60 destination_port_(destination_port), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 116 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, | 74 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, |
| 117 base::Unretained(this))); | 75 base::Unretained(this))); |
| 118 } | 76 } |
| 119 | 77 |
| 120 NativeMessageProcessHost::~NativeMessageProcessHost() { | 78 NativeMessageProcessHost::~NativeMessageProcessHost() { |
| 121 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 79 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 122 Close(std::string()); | 80 Close(std::string()); |
| 123 } | 81 } |
| 124 | 82 |
| 125 // static | 83 // static |
| 126 scoped_ptr<NativeMessageProcessHost> NativeMessageProcessHost::Create( | 84 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( |
| 127 gfx::NativeView native_view, | 85 gfx::NativeView native_view, |
| 128 base::WeakPtr<Client> weak_client_ui, | 86 base::WeakPtr<Client> weak_client_ui, |
| 129 const std::string& source_extension_id, | 87 const std::string& source_extension_id, |
| 130 const std::string& native_host_name, | 88 const std::string& native_host_name, |
| 131 int destination_port, | 89 int destination_port, |
| 132 bool allow_user_level) { | 90 bool allow_user_level) { |
| 133 return CreateWithLauncher(weak_client_ui, source_extension_id, | 91 return NativeMessageProcessHost::CreateWithLauncher( |
| 134 native_host_name, destination_port, | 92 weak_client_ui, |
| 135 NativeProcessLauncher::CreateDefault( | 93 source_extension_id, |
| 136 allow_user_level, native_view)); | 94 native_host_name, |
| 95 destination_port, |
| 96 NativeProcessLauncher::CreateDefault(allow_user_level, native_view)); |
| 137 } | 97 } |
| 138 | 98 |
| 139 // static | 99 // static |
| 140 scoped_ptr<NativeMessageProcessHost> | 100 scoped_ptr<NativeMessageHost> |
| 141 NativeMessageProcessHost::CreateWithLauncher( | 101 NativeMessageProcessHost::CreateWithLauncher( |
| 142 base::WeakPtr<Client> weak_client_ui, | 102 base::WeakPtr<Client> weak_client_ui, |
| 143 const std::string& source_extension_id, | 103 const std::string& source_extension_id, |
| 144 const std::string& native_host_name, | 104 const std::string& native_host_name, |
| 145 int destination_port, | 105 int destination_port, |
| 146 scoped_ptr<NativeProcessLauncher> launcher) { | 106 scoped_ptr<NativeProcessLauncher> launcher) { |
| 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 148 | 108 |
| 149 scoped_ptr<NativeMessageProcessHost> process(new NativeMessageProcessHost( | 109 scoped_ptr<NativeMessageHost> process(new NativeMessageProcessHost( |
| 150 weak_client_ui, source_extension_id, native_host_name, | 110 weak_client_ui, source_extension_id, native_host_name, |
| 151 destination_port, launcher.Pass())); | 111 destination_port, launcher.Pass())); |
| 152 | 112 |
| 153 return process.Pass(); | 113 return process.Pass(); |
| 154 } | 114 } |
| 155 | 115 |
| 156 void NativeMessageProcessHost::LaunchHostProcess() { | 116 void NativeMessageProcessHost::LaunchHostProcess() { |
| 157 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 117 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 158 | 118 |
| 159 GURL origin(std::string(kExtensionScheme) + "://" + source_extension_id_); | 119 GURL origin(std::string(kExtensionScheme) + "://" + source_extension_id_); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 LOG(ERROR) << "Native Messaging host tried sending a message that is " | 282 LOG(ERROR) << "Native Messaging host tried sending a message that is " |
| 323 << message_size << " bytes long."; | 283 << message_size << " bytes long."; |
| 324 Close(kHostInputOuputError); | 284 Close(kHostInputOuputError); |
| 325 return; | 285 return; |
| 326 } | 286 } |
| 327 | 287 |
| 328 if (incoming_data_.size() < message_size + kMessageHeaderSize) | 288 if (incoming_data_.size() < message_size + kMessageHeaderSize) |
| 329 return; | 289 return; |
| 330 | 290 |
| 331 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 291 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 332 base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, | 292 base::Bind(&Client::PostMessageFromNative, weak_client_ui_, |
| 333 destination_port_, | 293 destination_port_, |
| 334 incoming_data_.substr(kMessageHeaderSize, message_size))); | 294 incoming_data_.substr(kMessageHeaderSize, message_size))); |
| 335 | 295 |
| 336 incoming_data_.erase(0, kMessageHeaderSize + message_size); | 296 incoming_data_.erase(0, kMessageHeaderSize + message_size); |
| 337 } | 297 } |
| 338 } | 298 } |
| 339 | 299 |
| 340 void NativeMessageProcessHost::DoWrite() { | 300 void NativeMessageProcessHost::DoWrite() { |
| 341 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 301 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 342 | 302 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 content::BrowserThread::PostBlockingPoolTask( | 365 content::BrowserThread::PostBlockingPoolTask( |
| 406 FROM_HERE, base::Bind(&base::EnsureProcessTerminated, process_handle_)); | 366 FROM_HERE, base::Bind(&base::EnsureProcessTerminated, process_handle_)); |
| 407 #else | 367 #else |
| 408 base::EnsureProcessTerminated(process_handle_); | 368 base::EnsureProcessTerminated(process_handle_); |
| 409 #endif | 369 #endif |
| 410 process_handle_ = base::kNullProcessHandle; | 370 process_handle_ = base::kNullProcessHandle; |
| 411 } | 371 } |
| 412 } | 372 } |
| 413 | 373 |
| 414 } // namespace extensions | 374 } // namespace extensions |
| OLD | NEW |