| 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 // On Linux, when the user tries to launch a second copy of chrome, we check | 5 // On Linux, when the user tries to launch a second copy of chrome, we check |
| 6 // for a socket in the user's profile directory. If the socket file is open we | 6 // for a socket in the user's profile directory. If the socket file is open we |
| 7 // send a message to the first chrome browser process with the current | 7 // send a message to the first chrome browser process with the current |
| 8 // directory and second process command line flags. The second process then | 8 // directory and second process command line flags. The second process then |
| 9 // exits. | 9 // exits. |
| 10 // | 10 // |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 } | 398 } |
| 399 } | 399 } |
| 400 | 400 |
| 401 } // namespace | 401 } // namespace |
| 402 | 402 |
| 403 /////////////////////////////////////////////////////////////////////////////// | 403 /////////////////////////////////////////////////////////////////////////////// |
| 404 // ProcessSingleton::LinuxWatcher | 404 // ProcessSingleton::LinuxWatcher |
| 405 // A helper class for a Linux specific implementation of the process singleton. | 405 // A helper class for a Linux specific implementation of the process singleton. |
| 406 // This class sets up a listener on the singleton socket and handles parsing | 406 // This class sets up a listener on the singleton socket and handles parsing |
| 407 // messages that come in on the singleton socket. | 407 // messages that come in on the singleton socket. |
| 408 class ProcessSingleton::LinuxWatcher | 408 class ProcessSingleton::LinuxWatcher : |
| 409 : public MessageLoopForIO::Watcher, | 409 public base::MessageLoopForIO::Watcher, |
| 410 public MessageLoop::DestructionObserver, | 410 public base::MessageLoop::DestructionObserver, |
| 411 public base::RefCountedThreadSafe<ProcessSingleton::LinuxWatcher, | 411 public base::RefCountedThreadSafe<ProcessSingleton::LinuxWatcher, |
| 412 BrowserThread::DeleteOnIOThread> { | 412 BrowserThread::DeleteOnIOThread> { |
| 413 public: | 413 public: |
| 414 // A helper class to read message from an established socket. | 414 // A helper class to read message from an established socket. |
| 415 class SocketReader : public MessageLoopForIO::Watcher { | 415 class SocketReader : public base::MessageLoopForIO::Watcher { |
| 416 public: | 416 public: |
| 417 SocketReader(ProcessSingleton::LinuxWatcher* parent, | 417 SocketReader(ProcessSingleton::LinuxWatcher* parent, |
| 418 MessageLoop* ui_message_loop, | 418 base::MessageLoop* ui_message_loop, |
| 419 int fd) | 419 int fd) |
| 420 : parent_(parent), | 420 : parent_(parent), |
| 421 ui_message_loop_(ui_message_loop), | 421 ui_message_loop_(ui_message_loop), |
| 422 fd_(fd), | 422 fd_(fd), |
| 423 bytes_read_(0) { | 423 bytes_read_(0) { |
| 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 425 // Wait for reads. | 425 // Wait for reads. |
| 426 MessageLoopForIO::current()->WatchFileDescriptor( | 426 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 427 fd, true, MessageLoopForIO::WATCH_READ, &fd_reader_, this); | 427 fd, true, base::MessageLoopForIO::WATCH_READ, &fd_reader_, this); |
| 428 // If we haven't completed in a reasonable amount of time, give up. | 428 // If we haven't completed in a reasonable amount of time, give up. |
| 429 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kTimeoutInSeconds), | 429 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(kTimeoutInSeconds), |
| 430 this, &SocketReader::CleanupAndDeleteSelf); | 430 this, &SocketReader::CleanupAndDeleteSelf); |
| 431 } | 431 } |
| 432 | 432 |
| 433 virtual ~SocketReader() { | 433 virtual ~SocketReader() { |
| 434 CloseSocket(fd_); | 434 CloseSocket(fd_); |
| 435 } | 435 } |
| 436 | 436 |
| 437 // MessageLoopForIO::Watcher impl. | 437 // MessageLoopForIO::Watcher impl. |
| 438 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 438 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 439 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE { | 439 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE { |
| 440 // SocketReader only watches for accept (read) events. | 440 // SocketReader only watches for accept (read) events. |
| 441 NOTREACHED(); | 441 NOTREACHED(); |
| 442 } | 442 } |
| 443 | 443 |
| 444 // Finish handling the incoming message by optionally sending back an ACK | 444 // Finish handling the incoming message by optionally sending back an ACK |
| 445 // message and removing this SocketReader. | 445 // message and removing this SocketReader. |
| 446 void FinishWithACK(const char *message, size_t length); | 446 void FinishWithACK(const char *message, size_t length); |
| 447 | 447 |
| 448 private: | 448 private: |
| 449 void CleanupAndDeleteSelf() { | 449 void CleanupAndDeleteSelf() { |
| 450 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 450 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 451 | 451 |
| 452 parent_->RemoveSocketReader(this); | 452 parent_->RemoveSocketReader(this); |
| 453 // We're deleted beyond this point. | 453 // We're deleted beyond this point. |
| 454 } | 454 } |
| 455 | 455 |
| 456 MessageLoopForIO::FileDescriptorWatcher fd_reader_; | 456 base::MessageLoopForIO::FileDescriptorWatcher fd_reader_; |
| 457 | 457 |
| 458 // The ProcessSingleton::LinuxWatcher that owns us. | 458 // The ProcessSingleton::LinuxWatcher that owns us. |
| 459 ProcessSingleton::LinuxWatcher* const parent_; | 459 ProcessSingleton::LinuxWatcher* const parent_; |
| 460 | 460 |
| 461 // A reference to the UI message loop. | 461 // A reference to the UI message loop. |
| 462 MessageLoop* const ui_message_loop_; | 462 base::MessageLoop* const ui_message_loop_; |
| 463 | 463 |
| 464 // The file descriptor we're reading. | 464 // The file descriptor we're reading. |
| 465 const int fd_; | 465 const int fd_; |
| 466 | 466 |
| 467 // Store the message in this buffer. | 467 // Store the message in this buffer. |
| 468 char buf_[kMaxMessageLength]; | 468 char buf_[kMaxMessageLength]; |
| 469 | 469 |
| 470 // Tracks the number of bytes we've read in case we're getting partial | 470 // Tracks the number of bytes we've read in case we're getting partial |
| 471 // reads. | 471 // reads. |
| 472 size_t bytes_read_; | 472 size_t bytes_read_; |
| 473 | 473 |
| 474 base::OneShotTimer<SocketReader> timer_; | 474 base::OneShotTimer<SocketReader> timer_; |
| 475 | 475 |
| 476 DISALLOW_COPY_AND_ASSIGN(SocketReader); | 476 DISALLOW_COPY_AND_ASSIGN(SocketReader); |
| 477 }; | 477 }; |
| 478 | 478 |
| 479 // We expect to only be constructed on the UI thread. | 479 // We expect to only be constructed on the UI thread. |
| 480 explicit LinuxWatcher(ProcessSingleton* parent) | 480 explicit LinuxWatcher(ProcessSingleton* parent) |
| 481 : ui_message_loop_(MessageLoop::current()), | 481 : ui_message_loop_(base::MessageLoop::current()), parent_(parent) {} |
| 482 parent_(parent) { | |
| 483 } | |
| 484 | 482 |
| 485 // Start listening for connections on the socket. This method should be | 483 // Start listening for connections on the socket. This method should be |
| 486 // called from the IO thread. | 484 // called from the IO thread. |
| 487 void StartListening(int socket); | 485 void StartListening(int socket); |
| 488 | 486 |
| 489 // This method determines if we should use the same process and if we should, | 487 // This method determines if we should use the same process and if we should, |
| 490 // opens a new browser tab. This runs on the UI thread. | 488 // opens a new browser tab. This runs on the UI thread. |
| 491 // |reader| is for sending back ACK message. | 489 // |reader| is for sending back ACK message. |
| 492 void HandleMessage(const std::string& current_dir, | 490 void HandleMessage(const std::string& current_dir, |
| 493 const std::vector<std::string>& argv, | 491 const std::vector<std::string>& argv, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 506 } | 504 } |
| 507 | 505 |
| 508 private: | 506 private: |
| 509 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; | 507 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; |
| 510 friend class base::DeleteHelper<ProcessSingleton::LinuxWatcher>; | 508 friend class base::DeleteHelper<ProcessSingleton::LinuxWatcher>; |
| 511 | 509 |
| 512 virtual ~LinuxWatcher() { | 510 virtual ~LinuxWatcher() { |
| 513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 511 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 514 STLDeleteElements(&readers_); | 512 STLDeleteElements(&readers_); |
| 515 | 513 |
| 516 MessageLoopForIO* ml = MessageLoopForIO::current(); | 514 base::MessageLoopForIO* ml = base::MessageLoopForIO::current(); |
| 517 ml->RemoveDestructionObserver(this); | 515 ml->RemoveDestructionObserver(this); |
| 518 } | 516 } |
| 519 | 517 |
| 520 // Removes and deletes the SocketReader. | 518 // Removes and deletes the SocketReader. |
| 521 void RemoveSocketReader(SocketReader* reader); | 519 void RemoveSocketReader(SocketReader* reader); |
| 522 | 520 |
| 523 MessageLoopForIO::FileDescriptorWatcher fd_watcher_; | 521 base::MessageLoopForIO::FileDescriptorWatcher fd_watcher_; |
| 524 | 522 |
| 525 // A reference to the UI message loop (i.e., the message loop we were | 523 // A reference to the UI message loop (i.e., the message loop we were |
| 526 // constructed on). | 524 // constructed on). |
| 527 MessageLoop* ui_message_loop_; | 525 base::MessageLoop* ui_message_loop_; |
| 528 | 526 |
| 529 // The ProcessSingleton that owns us. | 527 // The ProcessSingleton that owns us. |
| 530 ProcessSingleton* const parent_; | 528 ProcessSingleton* const parent_; |
| 531 | 529 |
| 532 std::set<SocketReader*> readers_; | 530 std::set<SocketReader*> readers_; |
| 533 | 531 |
| 534 DISALLOW_COPY_AND_ASSIGN(LinuxWatcher); | 532 DISALLOW_COPY_AND_ASSIGN(LinuxWatcher); |
| 535 }; | 533 }; |
| 536 | 534 |
| 537 void ProcessSingleton::LinuxWatcher::OnFileCanReadWithoutBlocking(int fd) { | 535 void ProcessSingleton::LinuxWatcher::OnFileCanReadWithoutBlocking(int fd) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 549 DCHECK_EQ(0, rv) << "Failed to make non-blocking socket."; | 547 DCHECK_EQ(0, rv) << "Failed to make non-blocking socket."; |
| 550 SocketReader* reader = new SocketReader(this, | 548 SocketReader* reader = new SocketReader(this, |
| 551 ui_message_loop_, | 549 ui_message_loop_, |
| 552 connection_socket); | 550 connection_socket); |
| 553 readers_.insert(reader); | 551 readers_.insert(reader); |
| 554 } | 552 } |
| 555 | 553 |
| 556 void ProcessSingleton::LinuxWatcher::StartListening(int socket) { | 554 void ProcessSingleton::LinuxWatcher::StartListening(int socket) { |
| 557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 558 // Watch for client connections on this socket. | 556 // Watch for client connections on this socket. |
| 559 MessageLoopForIO* ml = MessageLoopForIO::current(); | 557 base::MessageLoopForIO* ml = base::MessageLoopForIO::current(); |
| 560 ml->AddDestructionObserver(this); | 558 ml->AddDestructionObserver(this); |
| 561 ml->WatchFileDescriptor(socket, true, MessageLoopForIO::WATCH_READ, | 559 ml->WatchFileDescriptor( |
| 562 &fd_watcher_, this); | 560 socket, true, base::MessageLoopForIO::WATCH_READ, &fd_watcher_, this); |
| 563 } | 561 } |
| 564 | 562 |
| 565 void ProcessSingleton::LinuxWatcher::HandleMessage( | 563 void ProcessSingleton::LinuxWatcher::HandleMessage( |
| 566 const std::string& current_dir, const std::vector<std::string>& argv, | 564 const std::string& current_dir, const std::vector<std::string>& argv, |
| 567 SocketReader* reader) { | 565 SocketReader* reader) { |
| 568 DCHECK(ui_message_loop_ == MessageLoop::current()); | 566 DCHECK(ui_message_loop_ == base::MessageLoop::current()); |
| 569 DCHECK(reader); | 567 DCHECK(reader); |
| 570 | 568 |
| 571 if (parent_->notification_callback_.Run(CommandLine(argv), | 569 if (parent_->notification_callback_.Run(CommandLine(argv), |
| 572 base::FilePath(current_dir))) { | 570 base::FilePath(current_dir))) { |
| 573 // Send back "ACK" message to prevent the client process from starting up. | 571 // Send back "ACK" message to prevent the client process from starting up. |
| 574 reader->FinishWithACK(kACKToken, arraysize(kACKToken) - 1); | 572 reader->FinishWithACK(kACKToken, arraysize(kACKToken) - 1); |
| 575 } else { | 573 } else { |
| 576 LOG(WARNING) << "Not handling interprocess notification as browser" | 574 LOG(WARNING) << "Not handling interprocess notification as browser" |
| 577 " is shutting down"; | 575 " is shutting down"; |
| 578 // Send back "SHUTDOWN" message, so that the client process can start up | 576 // Send back "SHUTDOWN" message, so that the client process can start up |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 977 } | 975 } |
| 978 | 976 |
| 979 void ProcessSingleton::KillProcess(int pid) { | 977 void ProcessSingleton::KillProcess(int pid) { |
| 980 // TODO(james.su@gmail.com): Is SIGKILL ok? | 978 // TODO(james.su@gmail.com): Is SIGKILL ok? |
| 981 int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); | 979 int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); |
| 982 // ESRCH = No Such Process (can happen if the other process is already in | 980 // ESRCH = No Such Process (can happen if the other process is already in |
| 983 // progress of shutting down and finishes before we try to kill it). | 981 // progress of shutting down and finishes before we try to kill it). |
| 984 DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " | 982 DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " |
| 985 << safe_strerror(errno); | 983 << safe_strerror(errno); |
| 986 } | 984 } |
| OLD | NEW |