Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / copresence / handlers / gcm_handler_impl.h
blobd193eb8a172481253dce2379cee9d5d9bcccc7e7
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_COPRESENCE_HANDLERS_GCM_HANDLER_IMPL_H_
6 #define COMPONENTS_COPRESENCE_HANDLERS_GCM_HANDLER_IMPL_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback_forward.h"
12 #include "base/cancelable_callback.h"
13 #include "components/copresence/handlers/gcm_handler.h"
14 #include "components/copresence/public/copresence_constants.h"
15 #include "components/gcm_driver/gcm_app_handler.h"
16 #include "components/gcm_driver/gcm_client.h"
18 namespace gcm {
19 class GCMDriver;
22 namespace copresence {
24 class DirectiveHandler;
26 // This class handles GCM messages from the Copresence server.
27 class GCMHandlerImpl final : public GCMHandler,
28 public gcm::GCMAppHandler {
29 public:
30 // Callback to report that registration has completed.
31 // Returns an empty ID if registration failed.
32 using RegistrationCallback = base::Callback<void(const std::string&)>;
34 static const char kCopresenceAppId[];
35 static const char kCopresenceSenderId[];
36 static const char kGcmMessageKey[];
38 // |gcm_driver| is required, but may disappear if we get a ShutdownHandler()
39 // call first. |directive_handler| must outlive us. The caller owns both.
40 GCMHandlerImpl(gcm::GCMDriver* gcm_driver,
41 DirectiveHandler* directive_handler,
42 const MessagesCallback& new_messages_callback);
43 ~GCMHandlerImpl() override;
45 // GCMHandler override.
46 void GetGcmId(const RegistrationCallback& callback) override;
48 private:
49 friend class GCMHandlerTest;
51 // GCMAppHandler overrides
52 void ShutdownHandler() override;
53 void OnMessage(const std::string& app_id,
54 const gcm::IncomingMessage& message) override;
55 void OnMessagesDeleted(const std::string& app_id) override;
56 void OnSendError(
57 const std::string& /* app_id */,
58 const gcm::GCMClient::SendErrorDetails& /* send_error_details */)
59 override;
60 void OnSendAcknowledged(const std::string& /* app_id */,
61 const std::string& /* message_id */) override;
62 bool CanHandle(const std::string& app_id) const override;
64 // GCM Registration has finished. Notify clients as appropriate.
65 void RegistrationComplete(const std::string& registration_id,
66 gcm::GCMClient::Result result);
68 gcm::GCMDriver* driver_;
69 DirectiveHandler* const directive_handler_;
70 MessagesCallback new_messages_callback_;
72 std::string gcm_id_;
73 std::vector<RegistrationCallback> pending_id_requests_;
75 base::CancelableCallback<void(const std::string&,
76 gcm::GCMClient::Result)> registration_callback_;
78 DISALLOW_COPY_AND_ASSIGN(GCMHandlerImpl);
81 } // namespace copresence
83 #endif // COMPONENTS_COPRESENCE_HANDLERS_GCM_HANDLER_IMPL_H_