OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "google_apis/gcm/engine/gcm_store_impl.h" | 5 #include "google_apis/gcm/engine/gcm_store_impl.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/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 std::string outgoing_message; | 365 std::string outgoing_message; |
366 std::string key = MakeOutgoingKey(*iter); | 366 std::string key = MakeOutgoingKey(*iter); |
367 s = db_->Get(read_options, | 367 s = db_->Get(read_options, |
368 MakeSlice(key), | 368 MakeSlice(key), |
369 &outgoing_message); | 369 &outgoing_message); |
370 if (!s.ok()) | 370 if (!s.ok()) |
371 break; | 371 break; |
372 mcs_proto::DataMessageStanza data_message; | 372 mcs_proto::DataMessageStanza data_message; |
373 // Skip the initial tag byte and parse the rest to extract the message. | 373 // Skip the initial tag byte and parse the rest to extract the message. |
374 if (data_message.ParseFromString(outgoing_message.substr(1))) { | 374 if (data_message.ParseFromString(outgoing_message.substr(1))) { |
375 DCHECK(!data_message.from().empty()); | 375 DCHECK(!data_message.category().empty()); |
376 if (removed_message_counts.count(data_message.from()) != 0) | 376 if (removed_message_counts.count(data_message.category()) != 0) |
377 removed_message_counts[data_message.from()]++; | 377 removed_message_counts[data_message.category()]++; |
378 else | 378 else |
379 removed_message_counts[data_message.from()] = 1; | 379 removed_message_counts[data_message.category()] = 1; |
380 } | 380 } |
381 DVLOG(1) << "Removing outgoing message with id " << *iter; | 381 DVLOG(1) << "Removing outgoing message with id " << *iter; |
382 s = db_->Delete(write_options, MakeSlice(key)); | 382 s = db_->Delete(write_options, MakeSlice(key)); |
383 if (!s.ok()) | 383 if (!s.ok()) |
384 break; | 384 break; |
385 } | 385 } |
386 if (s.ok()) { | 386 if (s.ok()) { |
387 foreground_task_runner_->PostTask(FROM_HERE, | 387 foreground_task_runner_->PostTask(FROM_HERE, |
388 base::Bind(callback, | 388 base::Bind(callback, |
389 true, | 389 true, |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
681 backend_, | 681 backend_, |
682 persistent_ids, | 682 persistent_ids, |
683 callback)); | 683 callback)); |
684 } | 684 } |
685 | 685 |
686 bool GCMStoreImpl::AddOutgoingMessage(const std::string& persistent_id, | 686 bool GCMStoreImpl::AddOutgoingMessage(const std::string& persistent_id, |
687 const MCSMessage& message, | 687 const MCSMessage& message, |
688 const UpdateCallback& callback) { | 688 const UpdateCallback& callback) { |
689 DCHECK_EQ(message.tag(), kDataMessageStanzaTag); | 689 DCHECK_EQ(message.tag(), kDataMessageStanzaTag); |
690 std::string app_id = reinterpret_cast<const mcs_proto::DataMessageStanza*>( | 690 std::string app_id = reinterpret_cast<const mcs_proto::DataMessageStanza*>( |
691 &message.GetProtobuf())->from(); | 691 &message.GetProtobuf())->category(); |
692 DCHECK(!app_id.empty()); | 692 DCHECK(!app_id.empty()); |
693 if (app_message_counts_.count(app_id) == 0) | 693 if (app_message_counts_.count(app_id) == 0) |
694 app_message_counts_[app_id] = 0; | 694 app_message_counts_[app_id] = 0; |
695 if (app_message_counts_[app_id] < kMessagesPerAppLimit) { | 695 if (app_message_counts_[app_id] < kMessagesPerAppLimit) { |
696 app_message_counts_[app_id]++; | 696 app_message_counts_[app_id]++; |
697 | 697 |
698 blocking_task_runner_->PostTask( | 698 blocking_task_runner_->PostTask( |
699 FROM_HERE, | 699 FROM_HERE, |
700 base::Bind(&GCMStoreImpl::Backend::AddOutgoingMessage, | 700 base::Bind(&GCMStoreImpl::Backend::AddOutgoingMessage, |
701 backend_, | 701 backend_, |
702 persistent_id, | 702 persistent_id, |
703 message, | 703 message, |
704 base::Bind(&GCMStoreImpl::AddOutgoingMessageContinuation, | 704 base::Bind(&GCMStoreImpl::AddOutgoingMessageContinuation, |
705 weak_ptr_factory_.GetWeakPtr(), | 705 weak_ptr_factory_.GetWeakPtr(), |
706 callback, | 706 callback, |
707 app_id))); | 707 app_id))); |
708 return true; | 708 return true; |
709 } | 709 } |
710 return false; | 710 return false; |
711 } | 711 } |
712 | 712 |
713 void GCMStoreImpl::OverwriteOutgoingMessage(const std::string& persistent_id, | |
714 const MCSMessage& message, | |
715 const UpdateCallback& callback) { | |
716 DCHECK_EQ(message.tag(), kDataMessageStanzaTag); | |
717 std::string app_id = reinterpret_cast<const mcs_proto::DataMessageStanza*>( | |
718 &message.GetProtobuf())->category(); | |
719 DCHECK(!app_id.empty()); | |
720 // There should already be pending messages for this app. | |
721 DCHECK(app_message_counts_.count(app_id)); | |
722 // TODO(zea); consider verifying the specific message already exists. | |
fgorski
2014/02/04 22:22:36
s/;/:/
Nicolas Zea
2014/02/12 23:35:46
Done.
| |
723 blocking_task_runner_->PostTask( | |
724 FROM_HERE, | |
725 base::Bind(&GCMStoreImpl::Backend::AddOutgoingMessage, | |
726 backend_, | |
727 persistent_id, | |
728 message, | |
729 callback)); | |
730 } | |
731 | |
713 void GCMStoreImpl::RemoveOutgoingMessage(const std::string& persistent_id, | 732 void GCMStoreImpl::RemoveOutgoingMessage(const std::string& persistent_id, |
714 const UpdateCallback& callback) { | 733 const UpdateCallback& callback) { |
715 blocking_task_runner_->PostTask( | 734 blocking_task_runner_->PostTask( |
716 FROM_HERE, | 735 FROM_HERE, |
717 base::Bind(&GCMStoreImpl::Backend::RemoveOutgoingMessages, | 736 base::Bind(&GCMStoreImpl::Backend::RemoveOutgoingMessages, |
718 backend_, | 737 backend_, |
719 PersistentIdList(1, persistent_id), | 738 PersistentIdList(1, persistent_id), |
720 base::Bind(&GCMStoreImpl::RemoveOutgoingMessagesContinuation, | 739 base::Bind(&GCMStoreImpl::RemoveOutgoingMessagesContinuation, |
721 weak_ptr_factory_.GetWeakPtr(), | 740 weak_ptr_factory_.GetWeakPtr(), |
722 callback))); | 741 callback))); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
772 if (!result->success) { | 791 if (!result->success) { |
773 callback.Run(result.Pass()); | 792 callback.Run(result.Pass()); |
774 return; | 793 return; |
775 } | 794 } |
776 int num_throttled_apps = 0; | 795 int num_throttled_apps = 0; |
777 for (OutgoingMessageMap::const_iterator | 796 for (OutgoingMessageMap::const_iterator |
778 iter = result->outgoing_messages.begin(); | 797 iter = result->outgoing_messages.begin(); |
779 iter != result->outgoing_messages.end(); ++iter) { | 798 iter != result->outgoing_messages.end(); ++iter) { |
780 const mcs_proto::DataMessageStanza* data_message = | 799 const mcs_proto::DataMessageStanza* data_message = |
781 reinterpret_cast<mcs_proto::DataMessageStanza*>(iter->second.get()); | 800 reinterpret_cast<mcs_proto::DataMessageStanza*>(iter->second.get()); |
782 DCHECK(!data_message->from().empty()); | 801 DCHECK(!data_message->category().empty()); |
783 if (app_message_counts_.count(data_message->from()) == 0) | 802 if (app_message_counts_.count(data_message->category()) == 0) |
784 app_message_counts_[data_message->from()] = 1; | 803 app_message_counts_[data_message->category()] = 1; |
785 else | 804 else |
786 app_message_counts_[data_message->from()]++; | 805 app_message_counts_[data_message->category()]++; |
787 if (app_message_counts_[data_message->from()] == kMessagesPerAppLimit) | 806 if (app_message_counts_[data_message->category()] == kMessagesPerAppLimit) |
788 num_throttled_apps++; | 807 num_throttled_apps++; |
789 } | 808 } |
790 UMA_HISTOGRAM_COUNTS("GCM.NumThrottledApps", num_throttled_apps); | 809 UMA_HISTOGRAM_COUNTS("GCM.NumThrottledApps", num_throttled_apps); |
791 callback.Run(result.Pass()); | 810 callback.Run(result.Pass()); |
792 } | 811 } |
793 | 812 |
794 void GCMStoreImpl::AddOutgoingMessageContinuation( | 813 void GCMStoreImpl::AddOutgoingMessageContinuation( |
795 const UpdateCallback& callback, | 814 const UpdateCallback& callback, |
796 const std::string& app_id, | 815 const std::string& app_id, |
797 bool success) { | 816 bool success) { |
(...skipping 16 matching lines...) Expand all Loading... | |
814 removed_message_counts.begin(); | 833 removed_message_counts.begin(); |
815 iter != removed_message_counts.end(); ++iter) { | 834 iter != removed_message_counts.end(); ++iter) { |
816 DCHECK_NE(app_message_counts_.count(iter->first), 0U); | 835 DCHECK_NE(app_message_counts_.count(iter->first), 0U); |
817 app_message_counts_[iter->first] -= iter->second; | 836 app_message_counts_[iter->first] -= iter->second; |
818 DCHECK_GE(app_message_counts_[iter->first], 0); | 837 DCHECK_GE(app_message_counts_[iter->first], 0); |
819 } | 838 } |
820 callback.Run(true); | 839 callback.Run(true); |
821 } | 840 } |
822 | 841 |
823 } // namespace gcm | 842 } // namespace gcm |
OLD | NEW |