feat_add_data_transfer_to_requestsingleinstancelock.patch 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Raymond Zhao <[email protected]>
  3. Date: Tue, 7 Sep 2021 14:54:25 -0700
  4. Subject: feat: Add data transfer mechanism to requestSingleInstanceLock flow
  5. This patch adds code that allows for the second instance to send
  6. additional data to the first instance, and for the first instance
  7. to send additional data back to the second instance, during the
  8. app.requestSingleInstanceLock call.
  9. Firstly, this patch adds an additional_data parameter
  10. to the constructor of ProcessSingleton, so that the second instance
  11. can send additional data over to the first instance
  12. while requesting the ProcessSingleton lock.
  13. Then, we add additional processing to the second-instance event, both
  14. so the first instance can receive additional data from the second
  15. instance, but also so the second instance can send back additional
  16. data to the first instance if needed.
  17. diff --git a/chrome/browser/process_singleton.h b/chrome/browser/process_singleton.h
  18. index 5a64220aaf1309832dc0ad543e353de67fe0a779..5b701b1361707b610ed60c344e441e67ca701362 100644
  19. --- a/chrome/browser/process_singleton.h
  20. +++ b/chrome/browser/process_singleton.h
  21. @@ -18,6 +18,7 @@
  22. #include "base/files/file_path.h"
  23. #include "base/memory/ref_counted.h"
  24. #include "base/process/process.h"
  25. +#include "base/containers/span.h"
  26. #include "ui/gfx/native_widget_types.h"
  27. #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
  28. @@ -93,6 +94,9 @@ class ProcessSingleton {
  29. static constexpr int kNumNotifyResults = LAST_VALUE + 1;
  30. + using NotificationAckCallback =
  31. + base::RepeatingCallback<void(const base::span<const uint8_t>* ack_data)>;
  32. +
  33. // Implement this callback to handle notifications from other processes. The
  34. // callback will receive the command line and directory with which the other
  35. // Chrome process was launched. Return true if the command line will be
  36. @@ -100,21 +104,27 @@ class ProcessSingleton {
  37. // should handle it (i.e., because the current process is shutting down).
  38. using NotificationCallback =
  39. base::RepeatingCallback<bool(const base::CommandLine& command_line,
  40. - const base::FilePath& current_directory)>;
  41. + const base::FilePath& current_directory,
  42. + const std::vector<uint8_t> additional_data,
  43. + const NotificationAckCallback& ack_callback)>;
  44. #if BUILDFLAG(IS_WIN)
  45. ProcessSingleton(const std::string& program_name,
  46. const base::FilePath& user_data_dir,
  47. + const base::span<const uint8_t> additional_data,
  48. bool is_sandboxed,
  49. - const NotificationCallback& notification_callback);
  50. + const NotificationCallback& notification_callback,
  51. + const NotificationAckCallback& ack_notification_callback);
  52. #else
  53. ProcessSingleton(const base::FilePath& user_data_dir,
  54. - const NotificationCallback& notification_callback);
  55. + const base::span<const uint8_t> additional_data,
  56. + const NotificationCallback& notification_callback,
  57. + const NotificationAckCallback& ack_notification_callback);
  58. +#endif
  59. ProcessSingleton(const ProcessSingleton&) = delete;
  60. ProcessSingleton& operator=(const ProcessSingleton&) = delete;
  61. -#endif
  62. ~ProcessSingleton();
  63. // Notify another process, if available. Otherwise sets ourselves as the
  64. @@ -177,7 +187,13 @@ class ProcessSingleton {
  65. #endif
  66. private:
  67. - NotificationCallback notification_callback_; // Handler for notifications.
  68. + // A callback to run when the first instance receives data from the second.
  69. + NotificationCallback notification_callback_;
  70. + // A callback to run when the second instance
  71. + // receives an acknowledgement from the first.
  72. + NotificationAckCallback notification_ack_callback_;
  73. + // Custom data to pass to the other instance during notify.
  74. + base::span<const uint8_t> additional_data_;
  75. #if BUILDFLAG(IS_WIN)
  76. bool EscapeVirtualization(const base::FilePath& user_data_dir);
  77. @@ -190,6 +206,7 @@ class ProcessSingleton {
  78. HANDLE lock_file_;
  79. base::FilePath user_data_dir_;
  80. ShouldKillRemoteProcessCallback should_kill_remote_process_callback_;
  81. + HANDLE ack_pipe_;
  82. #elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
  83. // Return true if the given pid is one of our child processes.
  84. // Assumes that the current pid is the root of all pids of the current
  85. diff --git a/chrome/browser/process_singleton_posix.cc b/chrome/browser/process_singleton_posix.cc
  86. index be2c417c07a4206fac4a9a6c03e516fd0493c942..78f74b0b21242553b6af98628dc48190f7d1137d 100644
  87. --- a/chrome/browser/process_singleton_posix.cc
  88. +++ b/chrome/browser/process_singleton_posix.cc
  89. @@ -146,7 +146,7 @@ const char kACKToken[] = "ACK";
  90. const char kShutdownToken[] = "SHUTDOWN";
  91. const char kTokenDelimiter = '\0';
  92. const int kMaxMessageLength = 32 * 1024;
  93. -const int kMaxACKMessageLength = std::size(kShutdownToken) - 1;
  94. +const int kMaxACKMessageLength = kMaxMessageLength;
  95. bool g_disable_prompt = false;
  96. bool g_skip_is_chrome_process_check = false;
  97. @@ -612,6 +612,7 @@ class ProcessSingleton::LinuxWatcher
  98. // |reader| is for sending back ACK message.
  99. void HandleMessage(const std::string& current_dir,
  100. const std::vector<std::string>& argv,
  101. + const std::vector<uint8_t> additional_data,
  102. SocketReader* reader);
  103. private:
  104. @@ -636,6 +637,9 @@ class ProcessSingleton::LinuxWatcher
  105. // The ProcessSingleton that owns us.
  106. ProcessSingleton* const parent_;
  107. + bool ack_callback_called_ = false;
  108. + void AckCallback(SocketReader* reader, const base::span<const uint8_t>* response);
  109. +
  110. std::set<std::unique_ptr<SocketReader>, base::UniquePtrComparator> readers_;
  111. };
  112. @@ -666,16 +670,21 @@ void ProcessSingleton::LinuxWatcher::StartListening(int socket) {
  113. }
  114. void ProcessSingleton::LinuxWatcher::HandleMessage(
  115. - const std::string& current_dir, const std::vector<std::string>& argv,
  116. + const std::string& current_dir,
  117. + const std::vector<std::string>& argv,
  118. + const std::vector<uint8_t> additional_data,
  119. SocketReader* reader) {
  120. DCHECK(ui_task_runner_->BelongsToCurrentThread());
  121. DCHECK(reader);
  122. - if (parent_->notification_callback_.Run(base::CommandLine(argv),
  123. - base::FilePath(current_dir))) {
  124. - // Send back "ACK" message to prevent the client process from starting up.
  125. - reader->FinishWithACK(kACKToken, std::size(kACKToken) - 1);
  126. - } else {
  127. + auto wrapped_ack_callback =
  128. + base::BindRepeating(&ProcessSingleton::LinuxWatcher::AckCallback,
  129. + base::Unretained(this), reader);
  130. + ack_callback_called_ = false;
  131. + if (!parent_->notification_callback_.Run(base::CommandLine(argv),
  132. + base::FilePath(current_dir),
  133. + std::move(additional_data),
  134. + wrapped_ack_callback)) {
  135. LOG(WARNING) << "Not handling interprocess notification as browser"
  136. " is shutting down";
  137. // Send back "SHUTDOWN" message, so that the client process can start up
  138. @@ -685,6 +694,22 @@ void ProcessSingleton::LinuxWatcher::HandleMessage(
  139. }
  140. }
  141. +void ProcessSingleton::LinuxWatcher::AckCallback(
  142. + SocketReader* reader,
  143. + const base::span<const uint8_t>* response) {
  144. + // Send back "ACK" message to prevent the client process from starting up.
  145. + if (!ack_callback_called_) {
  146. + ack_callback_called_ = true;
  147. + std::string ack_message;
  148. + ack_message.append(kACKToken, std::size(kACKToken) - 1);
  149. + if (response && response->size_bytes()) {
  150. + ack_message.append(reinterpret_cast<const char*>(response->data()),
  151. + response->size_bytes());
  152. + }
  153. + reader->FinishWithACK(ack_message.c_str(), ack_message.size());
  154. + }
  155. +}
  156. +
  157. void ProcessSingleton::LinuxWatcher::RemoveSocketReader(SocketReader* reader) {
  158. DCHECK_CURRENTLY_ON(BrowserThread::IO);
  159. DCHECK(reader);
  160. @@ -720,7 +745,8 @@ void ProcessSingleton::LinuxWatcher::SocketReader::
  161. }
  162. }
  163. - // Validate the message. The shortest message is kStartToken\0x\0x
  164. + // Validate the message. The shortest message kStartToken\0\00
  165. + // The shortest message with additional data is kStartToken\0\00\00\0.
  166. const size_t kMinMessageLength = std::size(kStartToken) + 4;
  167. if (bytes_read_ < kMinMessageLength) {
  168. buf_[bytes_read_] = 0;
  169. @@ -750,10 +776,28 @@ void ProcessSingleton::LinuxWatcher::SocketReader::
  170. tokens.erase(tokens.begin());
  171. tokens.erase(tokens.begin());
  172. + size_t num_args;
  173. + base::StringToSizeT(tokens[0], &num_args);
  174. + std::vector<std::string> command_line(tokens.begin() + 1, tokens.begin() + 1 + num_args);
  175. +
  176. + std::vector<uint8_t> additional_data;
  177. + if (tokens.size() >= 3 + num_args) {
  178. + size_t additional_data_size;
  179. + base::StringToSizeT(tokens[1 + num_args], &additional_data_size);
  180. + std::string remaining_args = base::JoinString(
  181. + base::make_span(tokens.begin() + 2 + num_args, tokens.end()),
  182. + std::string(1, kTokenDelimiter));
  183. + const uint8_t* additional_data_bits =
  184. + reinterpret_cast<const uint8_t*>(remaining_args.c_str());
  185. + additional_data = std::vector<uint8_t>(additional_data_bits,
  186. + additional_data_bits + additional_data_size);
  187. + }
  188. +
  189. // Return to the UI thread to handle opening a new browser tab.
  190. ui_task_runner_->PostTask(
  191. FROM_HERE, base::BindOnce(&ProcessSingleton::LinuxWatcher::HandleMessage,
  192. - parent_, current_dir, tokens, this));
  193. + parent_, current_dir, command_line,
  194. + std::move(additional_data), this));
  195. fd_watch_controller_.reset();
  196. // LinuxWatcher::HandleMessage() is in charge of destroying this SocketReader
  197. @@ -782,8 +826,12 @@ void ProcessSingleton::LinuxWatcher::SocketReader::FinishWithACK(
  198. //
  199. ProcessSingleton::ProcessSingleton(
  200. const base::FilePath& user_data_dir,
  201. - const NotificationCallback& notification_callback)
  202. + const base::span<const uint8_t> additional_data,
  203. + const NotificationCallback& notification_callback,
  204. + const NotificationAckCallback& notification_ack_callback)
  205. : notification_callback_(notification_callback),
  206. + notification_ack_callback_(notification_ack_callback),
  207. + additional_data_(additional_data),
  208. current_pid_(base::GetCurrentProcId()),
  209. watcher_(new LinuxWatcher(this)) {
  210. socket_path_ = user_data_dir.Append(chrome::kSingletonSocketFilename);
  211. @@ -902,7 +950,8 @@ ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessWithTimeout(
  212. sizeof(socket_timeout));
  213. // Found another process, prepare our command line
  214. - // format is "START\0<current dir>\0<argv[0]>\0...\0<argv[n]>".
  215. + // format is "START\0<current-dir>\0<n-args>\0<argv[0]>\0...\0<argv[n]>
  216. + // \0<additional-data-length>\0<additional-data>".
  217. std::string to_send(kStartToken);
  218. to_send.push_back(kTokenDelimiter);
  219. @@ -912,11 +961,21 @@ ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessWithTimeout(
  220. to_send.append(current_dir.value());
  221. const std::vector<std::string>& argv = cmd_line.argv();
  222. + to_send.push_back(kTokenDelimiter);
  223. + to_send.append(base::NumberToString(argv.size()));
  224. for (auto it = argv.begin(); it != argv.end(); ++it) {
  225. to_send.push_back(kTokenDelimiter);
  226. to_send.append(*it);
  227. }
  228. + size_t data_to_send_size = additional_data_.size_bytes();
  229. + if (data_to_send_size) {
  230. + to_send.push_back(kTokenDelimiter);
  231. + to_send.append(base::NumberToString(data_to_send_size));
  232. + to_send.push_back(kTokenDelimiter);
  233. + to_send.append(reinterpret_cast<const char*>(additional_data_.data()), data_to_send_size);
  234. + }
  235. +
  236. // Send the message
  237. if (!WriteToSocket(socket.fd(), to_send.data(), to_send.length())) {
  238. // Try to kill the other process, because it might have been dead.
  239. @@ -958,6 +1017,17 @@ ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessWithTimeout(
  240. linux_ui->NotifyWindowManagerStartupComplete();
  241. #endif
  242. + size_t ack_data_len = len - (std::size(kACKToken) - 1);
  243. + if (ack_data_len) {
  244. + const uint8_t* raw_ack_data =
  245. + reinterpret_cast<const uint8_t*>(buf + std::size(kACKToken) - 1);
  246. + base::span<const uint8_t> ack_data =
  247. + base::make_span(raw_ack_data, raw_ack_data + ack_data_len);
  248. + notification_ack_callback_.Run(&ack_data);
  249. + } else {
  250. + notification_ack_callback_.Run(nullptr);
  251. + }
  252. +
  253. // Assume the other process is handling the request.
  254. return PROCESS_NOTIFIED;
  255. }
  256. diff --git a/chrome/browser/process_singleton_win.cc b/chrome/browser/process_singleton_win.cc
  257. index ec725b44296266bea1a51aea889463a0bba8449c..a3d4dd1efc950033855a1f2783f941384b249a5d 100644
  258. --- a/chrome/browser/process_singleton_win.cc
  259. +++ b/chrome/browser/process_singleton_win.cc
  260. @@ -21,6 +21,7 @@
  261. #include "base/strings/string_number_conversions.h"
  262. #include "base/strings/utf_string_conversions.h"
  263. #include "base/time/time.h"
  264. +#include "base/timer/timer.h"
  265. #include "base/trace_event/base_tracing.h"
  266. #include "base/win/registry.h"
  267. #include "base/win/scoped_handle.h"
  268. @@ -45,6 +46,14 @@
  269. namespace {
  270. const char kLockfile[] = "lockfile";
  271. +const LPCWSTR kPipeName = L"\\\\.\\pipe\\electronAckPipe";
  272. +const DWORD kPipeTimeout = 10000;
  273. +const DWORD kMaxMessageLength = 32 * 1024;
  274. +
  275. +std::unique_ptr<std::vector<uint8_t>> g_ack_data;
  276. +base::OneShotTimer g_ack_timer;
  277. +HANDLE g_write_ack_pipe;
  278. +bool g_write_ack_callback_called = false;
  279. // A helper class that acquires the given |mutex| while the AutoLockMutex is in
  280. // scope.
  281. @@ -80,10 +89,12 @@ BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) {
  282. bool ParseCommandLine(const COPYDATASTRUCT* cds,
  283. base::CommandLine* parsed_command_line,
  284. - base::FilePath* current_directory) {
  285. + base::FilePath* current_directory,
  286. + std::vector<uint8_t>* parsed_additional_data) {
  287. // We should have enough room for the shortest command (min_message_size)
  288. // and also be a multiple of wchar_t bytes. The shortest command
  289. - // possible is L"START\0\0" (empty current directory and command line).
  290. + // possible is L"START\0\0" (empty command line, current directory,
  291. + // and additional data).
  292. static const int min_message_size = 7;
  293. if (cds->cbData < min_message_size * sizeof(wchar_t) ||
  294. cds->cbData % sizeof(wchar_t) != 0) {
  295. @@ -133,11 +144,82 @@ bool ParseCommandLine(const COPYDATASTRUCT* cds,
  296. const std::wstring cmd_line =
  297. msg.substr(second_null + 1, third_null - second_null);
  298. *parsed_command_line = base::CommandLine::FromString(cmd_line);
  299. +
  300. + const std::wstring::size_type fourth_null =
  301. + msg.find_first_of(L'\0', third_null + 1);
  302. + if (fourth_null == std::wstring::npos ||
  303. + fourth_null == msg.length()) {
  304. + // No additional data was provided.
  305. + return true;
  306. + }
  307. +
  308. + // Get length of the additional data.
  309. + const std::wstring additional_data_length_string =
  310. + msg.substr(third_null + 1, fourth_null - third_null);
  311. + size_t additional_data_length;
  312. + base::StringToSizeT(additional_data_length_string, &additional_data_length);
  313. +
  314. + const std::wstring::size_type fifth_null =
  315. + msg.find_first_of(L'\0', fourth_null + 1);
  316. + if (fifth_null == std::wstring::npos ||
  317. + fifth_null == msg.length()) {
  318. + LOG(WARNING) << "Invalid format for start command, we need a string in 6 "
  319. + "parts separated by NULLs";
  320. + }
  321. +
  322. + // Get the actual additional data.
  323. + const std::wstring additional_data =
  324. + msg.substr(fourth_null + 1, fifth_null - fourth_null);
  325. + const uint8_t* additional_data_bytes =
  326. + reinterpret_cast<const uint8_t*>(additional_data.c_str());
  327. + *parsed_additional_data = std::vector<uint8_t>(additional_data_bytes,
  328. + additional_data_bytes + additional_data_length);
  329. +
  330. return true;
  331. }
  332. return false;
  333. }
  334. +void StoreAck(const base::span<const uint8_t>* ack_data) {
  335. + if (ack_data) {
  336. + g_ack_data = std::make_unique<std::vector<uint8_t>>(ack_data->begin(),
  337. + ack_data->end());
  338. + } else {
  339. + g_ack_data = nullptr;
  340. + }
  341. +}
  342. +
  343. +void SendBackAck() {
  344. + // This is the first instance sending the ack back to the second instance.
  345. + if (!g_write_ack_callback_called) {
  346. + g_write_ack_callback_called = true;
  347. + const uint8_t* data_buffer = nullptr;
  348. + DWORD data_to_send_size = 0;
  349. + if (g_ack_data) {
  350. + data_buffer = g_ack_data->data();
  351. + DWORD ack_data_size = g_ack_data->size() * sizeof(uint8_t);
  352. + data_to_send_size = (ack_data_size < kMaxMessageLength) ? ack_data_size : kMaxMessageLength;
  353. + }
  354. +
  355. + ::ConnectNamedPipe(g_write_ack_pipe, NULL);
  356. +
  357. + DWORD bytes_written = 0;
  358. + ::WriteFile(g_write_ack_pipe,
  359. + (LPCVOID)data_buffer,
  360. + data_to_send_size,
  361. + &bytes_written,
  362. + NULL);
  363. + DCHECK(bytes_written == data_to_send_size);
  364. +
  365. + ::FlushFileBuffers(g_write_ack_pipe);
  366. + ::DisconnectNamedPipe(g_write_ack_pipe);
  367. +
  368. + if (g_ack_data) {
  369. + g_ack_data.reset();
  370. + }
  371. + }
  372. +}
  373. +
  374. bool ProcessLaunchNotification(
  375. const ProcessSingleton::NotificationCallback& notification_callback,
  376. UINT message,
  377. @@ -151,16 +233,23 @@ bool ProcessLaunchNotification(
  378. // Handle the WM_COPYDATA message from another process.
  379. const COPYDATASTRUCT* cds = reinterpret_cast<COPYDATASTRUCT*>(lparam);
  380. -
  381. base::CommandLine parsed_command_line(base::CommandLine::NO_PROGRAM);
  382. base::FilePath current_directory;
  383. - if (!ParseCommandLine(cds, &parsed_command_line, &current_directory)) {
  384. + std::vector<uint8_t> additional_data;
  385. + if (!ParseCommandLine(cds, &parsed_command_line, &current_directory,
  386. + &additional_data)) {
  387. *result = TRUE;
  388. return true;
  389. }
  390. - *result = notification_callback.Run(parsed_command_line, current_directory) ?
  391. - TRUE : FALSE;
  392. + g_write_ack_callback_called = false;
  393. + *result = notification_callback.Run(parsed_command_line, current_directory,
  394. + std::move(additional_data),
  395. + base::BindRepeating(&StoreAck))
  396. + ? TRUE
  397. + : FALSE;
  398. + g_ack_timer.Start(FROM_HERE, base::Seconds(0),
  399. + base::BindOnce(&SendBackAck));
  400. return true;
  401. }
  402. @@ -261,9 +350,13 @@ bool ProcessSingleton::EscapeVirtualization(
  403. ProcessSingleton::ProcessSingleton(
  404. const std::string& program_name,
  405. const base::FilePath& user_data_dir,
  406. + const base::span<const uint8_t> additional_data,
  407. bool is_app_sandboxed,
  408. - const NotificationCallback& notification_callback)
  409. + const NotificationCallback& notification_callback,
  410. + const NotificationAckCallback& notification_ack_callback)
  411. : notification_callback_(notification_callback),
  412. + notification_ack_callback_(notification_ack_callback),
  413. + additional_data_(additional_data),
  414. program_name_(program_name),
  415. is_app_sandboxed_(is_app_sandboxed),
  416. is_virtualized_(false),
  417. @@ -278,6 +371,37 @@ ProcessSingleton::~ProcessSingleton() {
  418. ::CloseHandle(lock_file_);
  419. }
  420. +void ReadAck(const ProcessSingleton::NotificationAckCallback& ack_callback) {
  421. + // We are reading the ack from the first instance.
  422. + // First, wait for the pipe.
  423. + ::WaitNamedPipe(kPipeName, NMPWAIT_USE_DEFAULT_WAIT);
  424. +
  425. + HANDLE read_ack_pipe = ::CreateFile(kPipeName,
  426. + GENERIC_READ,
  427. + FILE_SHARE_READ,
  428. + NULL,
  429. + OPEN_EXISTING,
  430. + FILE_ATTRIBUTE_NORMAL,
  431. + NULL);
  432. + CHECK(read_ack_pipe != INVALID_HANDLE_VALUE);
  433. +
  434. + DWORD bytes_read;
  435. + uint8_t read_ack_buffer[kMaxMessageLength];
  436. + ::ReadFile(read_ack_pipe,
  437. + (LPVOID)read_ack_buffer,
  438. + kMaxMessageLength,
  439. + &bytes_read,
  440. + NULL);
  441. +
  442. + if (!bytes_read) {
  443. + ack_callback.Run(nullptr);
  444. + } else {
  445. + base::span<const uint8_t> out_span(read_ack_buffer, read_ack_buffer + bytes_read);
  446. + ack_callback.Run(&out_span);
  447. + }
  448. + ::CloseHandle(read_ack_pipe);
  449. +}
  450. +
  451. // Code roughly based on Mozilla.
  452. ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() {
  453. TRACE_EVENT0("startup", "ProcessSingleton::NotifyOtherProcess");
  454. @@ -290,8 +414,9 @@ ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() {
  455. return PROCESS_NONE;
  456. }
  457. - switch (chrome::AttemptToNotifyRunningChrome(remote_window_)) {
  458. + switch (chrome::AttemptToNotifyRunningChrome(remote_window_, additional_data_)) {
  459. case chrome::NOTIFY_SUCCESS:
  460. + ReadAck(notification_ack_callback_);
  461. return PROCESS_NOTIFIED;
  462. case chrome::NOTIFY_FAILED:
  463. remote_window_ = NULL;
  464. @@ -429,6 +554,18 @@ bool ProcessSingleton::Create() {
  465. << "Lock file can not be created! Error code: " << error;
  466. if (lock_file_ != INVALID_HANDLE_VALUE) {
  467. + // We are the first instance. Create a pipe to send out ack data.
  468. + ack_pipe_ = ::CreateNamedPipe(kPipeName,
  469. + PIPE_ACCESS_OUTBOUND,
  470. + PIPE_TYPE_BYTE | PIPE_REJECT_REMOTE_CLIENTS,
  471. + PIPE_UNLIMITED_INSTANCES,
  472. + kMaxMessageLength,
  473. + 0,
  474. + kPipeTimeout,
  475. + NULL);
  476. + CHECK(ack_pipe_ != INVALID_HANDLE_VALUE);
  477. + g_write_ack_pipe = ack_pipe_;
  478. +
  479. // Set the window's title to the path of our user data directory so
  480. // other Chrome instances can decide if they should forward to us.
  481. TRACE_EVENT0("startup", "ProcessSingleton::Create:CreateWindow");
  482. @@ -456,6 +593,7 @@ bool ProcessSingleton::Create() {
  483. }
  484. void ProcessSingleton::Cleanup() {
  485. + ::CloseHandle(ack_pipe_);
  486. }
  487. void ProcessSingleton::OverrideShouldKillRemoteProcessCallbackForTesting(
  488. diff --git a/chrome/browser/win/chrome_process_finder.cc b/chrome/browser/win/chrome_process_finder.cc
  489. index b64ed1d155a30582e48c9cdffcee9d0f25a53a6a..ce851d09d501ebcc6d6c4065e746e869d5275b2b 100644
  490. --- a/chrome/browser/win/chrome_process_finder.cc
  491. +++ b/chrome/browser/win/chrome_process_finder.cc
  492. @@ -36,9 +36,10 @@ HWND FindRunningChromeWindow(const base::FilePath& user_data_dir) {
  493. return base::win::MessageWindow::FindWindow(user_data_dir.value());
  494. }
  495. -NotifyChromeResult AttemptToNotifyRunningChrome(HWND remote_window) {
  496. +NotifyChromeResult AttemptToNotifyRunningChrome(
  497. + HWND remote_window,
  498. + const base::span<const uint8_t> additional_data) {
  499. TRACE_EVENT0("startup", "AttemptToNotifyRunningChrome");
  500. -
  501. DCHECK(remote_window);
  502. DWORD process_id = 0;
  503. DWORD thread_id = GetWindowThreadProcessId(remote_window, &process_id);
  504. @@ -50,7 +51,8 @@ NotifyChromeResult AttemptToNotifyRunningChrome(HWND remote_window) {
  505. }
  506. // Send the command line to the remote chrome window.
  507. - // Format is "START\0<<<current directory>>>\0<<<commandline>>>".
  508. + // Format is
  509. + // "START\0<current-directory>\0<command-line>\0<additional-data-length>\0<additional-data>".
  510. std::wstring to_send(L"START\0", 6); // want the NULL in the string.
  511. base::FilePath cur_dir;
  512. if (!base::GetCurrentDirectory(&cur_dir)) {
  513. @@ -64,6 +66,22 @@ NotifyChromeResult AttemptToNotifyRunningChrome(HWND remote_window) {
  514. base::CommandLine::ForCurrentProcess()->GetCommandLineString());
  515. to_send.append(L"\0", 1); // Null separator.
  516. + size_t additional_data_size = additional_data.size_bytes();
  517. + if (additional_data_size) {
  518. + // Send over the size, because the reinterpret cast to wchar_t could
  519. + // add padding.
  520. + to_send.append(base::UTF8ToWide(base::NumberToString(additional_data_size)));
  521. + to_send.append(L"\0", 1); // Null separator.
  522. +
  523. + size_t padded_size = additional_data_size / sizeof(wchar_t);
  524. + if (additional_data_size % sizeof(wchar_t) != 0) {
  525. + padded_size++;
  526. + }
  527. + to_send.append(reinterpret_cast<const wchar_t*>(additional_data.data()),
  528. + padded_size);
  529. + to_send.append(L"\0", 1); // Null separator.
  530. + }
  531. +
  532. // Allow the current running browser window to make itself the foreground
  533. // window (otherwise it will just flash in the taskbar).
  534. ::AllowSetForegroundWindow(process_id);
  535. diff --git a/chrome/browser/win/chrome_process_finder.h b/chrome/browser/win/chrome_process_finder.h
  536. index 5516673cee019f6060077091e59498bf9038cd6e..8edea5079b46c2cba67833114eb9c21d85cfc22d 100644
  537. --- a/chrome/browser/win/chrome_process_finder.h
  538. +++ b/chrome/browser/win/chrome_process_finder.h
  539. @@ -7,6 +7,7 @@
  540. #include <windows.h>
  541. +#include "base/containers/span.h"
  542. #include "base/time/time.h"
  543. namespace base {
  544. @@ -27,7 +28,9 @@ HWND FindRunningChromeWindow(const base::FilePath& user_data_dir);
  545. // Attempts to send the current command line to an already running instance of
  546. // Chrome via a WM_COPYDATA message.
  547. // Returns true if a running Chrome is found and successfully notified.
  548. -NotifyChromeResult AttemptToNotifyRunningChrome(HWND remote_window);
  549. +NotifyChromeResult AttemptToNotifyRunningChrome(
  550. + HWND remote_window,
  551. + const base::span<const uint8_t> additional_data);
  552. // Changes the notification timeout to |new_timeout|, returns the old timeout.
  553. base::TimeDelta SetNotificationTimeoutForTesting(base::TimeDelta new_timeout);