Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Side by Side Diff: chrome/browser/extensions/api/messaging/native_process_launcher.cc

Issue 22532011: Pass handle of the native view window to the native messaging host. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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";
Sergey Ulanov 2013/08/08 18:22:37 This is not used anywhere. Can you please remove i
alexeypa (please no reviews) 2013/08/08 23:57:41 Done.
28 const char kNativeViewSwitchName[] = "native_view";
Sergey Ulanov 2013/08/08 18:22:37 Maybe call it "parent-hwnd" or something like that
alexeypa (please no reviews) 2013/08/08 23:57:41 Done. --parent-window
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::NativeView 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::NativeView 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::NativeView 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::NativeView 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
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. This
192 // way the host will be able to create properly focused UI windows.
193 #if defined(OS_WIN)
194 int64 window_handle = reinterpret_cast<intptr_t>(native_view_);
195 command_line.AppendSwitchASCII(kNativeViewSwitchName,
196 base::Int64ToString(window_handle));
197 #endif // !defined(OS_WIN)
198
185 base::PlatformFile read_file; 199 base::PlatformFile read_file;
186 base::PlatformFile write_file; 200 base::PlatformFile write_file;
187 if (NativeProcessLauncher::LaunchNativeProcess( 201 if (NativeProcessLauncher::LaunchNativeProcess(
188 command_line, &read_file, &write_file)) { 202 command_line, &read_file, &write_file)) {
189 PostResult(callback, read_file, write_file); 203 PostResult(callback, read_file, write_file);
190 } else { 204 } else {
191 PostErrorResult(callback, RESULT_FAILED_TO_START); 205 PostErrorResult(callback, RESULT_FAILED_TO_START);
192 } 206 }
193 } 207 }
194 208
(...skipping 28 matching lines...) Expand all
223 void NativeProcessLauncherImpl::Core::PostResult( 237 void NativeProcessLauncherImpl::Core::PostResult(
224 const LaunchedCallback& callback, 238 const LaunchedCallback& callback,
225 base::PlatformFile read_file, 239 base::PlatformFile read_file,
226 base::PlatformFile write_file) { 240 base::PlatformFile write_file) {
227 content::BrowserThread::PostTask( 241 content::BrowserThread::PostTask(
228 content::BrowserThread::IO, FROM_HERE, 242 content::BrowserThread::IO, FROM_HERE,
229 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, 243 base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread,
230 this, callback, RESULT_SUCCESS, read_file, write_file)); 244 this, callback, RESULT_SUCCESS, read_file, write_file));
231 } 245 }
232 246
233 NativeProcessLauncherImpl::NativeProcessLauncherImpl() 247 NativeProcessLauncherImpl::NativeProcessLauncherImpl(
234 : core_(new Core()) { 248 gfx::NativeView native_view)
249 : core_(new Core(native_view)) {
235 } 250 }
236 251
237 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() { 252 NativeProcessLauncherImpl::~NativeProcessLauncherImpl() {
238 core_->Detach(); 253 core_->Detach();
239 } 254 }
240 255
241 void NativeProcessLauncherImpl::Launch(const GURL& origin, 256 void NativeProcessLauncherImpl::Launch(const GURL& origin,
242 const std::string& native_host_name, 257 const std::string& native_host_name,
243 LaunchedCallback callback) const { 258 LaunchedCallback callback) const {
244 core_->Launch(origin, native_host_name, callback); 259 core_->Launch(origin, native_host_name, callback);
245 } 260 }
246 261
247 } // namespace 262 } // namespace
248 263
249 // static 264 // static
250 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault() { 265 scoped_ptr<NativeProcessLauncher> NativeProcessLauncher::CreateDefault(
251 return scoped_ptr<NativeProcessLauncher>(new NativeProcessLauncherImpl()); 266 gfx::NativeView native_view) {
267 return scoped_ptr<NativeProcessLauncher>(
268 new NativeProcessLauncherImpl(native_view));
252 } 269 }
253 270
254 } // namespace extensions 271 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698