15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef COMPONENTS_INVALIDATION_INVALIDATION_H_
65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define COMPONENTS_INVALIDATION_INVALIDATION_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/basictypes.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/memory/weak_ptr.h"
136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/sequenced_task_runner.h"
144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/values.h"
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/invalidation/ack_handle.h"
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/invalidation/invalidation_export.h"
174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "google/cacheinvalidation/include/types.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace syncer {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class DroppedInvalidationTracker;
224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class AckHandler;
234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Represents a local invalidation, and is roughly analogous to
254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// invalidation::Invalidation.  Unlike invalidation::Invalidation, this class
264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// supports "local" ack-tracking and simple serialization to pref values.
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class INVALIDATION_EXPORT Invalidation {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Factory functions.
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static Invalidation Init(const invalidation::ObjectId& id,
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                           int64 version,
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                           const std::string& payload);
334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  static Invalidation InitUnknownVersion(const invalidation::ObjectId& id);
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  static Invalidation InitFromDroppedInvalidation(const Invalidation& dropped);
354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  static scoped_ptr<Invalidation> InitFromValue(
364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      const base::DictionaryValue& value);
374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ~Invalidation();
394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Compares two invalidations.  The comparison ignores ack-tracking state.
414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool Equals(const Invalidation& other) const;
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  invalidation::ObjectId object_id() const;
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool is_unknown_version() const;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Safe to call only if is_unknown_version() returns false.
474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int64 version() const;
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Safe to call only if is_unknown_version() returns false.
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const std::string& payload() const;
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  const AckHandle& ack_handle() const;
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Sets the AckHandler to be used to track this Invalidation.
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  //
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // This should be set by the class that generates the invalidation.  Clients
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // of the Invalidations API should not need to call this.
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  //
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Note that some sources of invalidations do not support ack tracking, and do
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // not set the ack_handler.  This will be hidden from users of this class.
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void SetAckHandler(
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      base::WeakPtr<AckHandler> handler,
636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      scoped_refptr<base::SequencedTaskRunner> handler_task_runner);
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns whether or not this instance supports ack tracking.  This will
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // depend on whether or not the source of invaliadations supports
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // invalidations.
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  //
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Clients can safely ignore this flag.  They can assume that all
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // invalidations support ack tracking.  If they're wrong, then invalidations
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // will be less reliable, but their behavior will be no less correct.
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool SupportsAcknowledgement() const;
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Acknowledges the receipt of this invalidation.
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  //
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Clients should call this on a received invalidation when they have fully
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // processed the invalidation and persisted the results to disk.  Once this
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // function is called, the invalidations system is under no obligation to
79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // re-deliver this invalidation in the event of a crash or restart.
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void Acknowledge() const;
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Informs the ack tracker that this invalidation will not be serviced.
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  //
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If a client's buffer reaches its limit and it is forced to start dropping
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // invalidations, it should call this function before dropping its
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // invalidations in order to allow the ack tracker to drop the invalidation,
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // too.
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  //
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // To indicate recovery from a drop event, the client should call
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Acknowledge() on the most recently dropped inavlidation.
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void Drop();
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_ptr<base::DictionaryValue> ToValue() const;
944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string ToString() const;
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  Invalidation(const invalidation::ObjectId& id,
984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)               bool is_unknown_version,
994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)               int64 version,
1004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)               const std::string& payload,
1014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)               AckHandle ack_handle);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The ObjectId to which this invalidation belongs.
1044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  invalidation::ObjectId id_;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // This flag is set to true if this is an unknown version invalidation.
1074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool is_unknown_version_;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The version number of this invalidation.  Should not be accessed if this is
1104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // an unkown version invalidation.
1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int64 version_;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The payaload associated with this invalidation.  Should not be accessed if
1144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // this is an unknown version invalidation.
1154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string payload_;
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // A locally generated unique ID used to manage local acknowledgements.
1184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  AckHandle ack_handle_;
1196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // The acknowledgement tracking handler and its thread.
1216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::WeakPtr<AckHandler> ack_handler_;
1226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_refptr<base::SequencedTaskRunner> ack_handler_task_runner_;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace syncer
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif  // COMPONENTS_INVALIDATION_INVALIDATION_H_
128