OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/thread.h" | 10 #include "base/thread.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 READ_DATA, | 97 READ_DATA, |
98 // End processing requests. | 98 // End processing requests. |
99 QUIT | 99 QUIT |
100 }; | 100 }; |
101 | 101 |
102 // The list of return codes. | 102 // The list of return codes. |
103 enum { | 103 enum { |
104 RESULT_OK = 0, | 104 RESULT_OK = 0, |
105 RESULT_UNKNOWN_COMMAND, | 105 RESULT_UNKNOWN_COMMAND, |
106 RESULT_INVALID_PARAMETER, | 106 RESULT_INVALID_PARAMETER, |
107 RESULT_NAME_OVERFLOW, | 107 RESULT_NAME_OVERFLOW |
108 RESULT_PENDING // This error code is NOT expected by the master process. | |
109 }; | 108 }; |
110 | 109 |
111 // ----------------------------------------------------------------------- | 110 // ----------------------------------------------------------------------- |
112 | 111 |
113 class BaseSM : public MessageLoopForIO::IOHandler { | 112 class BaseSM : public MessageLoopForIO::IOHandler { |
114 public: | 113 public: |
115 explicit BaseSM(HANDLE channel); | 114 explicit BaseSM(HANDLE channel); |
116 virtual ~BaseSM(); | 115 virtual ~BaseSM(); |
117 | 116 |
118 protected: | 117 protected: |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 private: | 568 private: |
570 enum { | 569 enum { |
571 SLAVE_INITIAL = 0, | 570 SLAVE_INITIAL = 0, |
572 SLAVE_WAITING, | 571 SLAVE_WAITING, |
573 SLAVE_END | 572 SLAVE_END |
574 }; | 573 }; |
575 | 574 |
576 void DoGetNextEntry(); | 575 void DoGetNextEntry(); |
577 void DoGetPrevEntry(); | 576 void DoGetPrevEntry(); |
578 int32 GetEntryFromList(); | 577 int32 GetEntryFromList(); |
579 void DoGetEntryComplete(int result); | |
580 void DoCloseEntry(); | 578 void DoCloseEntry(); |
581 void DoGetKey(); | 579 void DoGetKey(); |
582 void DoGetUseTimes(); | 580 void DoGetUseTimes(); |
583 void DoGetDataSize(); | 581 void DoGetDataSize(); |
584 void DoReadData(); | 582 void DoReadData(); |
585 void DoReadDataComplete(int ret); | 583 void DoReadDataComplete(int ret); |
586 void DoEnd(); | 584 void DoEnd(); |
587 void Fail(); | 585 void Fail(); |
588 | 586 |
589 void* iterator_; | 587 void* iterator_; |
590 Message msg_; // Used for DoReadDataComplete and DoGetEntryComplete. | 588 Message msg_; // Only used for DoReadDataComplete. |
591 | 589 |
592 net::CompletionCallbackImpl<SlaveSM> read_callback_; | 590 net::CompletionCallbackImpl<SlaveSM> read_callback_; |
593 net::CompletionCallbackImpl<SlaveSM> next_callback_; | |
594 scoped_ptr<disk_cache::BackendImpl> cache_; | 591 scoped_ptr<disk_cache::BackendImpl> cache_; |
595 }; | 592 }; |
596 | 593 |
597 SlaveSM::SlaveSM(const std::wstring& path, HANDLE channel) | 594 SlaveSM::SlaveSM(const std::wstring& path, HANDLE channel) |
598 : BaseSM(channel), iterator_(NULL), | 595 : BaseSM(channel), iterator_(NULL), |
599 ALLOW_THIS_IN_INITIALIZER_LIST( | 596 ALLOW_THIS_IN_INITIALIZER_LIST( |
600 read_callback_(this, &SlaveSM::DoReadDataComplete)), | 597 read_callback_(this, &SlaveSM::DoReadDataComplete)) { |
601 ALLOW_THIS_IN_INITIALIZER_LIST( | |
602 next_callback_(this, &SlaveSM::DoGetEntryComplete)) { | |
603 disk_cache::Backend* cache; | 598 disk_cache::Backend* cache; |
604 TestCompletionCallback cb; | 599 TestCompletionCallback cb; |
605 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, | 600 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, |
606 FilePath::FromWStringHack(path), 0, | 601 FilePath::FromWStringHack(path), 0, |
607 false, | 602 false, |
608 cache_thread_.message_loop_proxy(), | 603 cache_thread_.message_loop_proxy(), |
609 &cache, &cb); | 604 &cache, &cb); |
610 if (cb.GetResult(rv) != net::OK) { | 605 if (cb.GetResult(rv) != net::OK) { |
611 printf("Unable to open cache files\n"); | 606 printf("Unable to open cache files\n"); |
612 return; | 607 return; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 default: | 665 default: |
671 NOTREACHED(); | 666 NOTREACHED(); |
672 break; | 667 break; |
673 } | 668 } |
674 } | 669 } |
675 | 670 |
676 bool SlaveSM::DoInit() { | 671 bool SlaveSM::DoInit() { |
677 DEBUGMSG("\t\t\tSlave DoInit\n"); | 672 DEBUGMSG("\t\t\tSlave DoInit\n"); |
678 DCHECK(state_ == SLAVE_INITIAL); | 673 DCHECK(state_ == SLAVE_INITIAL); |
679 state_ = SLAVE_WAITING; | 674 state_ = SLAVE_WAITING; |
680 if (!cache_.get()) | |
681 return false; | |
682 | |
683 return ReceiveMsg(); | 675 return ReceiveMsg(); |
684 } | 676 } |
685 | 677 |
686 void SlaveSM::DoGetNextEntry() { | 678 void SlaveSM::DoGetNextEntry() { |
687 DEBUGMSG("\t\t\tSlave DoGetNextEntry\n"); | 679 DEBUGMSG("\t\t\tSlave DoGetNextEntry\n"); |
688 Message msg; | 680 Message msg; |
689 msg.command = GET_NEXT_ENTRY; | 681 msg.command = GET_NEXT_ENTRY; |
690 | 682 |
691 if (input_->msg.arg1) { | 683 if (input_->msg.arg1) { |
692 // We only support one list. | 684 // We only support one list. |
693 msg.result = RESULT_UNKNOWN_COMMAND; | 685 msg.result = RESULT_UNKNOWN_COMMAND; |
694 } else { | 686 } else { |
695 msg.result = GetEntryFromList(); | 687 msg.result = GetEntryFromList(); |
696 msg.long_arg1 = reinterpret_cast<int64>(entry_); | 688 msg.long_arg1 = reinterpret_cast<int64>(entry_); |
697 } | 689 } |
698 SendMsg(msg); | 690 SendMsg(msg); |
699 } | 691 } |
700 | 692 |
701 void SlaveSM::DoGetPrevEntry() { | 693 void SlaveSM::DoGetPrevEntry() { |
702 DEBUGMSG("\t\t\tSlave DoGetPrevEntry\n"); | 694 DEBUGMSG("\t\t\tSlave DoGetPrevEntry\n"); |
703 Message msg; | 695 Message msg; |
704 msg.command = GET_PREV_ENTRY; | 696 msg.command = GET_PREV_ENTRY; |
705 | 697 |
706 if (input_->msg.arg1) { | 698 if (input_->msg.arg1) { |
707 // We only support one list. | 699 // We only support one list. |
708 msg.result = RESULT_UNKNOWN_COMMAND; | 700 msg.result = RESULT_UNKNOWN_COMMAND; |
709 } else { | 701 } else { |
710 msg.result = GetEntryFromList(); | 702 msg.result = GetEntryFromList(); |
711 if (msg.result == RESULT_PENDING) { | |
712 // We are not done yet. | |
713 msg_ = msg; | |
714 return; | |
715 } | |
716 msg.long_arg1 = reinterpret_cast<int64>(entry_); | 703 msg.long_arg1 = reinterpret_cast<int64>(entry_); |
717 } | 704 } |
718 SendMsg(msg); | 705 SendMsg(msg); |
719 } | 706 } |
720 | 707 |
721 // Move to the next or previous entry on the list. | 708 // Move to the next or previous entry on the list. |
722 int32 SlaveSM::GetEntryFromList() { | 709 int32 SlaveSM::GetEntryFromList() { |
723 DEBUGMSG("\t\t\tSlave GetEntryFromList\n"); | 710 DEBUGMSG("\t\t\tSlave GetEntryFromList\n"); |
724 if (input_->msg.long_arg1 != reinterpret_cast<int64>(entry_)) | 711 if (input_->msg.long_arg1 != reinterpret_cast<int64>(entry_)) |
725 return RESULT_INVALID_PARAMETER; | 712 return RESULT_INVALID_PARAMETER; |
726 | 713 |
727 // We know that the current iteration is valid. | 714 // We know that the current iteration is valid. |
728 if (entry_) | 715 if (entry_) |
729 entry_->Close(); | 716 entry_->Close(); |
730 | 717 |
731 int rv; | 718 bool ret; |
732 if (input_->msg.command == GET_NEXT_ENTRY) { | 719 if (input_->msg.command == GET_NEXT_ENTRY) { |
733 rv = cache_->OpenNextEntry(&iterator_, | 720 ret = cache_->OpenNextEntry(&iterator_, |
734 reinterpret_cast<disk_cache::Entry**>(&entry_), | 721 reinterpret_cast<disk_cache::Entry**>(&entry_)); |
735 &next_callback_); | |
736 } else { | 722 } else { |
737 DCHECK(input_->msg.command == GET_PREV_ENTRY); | 723 DCHECK(input_->msg.command == GET_PREV_ENTRY); |
738 rv = cache_->OpenPrevEntry(&iterator_, | 724 ret = cache_->OpenPrevEntry(&iterator_, |
739 reinterpret_cast<disk_cache::Entry**>(&entry_), | 725 reinterpret_cast<disk_cache::Entry**>(&entry_)); |
740 &next_callback_); | |
741 } | |
742 DCHECK_EQ(net::ERR_IO_PENDING, rv); | |
743 return RESULT_PENDING; | |
744 } | |
745 | |
746 void SlaveSM::DoGetEntryComplete(int result) { | |
747 DEBUGMSG("\t\t\tSlave DoGetEntryComplete\n"); | |
748 if (result != net::OK) { | |
749 entry_ = NULL; | |
750 DEBUGMSG("\t\t\tSlave end of list\n"); | |
751 } | 726 } |
752 | 727 |
753 msg_.result = RESULT_OK; | 728 if (!ret) |
754 msg_.long_arg1 = reinterpret_cast<int64>(entry_); | 729 entry_ = NULL; |
755 SendMsg(msg_); | 730 |
| 731 if (!entry_) |
| 732 DEBUGMSG("\t\t\tSlave end of list\n"); |
| 733 |
| 734 return RESULT_OK; |
756 } | 735 } |
757 | 736 |
758 void SlaveSM::DoCloseEntry() { | 737 void SlaveSM::DoCloseEntry() { |
759 DEBUGMSG("\t\t\tSlave DoCloseEntry\n"); | 738 DEBUGMSG("\t\t\tSlave DoCloseEntry\n"); |
760 Message msg; | 739 Message msg; |
761 msg.command = GET_KEY; | 740 msg.command = GET_KEY; |
762 | 741 |
763 if (!entry_ || input_->msg.long_arg1 != reinterpret_cast<int64>(entry_)) { | 742 if (!entry_ || input_->msg.long_arg1 != reinterpret_cast<int64>(entry_)) { |
764 msg.result = RESULT_INVALID_PARAMETER; | 743 msg.result = RESULT_INVALID_PARAMETER; |
765 } else { | 744 } else { |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
919 | 898 |
920 SlaveSM slave(input_path, pipe); | 899 SlaveSM slave(input_path, pipe); |
921 if (!slave.DoInit()) { | 900 if (!slave.DoInit()) { |
922 printf("Unable to talk with the main process\n"); | 901 printf("Unable to talk with the main process\n"); |
923 return -1; | 902 return -1; |
924 } | 903 } |
925 | 904 |
926 loop.Run(); | 905 loop.Run(); |
927 return 0; | 906 return 0; |
928 } | 907 } |
OLD | NEW |