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

Side by Side Diff: chrome/browser/process_singleton_linux.cc

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase again Created 7 years, 6 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 // 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 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
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()),
482 parent_(parent) { 482 parent_(parent) {
483 } 483 }
484 484
485 // Start listening for connections on the socket. This method should be 485 // Start listening for connections on the socket. This method should be
486 // called from the IO thread. 486 // called from the IO thread.
487 void StartListening(int socket); 487 void StartListening(int socket);
488 488
489 // This method determines if we should use the same process and if we should, 489 // 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. 490 // opens a new browser tab. This runs on the UI thread.
491 // |reader| is for sending back ACK message. 491 // |reader| is for sending back ACK message.
(...skipping 14 matching lines...) Expand all
506 } 506 }
507 507
508 private: 508 private:
509 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; 509 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
510 friend class base::DeleteHelper<ProcessSingleton::LinuxWatcher>; 510 friend class base::DeleteHelper<ProcessSingleton::LinuxWatcher>;
511 511
512 virtual ~LinuxWatcher() { 512 virtual ~LinuxWatcher() {
513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
514 STLDeleteElements(&readers_); 514 STLDeleteElements(&readers_);
515 515
516 MessageLoopForIO* ml = MessageLoopForIO::current(); 516 base::MessageLoopForIO* ml = base::MessageLoopForIO::current();
517 ml->RemoveDestructionObserver(this); 517 ml->RemoveDestructionObserver(this);
518 } 518 }
519 519
520 // Removes and deletes the SocketReader. 520 // Removes and deletes the SocketReader.
521 void RemoveSocketReader(SocketReader* reader); 521 void RemoveSocketReader(SocketReader* reader);
522 522
523 MessageLoopForIO::FileDescriptorWatcher fd_watcher_; 523 base::MessageLoopForIO::FileDescriptorWatcher fd_watcher_;
524 524
525 // A reference to the UI message loop (i.e., the message loop we were 525 // A reference to the UI message loop (i.e., the message loop we were
526 // constructed on). 526 // constructed on).
527 MessageLoop* ui_message_loop_; 527 base::MessageLoop* ui_message_loop_;
528 528
529 // The ProcessSingleton that owns us. 529 // The ProcessSingleton that owns us.
530 ProcessSingleton* const parent_; 530 ProcessSingleton* const parent_;
531 531
532 std::set<SocketReader*> readers_; 532 std::set<SocketReader*> readers_;
533 533
534 DISALLOW_COPY_AND_ASSIGN(LinuxWatcher); 534 DISALLOW_COPY_AND_ASSIGN(LinuxWatcher);
535 }; 535 };
536 536
537 void ProcessSingleton::LinuxWatcher::OnFileCanReadWithoutBlocking(int fd) { 537 void ProcessSingleton::LinuxWatcher::OnFileCanReadWithoutBlocking(int fd) {
(...skipping 11 matching lines...) Expand all
549 DCHECK_EQ(0, rv) << "Failed to make non-blocking socket."; 549 DCHECK_EQ(0, rv) << "Failed to make non-blocking socket.";
550 SocketReader* reader = new SocketReader(this, 550 SocketReader* reader = new SocketReader(this,
551 ui_message_loop_, 551 ui_message_loop_,
552 connection_socket); 552 connection_socket);
553 readers_.insert(reader); 553 readers_.insert(reader);
554 } 554 }
555 555
556 void ProcessSingleton::LinuxWatcher::StartListening(int socket) { 556 void ProcessSingleton::LinuxWatcher::StartListening(int socket) {
557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
558 // Watch for client connections on this socket. 558 // Watch for client connections on this socket.
559 MessageLoopForIO* ml = MessageLoopForIO::current(); 559 base::MessageLoopForIO* ml = base::MessageLoopForIO::current();
560 ml->AddDestructionObserver(this); 560 ml->AddDestructionObserver(this);
561 ml->WatchFileDescriptor(socket, true, MessageLoopForIO::WATCH_READ, 561 ml->WatchFileDescriptor(socket, true, base::MessageLoopForIO::WATCH_READ,
562 &fd_watcher_, this); 562 &fd_watcher_, this);
563 } 563 }
564 564
565 void ProcessSingleton::LinuxWatcher::HandleMessage( 565 void ProcessSingleton::LinuxWatcher::HandleMessage(
566 const std::string& current_dir, const std::vector<std::string>& argv, 566 const std::string& current_dir, const std::vector<std::string>& argv,
567 SocketReader* reader) { 567 SocketReader* reader) {
568 DCHECK(ui_message_loop_ == MessageLoop::current()); 568 DCHECK(ui_message_loop_ == base::MessageLoop::current());
569 DCHECK(reader); 569 DCHECK(reader);
570 570
571 if (parent_->notification_callback_.Run(CommandLine(argv), 571 if (parent_->notification_callback_.Run(CommandLine(argv),
572 base::FilePath(current_dir))) { 572 base::FilePath(current_dir))) {
573 // Send back "ACK" message to prevent the client process from starting up. 573 // Send back "ACK" message to prevent the client process from starting up.
574 reader->FinishWithACK(kACKToken, arraysize(kACKToken) - 1); 574 reader->FinishWithACK(kACKToken, arraysize(kACKToken) - 1);
575 } else { 575 } else {
576 LOG(WARNING) << "Not handling interprocess notification as browser" 576 LOG(WARNING) << "Not handling interprocess notification as browser"
577 " is shutting down"; 577 " is shutting down";
578 // Send back "SHUTDOWN" message, so that the client process can start up 578 // Send back "SHUTDOWN" message, so that the client process can start up
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
977 } 977 }
978 978
979 void ProcessSingleton::KillProcess(int pid) { 979 void ProcessSingleton::KillProcess(int pid) {
980 // TODO(james.su@gmail.com): Is SIGKILL ok? 980 // TODO(james.su@gmail.com): Is SIGKILL ok?
981 int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); 981 int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL);
982 // ESRCH = No Such Process (can happen if the other process is already in 982 // 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). 983 // progress of shutting down and finishes before we try to kill it).
984 DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " 984 DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: "
985 << safe_strerror(errno); 985 << safe_strerror(errno);
986 } 986 }
OLDNEW
« no previous file with comments | « chrome/browser/process_singleton_browsertest.cc ('k') | chrome/browser/profiles/profile_destroyer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698