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