Chromium Code Reviews| 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 const char kNativeHostsDirectoryName[] = "native_hosts"; |
| 28 const char kNativeViewSwitchName[] = "native_view"; | |
| 27 | 29 |
| 28 base::FilePath GetHostManifestPathFromCommandLine( | 30 base::FilePath GetHostManifestPathFromCommandLine( |
| 29 const std::string& native_host_name) { | 31 const std::string& native_host_name) { |
| 30 const std::string& value = | 32 const std::string& value = |
| 31 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 33 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 32 switches::kNativeMessagingHosts); | 34 switches::kNativeMessagingHosts); |
| 33 if (value.empty()) | 35 if (value.empty()) |
| 34 return base::FilePath(); | 36 return base::FilePath(); |
| 35 | 37 |
| 36 std::vector<std::string> hosts; | 38 std::vector<std::string> hosts; |
| 37 base::SplitString(value, ',', &hosts); | 39 base::SplitString(value, ',', &hosts); |
| 38 for (size_t i = 0; i < hosts.size(); ++i) { | 40 for (size_t i = 0; i < hosts.size(); ++i) { |
| 39 std::vector<std::string> key_and_value; | 41 std::vector<std::string> key_and_value; |
| 40 base::SplitString(hosts[i], '=', &key_and_value); | 42 base::SplitString(hosts[i], '=', &key_and_value); |
| 41 if (key_and_value.size() != 2) | 43 if (key_and_value.size() != 2) |
| 42 continue; | 44 continue; |
| 43 if (key_and_value[0] == native_host_name) | 45 if (key_and_value[0] == native_host_name) |
| 44 return base::FilePath::FromUTF8Unsafe(key_and_value[1]); | 46 return base::FilePath::FromUTF8Unsafe(key_and_value[1]); |
| 45 } | 47 } |
| 46 | 48 |
| 47 return base::FilePath(); | 49 return base::FilePath(); |
| 48 } | 50 } |
| 49 | 51 |
| 50 | 52 |
| 51 // Default implementation on NativeProcessLauncher interface. | 53 // Default implementation on NativeProcessLauncher interface. |
| 52 class NativeProcessLauncherImpl : public NativeProcessLauncher { | 54 class NativeProcessLauncherImpl : public NativeProcessLauncher { |
| 53 public: | 55 public: |
| 54 NativeProcessLauncherImpl(); | 56 explicit NativeProcessLauncherImpl(gfx::NativeWindow native_view); |
| 55 virtual ~NativeProcessLauncherImpl(); | 57 virtual ~NativeProcessLauncherImpl(); |
| 56 | 58 |
| 57 virtual void Launch(const GURL& origin, | 59 virtual void Launch(const GURL& origin, |
| 58 const std::string& native_host_name, | 60 const std::string& native_host_name, |
| 59 LaunchedCallback callback) const OVERRIDE; | 61 LaunchedCallback callback) const OVERRIDE; |
| 60 | 62 |
| 61 private: | 63 private: |
| 62 class Core : public base::RefCountedThreadSafe<Core> { | 64 class Core : public base::RefCountedThreadSafe<Core> { |
| 63 public: | 65 public: |
| 64 Core(); | 66 explicit Core(gfx::NativeWindow native_view); |
| 65 void Launch(const GURL& origin, | 67 void Launch(const GURL& origin, |
| 66 const std::string& native_host_name, | 68 const std::string& native_host_name, |
| 67 LaunchedCallback callback); | 69 LaunchedCallback callback); |
| 68 void Detach(); | 70 void Detach(); |
| 69 | 71 |
| 70 private: | 72 private: |
| 71 friend class base::RefCountedThreadSafe<Core>; | 73 friend class base::RefCountedThreadSafe<Core>; |
| 72 virtual ~Core(); | 74 virtual ~Core(); |
| 73 | 75 |
| 74 void DoLaunchOnThreadPool(const GURL& origin, | 76 void DoLaunchOnThreadPool(const GURL& origin, |
| 75 const std::string& native_host_name, | 77 const std::string& native_host_name, |
| 76 LaunchedCallback callback); | 78 LaunchedCallback callback); |
| 77 void PostErrorResult(const LaunchedCallback& callback, LaunchResult error); | 79 void PostErrorResult(const LaunchedCallback& callback, LaunchResult error); |
| 78 void PostResult(const LaunchedCallback& callback, | 80 void PostResult(const LaunchedCallback& callback, |
| 79 base::PlatformFile read_file, | 81 base::PlatformFile read_file, |
| 80 base::PlatformFile write_file); | 82 base::PlatformFile write_file); |
| 81 void CallCallbackOnIOThread(LaunchedCallback callback, | 83 void CallCallbackOnIOThread(LaunchedCallback callback, |
| 82 LaunchResult result, | 84 LaunchResult result, |
| 83 base::PlatformFile read_file, | 85 base::PlatformFile read_file, |
| 84 base::PlatformFile write_file); | 86 base::PlatformFile write_file); |
| 85 | 87 |
| 86 bool detached_; | 88 bool detached_; |
| 87 | 89 |
| 90 // Handle of the native view corrsponding to the extension. | |
| 91 gfx::NativeWindow native_view_; | |
| 92 | |
| 88 DISALLOW_COPY_AND_ASSIGN(Core); | 93 DISALLOW_COPY_AND_ASSIGN(Core); |
| 89 }; | 94 }; |
| 90 | 95 |
| 91 scoped_refptr<Core> core_; | 96 scoped_refptr<Core> core_; |
| 92 | 97 |
| 93 DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncherImpl); | 98 DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncherImpl); |
| 94 }; | 99 }; |
| 95 | 100 |
| 96 NativeProcessLauncherImpl::Core::Core() | 101 NativeProcessLauncherImpl::Core::Core(gfx::NativeWindow native_view) |
| 97 : detached_(false) { | 102 : detached_(false), |
| 103 native_view_(native_view) { | |
| 98 } | 104 } |
| 99 | 105 |
| 100 NativeProcessLauncherImpl::Core::~Core() { | 106 NativeProcessLauncherImpl::Core::~Core() { |
| 101 DCHECK(detached_); | 107 DCHECK(detached_); |
| 102 } | 108 } |
| 103 | 109 |
| 104 void NativeProcessLauncherImpl::Core::Detach() { | 110 void NativeProcessLauncherImpl::Core::Detach() { |
| 105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 106 detached_ = true; | 112 detached_ = true; |
| 107 } | 113 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 LOG(ERROR) << "Native messaging host path must be absolute for " | 181 LOG(ERROR) << "Native messaging host path must be absolute for " |
| 176 << native_host_name; | 182 << native_host_name; |
| 177 PostErrorResult(callback, RESULT_NOT_FOUND); | 183 PostErrorResult(callback, RESULT_NOT_FOUND); |
| 178 return; | 184 return; |
| 179 #endif // !defined(OS_WIN) | 185 #endif // !defined(OS_WIN) |
| 180 } | 186 } |
| 181 | 187 |
| 182 CommandLine command_line(host_path); | 188 CommandLine command_line(host_path); |
| 183 command_line.AppendArg(origin.spec()); | 189 command_line.AppendArg(origin.spec()); |
| 184 | 190 |
| 191 // Pass handle of the native view window to the native messaging host. | |
|
Sergey Ulanov
2013/08/07 22:51:20
explain why it can be useful please.
alexeypa (please no reviews)
2013/08/08 16:57:32
Done.
| |
| 192 #if defined(OS_WIN) | |
| 193 int64 window_handle = reinterpret_cast<intptr_t>(native_view_); | |
| 194 command_line.AppendSwitchASCII(kNativeViewSwitchName, | |
| 195 base::Int64ToString(window_handle)); | |
|
Sergey Ulanov
2013/08/07 22:51:20
Maybe use hexadecimal form instead of decimal numb
alexeypa (please no reviews)
2013/08/08 16:57:32
Parsing a decimal value is typically more common (
Sergey Ulanov
2013/08/08 18:22:37
Yes, I was thinking about doing it without the pre
alexeypa (please no reviews)
2013/08/08 23:57:41
Pointers are signed too. (void*)0xffffffff is smal
Sergey Ulanov
2013/08/09 01:00:52
AFAIK pointer ordering is undefined, so it depends
| |
| 196 #endif // !defined(OS_WIN) | |
| 197 | |
| 185 base::PlatformFile read_file; | 198 base::PlatformFile read_file; |
| 186 base::PlatformFile write_file; | 199 base::PlatformFile write_file; |
| 187 if (NativeProcessLauncher::LaunchNativeProcess( | 200 if (NativeProcessLauncher::LaunchNativeProcess( |
| 188 command_line, &read_file, &write_file)) { | 201 command_line, &read_file, &write_file)) { |
| 189 PostResult(callback, read_file, write_file); | 202 PostResult(callback, read_file, write_file); |
| 190 } else { | 203 } else { |
| 191 PostErrorResult(callback, RESULT_FAILED_TO_START); | 204 PostErrorResult(callback, RESULT_FAILED_TO_START); |
| 192 } | 205 } |
| 193 } | 206 } |
| 194 | 207 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 223 void NativeProcessLauncherImpl::Core::PostResult( | 236 void NativeProcessLauncherImpl::Core::PostResult( |
| 224 const LaunchedCallback& callback, | 237 const LaunchedCallback& callback, |
| 225 base::PlatformFile read_file, | 238 base::PlatformFile read_file, |
| 226 base::PlatformFile write_file) { | 239 base::PlatformFile write_file) { |
| 227 content::BrowserThread::PostTask( | 240 content::BrowserThread::PostTask( |
| 228 content::BrowserThread::IO, FROM_HERE, | 241 content::BrowserThread::IO, FROM_HERE, |
| 229 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, | 242 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, |
| 230 this, callback, RESULT_SUCCESS, read_file, write_file)); | 243 this, callback, RESULT_SUCCESS, read_file, write_file)); |
| 231 } | 244 } |
| 232 | 245 |
| 233 NativeProcessLauncherImpl::NativeProcessLauncherImpl() | 246 NativeProcessLauncherImpl::NativeProcessLauncherImpl( |
| 234 : core_(new Core()) { | 247 gfx::NativeWindow native_view) |
| 248 : core_(new Core(native_view)) { | |
| 235 } | 249 } |
| 236 | 250 |
| 237 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() { | 251 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() { |
| 238 core_->Detach(); | 252 core_->Detach(); |
| 239 } | 253 } |
| 240 | 254 |
| 241 void NativeProcessLauncherImpl::Launch(const GURL& origin, | 255 void NativeProcessLauncherImpl::Launch(const GURL& origin, |
| 242 const std::string& native_host_name, | 256 const std::string& native_host_name, |
| 243 LaunchedCallback callback) const { | 257 LaunchedCallback callback) const { |
| 244 core_->Launch(origin, native_host_name, callback); | 258 core_->Launch(origin, native_host_name, callback); |
| 245 } | 259 } |
| 246 | 260 |
| 247 } // namespace | 261 } // namespace |
| 248 | 262 |
| 249 // static | 263 // static |
| 250 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault() { | 264 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault( |
| 251 return scoped_ptr<NativeProcessLauncher>(new NativeProcessLauncherImpl()); | 265 gfx::NativeWindow native_view) { |
| 266 return scoped_ptr<NativeProcessLauncher>( | |
| 267 new NativeProcessLauncherImpl(native_view)); | |
| 252 } | 268 } |
| 253 | 269 |
| 254 } // namespace extensions | 270 } // namespace extensions |
| OLD | NEW |