| 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_process_launcher.h" | 5 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 16 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" | 17 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" |
| 17 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 21 | 22 |
| 22 namespace extensions { | 23 namespace extensions { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 const char kNativeHostsDirectoryName[] = "native_hosts"; | 27 // Name of the command line switch used to pass handle of the native view to |
| 28 // the native messaging host. |
| 29 const char kParentWindowSwitchName[] = "parent-window"; |
| 27 | 30 |
| 28 base::FilePath GetHostManifestPathFromCommandLine( | 31 base::FilePath GetHostManifestPathFromCommandLine( |
| 29 const std::string& native_host_name) { | 32 const std::string& native_host_name) { |
| 30 const std::string& value = | 33 const std::string& value = |
| 31 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 34 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 32 switches::kNativeMessagingHosts); | 35 switches::kNativeMessagingHosts); |
| 33 if (value.empty()) | 36 if (value.empty()) |
| 34 return base::FilePath(); | 37 return base::FilePath(); |
| 35 | 38 |
| 36 std::vector<std::string> hosts; | 39 std::vector<std::string> hosts; |
| 37 base::SplitString(value, ',', &hosts); | 40 base::SplitString(value, ',', &hosts); |
| 38 for (size_t i = 0; i < hosts.size(); ++i) { | 41 for (size_t i = 0; i < hosts.size(); ++i) { |
| 39 std::vector<std::string> key_and_value; | 42 std::vector<std::string> key_and_value; |
| 40 base::SplitString(hosts[i], '=', &key_and_value); | 43 base::SplitString(hosts[i], '=', &key_and_value); |
| 41 if (key_and_value.size() != 2) | 44 if (key_and_value.size() != 2) |
| 42 continue; | 45 continue; |
| 43 if (key_and_value[0] == native_host_name) | 46 if (key_and_value[0] == native_host_name) |
| 44 return base::FilePath::FromUTF8Unsafe(key_and_value[1]); | 47 return base::FilePath::FromUTF8Unsafe(key_and_value[1]); |
| 45 } | 48 } |
| 46 | 49 |
| 47 return base::FilePath(); | 50 return base::FilePath(); |
| 48 } | 51 } |
| 49 | 52 |
| 50 | 53 |
| 51 // Default implementation on NativeProcessLauncher interface. | 54 // Default implementation on NativeProcessLauncher interface. |
| 52 class NativeProcessLauncherImpl : public NativeProcessLauncher { | 55 class NativeProcessLauncherImpl : public NativeProcessLauncher { |
| 53 public: | 56 public: |
| 54 NativeProcessLauncherImpl(); | 57 explicit NativeProcessLauncherImpl(gfx::NativeView native_view); |
| 55 virtual ~NativeProcessLauncherImpl(); | 58 virtual ~NativeProcessLauncherImpl(); |
| 56 | 59 |
| 57 virtual void Launch(const GURL& origin, | 60 virtual void Launch(const GURL& origin, |
| 58 const std::string& native_host_name, | 61 const std::string& native_host_name, |
| 59 LaunchedCallback callback) const OVERRIDE; | 62 LaunchedCallback callback) const OVERRIDE; |
| 60 | 63 |
| 61 private: | 64 private: |
| 62 class Core : public base::RefCountedThreadSafe<Core> { | 65 class Core : public base::RefCountedThreadSafe<Core> { |
| 63 public: | 66 public: |
| 64 Core(); | 67 explicit Core(gfx::NativeView native_view); |
| 65 void Launch(const GURL& origin, | 68 void Launch(const GURL& origin, |
| 66 const std::string& native_host_name, | 69 const std::string& native_host_name, |
| 67 LaunchedCallback callback); | 70 LaunchedCallback callback); |
| 68 void Detach(); | 71 void Detach(); |
| 69 | 72 |
| 70 private: | 73 private: |
| 71 friend class base::RefCountedThreadSafe<Core>; | 74 friend class base::RefCountedThreadSafe<Core>; |
| 72 virtual ~Core(); | 75 virtual ~Core(); |
| 73 | 76 |
| 74 void DoLaunchOnThreadPool(const GURL& origin, | 77 void DoLaunchOnThreadPool(const GURL& origin, |
| 75 const std::string& native_host_name, | 78 const std::string& native_host_name, |
| 76 LaunchedCallback callback); | 79 LaunchedCallback callback); |
| 77 void PostErrorResult(const LaunchedCallback& callback, LaunchResult error); | 80 void PostErrorResult(const LaunchedCallback& callback, LaunchResult error); |
| 78 void PostResult(const LaunchedCallback& callback, | 81 void PostResult(const LaunchedCallback& callback, |
| 79 base::PlatformFile read_file, | 82 base::PlatformFile read_file, |
| 80 base::PlatformFile write_file); | 83 base::PlatformFile write_file); |
| 81 void CallCallbackOnIOThread(LaunchedCallback callback, | 84 void CallCallbackOnIOThread(LaunchedCallback callback, |
| 82 LaunchResult result, | 85 LaunchResult result, |
| 83 base::PlatformFile read_file, | 86 base::PlatformFile read_file, |
| 84 base::PlatformFile write_file); | 87 base::PlatformFile write_file); |
| 85 | 88 |
| 86 bool detached_; | 89 bool detached_; |
| 87 | 90 |
| 91 // Handle of the native view corrsponding to the extension. |
| 92 gfx::NativeView native_view_; |
| 93 |
| 88 DISALLOW_COPY_AND_ASSIGN(Core); | 94 DISALLOW_COPY_AND_ASSIGN(Core); |
| 89 }; | 95 }; |
| 90 | 96 |
| 91 scoped_refptr<Core> core_; | 97 scoped_refptr<Core> core_; |
| 92 | 98 |
| 93 DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncherImpl); | 99 DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncherImpl); |
| 94 }; | 100 }; |
| 95 | 101 |
| 96 NativeProcessLauncherImpl::Core::Core() | 102 NativeProcessLauncherImpl::Core::Core(gfx::NativeView native_view) |
| 97 : detached_(false) { | 103 : detached_(false), |
| 104 native_view_(native_view) { |
| 98 } | 105 } |
| 99 | 106 |
| 100 NativeProcessLauncherImpl::Core::~Core() { | 107 NativeProcessLauncherImpl::Core::~Core() { |
| 101 DCHECK(detached_); | 108 DCHECK(detached_); |
| 102 } | 109 } |
| 103 | 110 |
| 104 void NativeProcessLauncherImpl::Core::Detach() { | 111 void NativeProcessLauncherImpl::Core::Detach() { |
| 105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 112 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 106 detached_ = true; | 113 detached_ = true; |
| 107 } | 114 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 LOG(ERROR) << "Native messaging host path must be absolute for " | 182 LOG(ERROR) << "Native messaging host path must be absolute for " |
| 176 << native_host_name; | 183 << native_host_name; |
| 177 PostErrorResult(callback, RESULT_NOT_FOUND); | 184 PostErrorResult(callback, RESULT_NOT_FOUND); |
| 178 return; | 185 return; |
| 179 #endif // !defined(OS_WIN) | 186 #endif // !defined(OS_WIN) |
| 180 } | 187 } |
| 181 | 188 |
| 182 CommandLine command_line(host_path); | 189 CommandLine command_line(host_path); |
| 183 command_line.AppendArg(origin.spec()); | 190 command_line.AppendArg(origin.spec()); |
| 184 | 191 |
| 192 // Pass handle of the native view window to the native messaging host. This |
| 193 // way the host will be able to create properly focused UI windows. |
| 194 #if defined(OS_WIN) |
| 195 int64 window_handle = reinterpret_cast<intptr_t>(native_view_); |
| 196 command_line.AppendSwitchASCII(kParentWindowSwitchName, |
| 197 base::Int64ToString(window_handle)); |
| 198 #endif // !defined(OS_WIN) |
| 199 |
| 185 base::PlatformFile read_file; | 200 base::PlatformFile read_file; |
| 186 base::PlatformFile write_file; | 201 base::PlatformFile write_file; |
| 187 if (NativeProcessLauncher::LaunchNativeProcess( | 202 if (NativeProcessLauncher::LaunchNativeProcess( |
| 188 command_line, &read_file, &write_file)) { | 203 command_line, &read_file, &write_file)) { |
| 189 PostResult(callback, read_file, write_file); | 204 PostResult(callback, read_file, write_file); |
| 190 } else { | 205 } else { |
| 191 PostErrorResult(callback, RESULT_FAILED_TO_START); | 206 PostErrorResult(callback, RESULT_FAILED_TO_START); |
| 192 } | 207 } |
| 193 } | 208 } |
| 194 | 209 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 223 void NativeProcessLauncherImpl::Core::PostResult( | 238 void NativeProcessLauncherImpl::Core::PostResult( |
| 224 const LaunchedCallback& callback, | 239 const LaunchedCallback& callback, |
| 225 base::PlatformFile read_file, | 240 base::PlatformFile read_file, |
| 226 base::PlatformFile write_file) { | 241 base::PlatformFile write_file) { |
| 227 content::BrowserThread::PostTask( | 242 content::BrowserThread::PostTask( |
| 228 content::BrowserThread::IO, FROM_HERE, | 243 content::BrowserThread::IO, FROM_HERE, |
| 229 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, | 244 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, |
| 230 this, callback, RESULT_SUCCESS, read_file, write_file)); | 245 this, callback, RESULT_SUCCESS, read_file, write_file)); |
| 231 } | 246 } |
| 232 | 247 |
| 233 NativeProcessLauncherImpl::NativeProcessLauncherImpl() | 248 NativeProcessLauncherImpl::NativeProcessLauncherImpl( |
| 234 : core_(new Core()) { | 249 gfx::NativeView native_view) |
| 250 : core_(new Core(native_view)) { |
| 235 } | 251 } |
| 236 | 252 |
| 237 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() { | 253 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() { |
| 238 core_->Detach(); | 254 core_->Detach(); |
| 239 } | 255 } |
| 240 | 256 |
| 241 void NativeProcessLauncherImpl::Launch(const GURL& origin, | 257 void NativeProcessLauncherImpl::Launch(const GURL& origin, |
| 242 const std::string& native_host_name, | 258 const std::string& native_host_name, |
| 243 LaunchedCallback callback) const { | 259 LaunchedCallback callback) const { |
| 244 core_->Launch(origin, native_host_name, callback); | 260 core_->Launch(origin, native_host_name, callback); |
| 245 } | 261 } |
| 246 | 262 |
| 247 } // namespace | 263 } // namespace |
| 248 | 264 |
| 249 // static | 265 // static |
| 250 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault() { | 266 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault( |
| 251 return scoped_ptr<NativeProcessLauncher>(new NativeProcessLauncherImpl()); | 267 gfx::NativeView native_view) { |
| 268 return scoped_ptr<NativeProcessLauncher>( |
| 269 new NativeProcessLauncherImpl(native_view)); |
| 252 } | 270 } |
| 253 | 271 |
| 254 } // namespace extensions | 272 } // namespace extensions |
| OLD | NEW |