1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/sync/test/integration/fake_server_invalidation_sender.h"
6
7 #include "chrome/browser/profiles/profile.h"
8 #include "components/invalidation/impl/fcm_network_handler.h"
9
10 namespace fake_server {
11
12 namespace {
13
14 const char kInvalidationsFCMAppId[] = "com.google.chrome.fcm.invalidations";
15
16 } // namespace
17
FakeServerInvalidationSender(const std::string & client_id,bool self_notify,base::RepeatingCallback<syncer::FCMNetworkHandler * ()> fcm_network_handler_getter)18 FakeServerInvalidationSender::FakeServerInvalidationSender(
19 const std::string& client_id,
20 bool self_notify,
21 base::RepeatingCallback<syncer::FCMNetworkHandler*()>
22 fcm_network_handler_getter)
23 : client_id_(client_id),
24 self_notify_(self_notify),
25 fcm_network_handler_getter_(fcm_network_handler_getter) {}
26
~FakeServerInvalidationSender()27 FakeServerInvalidationSender::~FakeServerInvalidationSender() {}
28
OnCommit(const std::string & committer_invalidator_client_id,syncer::ModelTypeSet committed_model_types)29 void FakeServerInvalidationSender::OnCommit(
30 const std::string& committer_invalidator_client_id,
31 syncer::ModelTypeSet committed_model_types) {
32 if (!self_notify_ && client_id_ == committer_invalidator_client_id) {
33 return;
34 }
35 syncer::FCMNetworkHandler* fcm_network_handler =
36 fcm_network_handler_getter_.Run();
37 // If there is no FCM network handler registered for this profile, there is
38 // nothing to do. This could be the case during test Setup phase because the
39 // FCM network handlers get assigned in SetupInvalidations() which happens
40 // after SetupSync().
41 if (fcm_network_handler == nullptr) {
42 DLOG(WARNING) << "Received invalidations for the following data types in "
43 "invalidation sender "
44 << this << " will be dropped:"
45 << ModelTypeSetToString(committed_model_types);
46 return;
47 }
48 // For each of the committed model types, pass a message to the FCM Network
49 // Handler to simulate a message from the GCMDriver.
50 for (syncer::ModelType type : committed_model_types) {
51 std::string notification_type;
52 bool result = RealModelTypeToNotificationType(type, ¬ification_type);
53 // We shouldn't ever get commits for non-protocol types.
54 DCHECK(result);
55
56 gcm::IncomingMessage message;
57 // Client doesn't parse the payload.
58 message.data["payload"] = "any_payload";
59 // version doesn't matter, it's not used in the client.
60 message.data["version"] = "1234567890";
61 // The public topic name should be stored in the external name field.
62 message.data["external_name"] = notification_type;
63 // The private topic name is stored in the sender_id field.
64 message.sender_id =
65 "/topics/private/" + notification_type + "-topic_server_user_id";
66 fcm_network_handler->OnMessage(kInvalidationsFCMAppId, message);
67 }
68 }
69
70 } // namespace fake_server
71