| 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_number_conversions.h" |
| 15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 16 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 17 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" | 17 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" |
| 18 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 22 | 22 |
| 23 #if defined(OS_WIN) |
| 24 #include "ui/views/win/hwnd_util.h" |
| 25 #endif |
| 26 |
| 23 namespace extensions { | 27 namespace extensions { |
| 24 | 28 |
| 25 namespace { | 29 namespace { |
| 26 | 30 |
| 27 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
| 28 // Name of the command line switch used to pass handle of the native view to | 32 // Name of the command line switch used to pass handle of the native view to |
| 29 // the native messaging host. | 33 // the native messaging host. |
| 30 const char kParentWindowSwitchName[] = "parent-window"; | 34 const char kParentWindowSwitchName[] = "parent-window"; |
| 31 #endif // defined(OS_WIN) | 35 #endif // defined(OS_WIN) |
| 32 | 36 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 49 return base::FilePath::FromUTF8Unsafe(key_and_value[1]); | 53 return base::FilePath::FromUTF8Unsafe(key_and_value[1]); |
| 50 } | 54 } |
| 51 | 55 |
| 52 return base::FilePath(); | 56 return base::FilePath(); |
| 53 } | 57 } |
| 54 | 58 |
| 55 | 59 |
| 56 // Default implementation on NativeProcessLauncher interface. | 60 // Default implementation on NativeProcessLauncher interface. |
| 57 class NativeProcessLauncherImpl : public NativeProcessLauncher { | 61 class NativeProcessLauncherImpl : public NativeProcessLauncher { |
| 58 public: | 62 public: |
| 59 explicit NativeProcessLauncherImpl(gfx::NativeView native_view); | 63 explicit NativeProcessLauncherImpl(intptr_t native_window); |
| 60 virtual ~NativeProcessLauncherImpl(); | 64 virtual ~NativeProcessLauncherImpl(); |
| 61 | 65 |
| 62 virtual void Launch(const GURL& origin, | 66 virtual void Launch(const GURL& origin, |
| 63 const std::string& native_host_name, | 67 const std::string& native_host_name, |
| 64 LaunchedCallback callback) const OVERRIDE; | 68 LaunchedCallback callback) const OVERRIDE; |
| 65 | 69 |
| 66 private: | 70 private: |
| 67 class Core : public base::RefCountedThreadSafe<Core> { | 71 class Core : public base::RefCountedThreadSafe<Core> { |
| 68 public: | 72 public: |
| 69 explicit Core(gfx::NativeView native_view); | 73 explicit Core(intptr_t native_window); |
| 70 void Launch(const GURL& origin, | 74 void Launch(const GURL& origin, |
| 71 const std::string& native_host_name, | 75 const std::string& native_host_name, |
| 72 LaunchedCallback callback); | 76 LaunchedCallback callback); |
| 73 void Detach(); | 77 void Detach(); |
| 74 | 78 |
| 75 private: | 79 private: |
| 76 friend class base::RefCountedThreadSafe<Core>; | 80 friend class base::RefCountedThreadSafe<Core>; |
| 77 virtual ~Core(); | 81 virtual ~Core(); |
| 78 | 82 |
| 79 void DoLaunchOnThreadPool(const GURL& origin, | 83 void DoLaunchOnThreadPool(const GURL& origin, |
| 80 const std::string& native_host_name, | 84 const std::string& native_host_name, |
| 81 LaunchedCallback callback); | 85 LaunchedCallback callback); |
| 82 void PostErrorResult(const LaunchedCallback& callback, LaunchResult error); | 86 void PostErrorResult(const LaunchedCallback& callback, LaunchResult error); |
| 83 void PostResult(const LaunchedCallback& callback, | 87 void PostResult(const LaunchedCallback& callback, |
| 84 base::ProcessHandle process_handle, | 88 base::ProcessHandle process_handle, |
| 85 base::PlatformFile read_file, | 89 base::PlatformFile read_file, |
| 86 base::PlatformFile write_file); | 90 base::PlatformFile write_file); |
| 87 void CallCallbackOnIOThread(LaunchedCallback callback, | 91 void CallCallbackOnIOThread(LaunchedCallback callback, |
| 88 LaunchResult result, | 92 LaunchResult result, |
| 89 base::ProcessHandle process_handle, | 93 base::ProcessHandle process_handle, |
| 90 base::PlatformFile read_file, | 94 base::PlatformFile read_file, |
| 91 base::PlatformFile write_file); | 95 base::PlatformFile write_file); |
| 92 | 96 |
| 93 bool detached_; | 97 bool detached_; |
| 94 | 98 |
| 95 // Handle of the native view corrsponding to the extension. | 99 // Handle of the native window corresponding to the extension. |
| 96 gfx::NativeView native_view_; | 100 intptr_t window_handle_; |
| 97 | 101 |
| 98 DISALLOW_COPY_AND_ASSIGN(Core); | 102 DISALLOW_COPY_AND_ASSIGN(Core); |
| 99 }; | 103 }; |
| 100 | 104 |
| 101 scoped_refptr<Core> core_; | 105 scoped_refptr<Core> core_; |
| 102 | 106 |
| 103 DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncherImpl); | 107 DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncherImpl); |
| 104 }; | 108 }; |
| 105 | 109 |
| 106 NativeProcessLauncherImpl::Core::Core(gfx::NativeView native_view) | 110 NativeProcessLauncherImpl::Core::Core(intptr_t window_handle) |
| 107 : detached_(false), | 111 : detached_(false), |
| 108 native_view_(native_view) { | 112 window_handle_(window_handle) { |
| 109 } | 113 } |
| 110 | 114 |
| 111 NativeProcessLauncherImpl::Core::~Core() { | 115 NativeProcessLauncherImpl::Core::~Core() { |
| 112 DCHECK(detached_); | 116 DCHECK(detached_); |
| 113 } | 117 } |
| 114 | 118 |
| 115 void NativeProcessLauncherImpl::Core::Detach() { | 119 void NativeProcessLauncherImpl::Core::Detach() { |
| 116 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 117 detached_ = true; | 121 detached_ = true; |
| 118 } | 122 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 return; | 193 return; |
| 190 #endif // !defined(OS_WIN) | 194 #endif // !defined(OS_WIN) |
| 191 } | 195 } |
| 192 | 196 |
| 193 CommandLine command_line(host_path); | 197 CommandLine command_line(host_path); |
| 194 command_line.AppendArg(origin.spec()); | 198 command_line.AppendArg(origin.spec()); |
| 195 | 199 |
| 196 // Pass handle of the native view window to the native messaging host. This | 200 // Pass handle of the native view window to the native messaging host. This |
| 197 // way the host will be able to create properly focused UI windows. | 201 // way the host will be able to create properly focused UI windows. |
| 198 #if defined(OS_WIN) | 202 #if defined(OS_WIN) |
| 199 int64 window_handle = reinterpret_cast<intptr_t>(native_view_); | |
| 200 command_line.AppendSwitchASCII(kParentWindowSwitchName, | 203 command_line.AppendSwitchASCII(kParentWindowSwitchName, |
| 201 base::Int64ToString(window_handle)); | 204 base::Int64ToString(window_handle_)); |
| 202 #endif // !defined(OS_WIN) | 205 #endif // !defined(OS_WIN) |
| 203 | 206 |
| 204 base::ProcessHandle process_handle; | 207 base::ProcessHandle process_handle; |
| 205 base::PlatformFile read_file; | 208 base::PlatformFile read_file; |
| 206 base::PlatformFile write_file; | 209 base::PlatformFile write_file; |
| 207 if (NativeProcessLauncher::LaunchNativeProcess( | 210 if (NativeProcessLauncher::LaunchNativeProcess( |
| 208 command_line, &process_handle, &read_file, &write_file)) { | 211 command_line, &process_handle, &read_file, &write_file)) { |
| 209 PostResult(callback, process_handle, read_file, write_file); | 212 PostResult(callback, process_handle, read_file, write_file); |
| 210 } else { | 213 } else { |
| 211 PostErrorResult(callback, RESULT_FAILED_TO_START); | 214 PostErrorResult(callback, RESULT_FAILED_TO_START); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 base::ProcessHandle process_handle, | 249 base::ProcessHandle process_handle, |
| 247 base::PlatformFile read_file, | 250 base::PlatformFile read_file, |
| 248 base::PlatformFile write_file) { | 251 base::PlatformFile write_file) { |
| 249 content::BrowserThread::PostTask( | 252 content::BrowserThread::PostTask( |
| 250 content::BrowserThread::IO, FROM_HERE, | 253 content::BrowserThread::IO, FROM_HERE, |
| 251 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, this, | 254 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, this, |
| 252 callback, RESULT_SUCCESS, process_handle, read_file, | 255 callback, RESULT_SUCCESS, process_handle, read_file, |
| 253 write_file)); | 256 write_file)); |
| 254 } | 257 } |
| 255 | 258 |
| 256 NativeProcessLauncherImpl::NativeProcessLauncherImpl( | 259 NativeProcessLauncherImpl::NativeProcessLauncherImpl(intptr_t window_handle) |
| 257 gfx::NativeView native_view) | 260 : core_(new Core(window_handle)) { |
| 258 : core_(new Core(native_view)) { | |
| 259 } | 261 } |
| 260 | 262 |
| 261 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() { | 263 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() { |
| 262 core_->Detach(); | 264 core_->Detach(); |
| 263 } | 265 } |
| 264 | 266 |
| 265 void NativeProcessLauncherImpl::Launch(const GURL& origin, | 267 void NativeProcessLauncherImpl::Launch(const GURL& origin, |
| 266 const std::string& native_host_name, | 268 const std::string& native_host_name, |
| 267 LaunchedCallback callback) const { | 269 LaunchedCallback callback) const { |
| 268 core_->Launch(origin, native_host_name, callback); | 270 core_->Launch(origin, native_host_name, callback); |
| 269 } | 271 } |
| 270 | 272 |
| 271 } // namespace | 273 } // namespace |
| 272 | 274 |
| 273 // static | 275 // static |
| 274 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault( | 276 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault( |
| 275 gfx::NativeView native_view) { | 277 gfx::NativeView native_view) { |
| 278 intptr_t window_handle = 0; |
| 279 #if defined(OS_WIN) |
| 280 window_handle = reinterpret_cast<intptr_t>( |
| 281 views::HWNDForNativeView(native_view)); |
| 282 #endif |
| 276 return scoped_ptr<NativeProcessLauncher>( | 283 return scoped_ptr<NativeProcessLauncher>( |
| 277 new NativeProcessLauncherImpl(native_view)); | 284 new NativeProcessLauncherImpl(window_handle)); |
| 278 } | 285 } |
| 279 | 286 |
| 280 } // namespace extensions | 287 } // namespace extensions |
| OLD | NEW |