Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: blimp/helium/lazy_syncable_adapter.h

Issue 2522633004: Add LazySyncableAdapter (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « blimp/helium/BUILD.gn ('k') | blimp/helium/lazy_syncable_adapter_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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 #ifndef BLIMP_HELIUM_LAZY_SYNCABLE_ADAPTER_H_
6 #define BLIMP_HELIUM_LAZY_SYNCABLE_ADAPTER_H_
7
8 #include <string>
9
10 #include "base/memory/ptr_util.h"
11 #include "blimp/helium/blimp_helium_export.h"
12 #include "blimp/helium/syncable.h"
13 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
14 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite .h"
15
16 namespace blimp {
17 namespace helium {
18
19 namespace {
20 template <class ChangesetType>
21 std::unique_ptr<ChangesetType> ParseChangesetFromString(
CJ 2016/11/23 22:39:27 Just curious, why is this function in an anon name
steimel 2016/11/28 17:19:26 I was on the fence as to whether it should be a pr
CJ 2016/11/28 19:41:15 Acknowledged.
22 const std::string& changeset) {
23 google::protobuf::io::ArrayInputStream raw_input_stream(changeset.data(),
24 changeset.size());
25 google::protobuf::io::CodedInputStream input_stream(&raw_input_stream);
26 std::unique_ptr<ChangesetType> parsed_changeset =
27 base::MakeUnique<ChangesetType>();
28 parsed_changeset->Parse(&input_stream);
29 return parsed_changeset;
30 }
31 } // namespace
32
33 // The LazySyncableAdapter is used to wrap LazySyncables of any changeset type
34 // into a LazySyncable with a string changeset by serializing the changesets
35 // into a string and parsing them from strings. That way, ObjectSyncState can
36 // just work with LazySyncables of type string.
37 template <class ChangesetType>
38 class BLIMP_HELIUM_EXPORT LazySyncableAdapter
39 : public LazySyncable<std::string> {
40 public:
41 explicit LazySyncableAdapter(LazySyncable<ChangesetType>* object)
scf 2016/11/22 23:35:23 nit: rename to inner_syncable_ ?
steimel 2016/11/23 15:56:43 Done.
42 : object_(object) {}
43 ~LazySyncableAdapter() = default;
44
45 // LazySyncable implementation.
46 std::unique_ptr<std::string> CreateChangeset(Revision from) const override;
47 void ApplyChangeset(const std::string& changeset) override;
48 bool ValidateChangeset(const std::string& changeset) const override;
49 void SetLocalUpdateCallback(
50 const base::Closure& local_update_callback) override {
scf 2016/11/22 23:35:22 for consistency put implementation outside?
steimel 2016/11/23 15:56:43 Done.
51 object_->SetLocalUpdateCallback(local_update_callback);
52 }
53 void ReleaseBefore(Revision checkpoint) override {
scf 2016/11/22 23:35:23 according to @wez i think this was renamed to |bef
steimel 2016/11/23 15:56:43 Okay, changed. Strangely enough, it's currently |c
54 object_->ReleaseBefore(checkpoint);
55 }
56 Revision GetRevision() const override { return object_->GetRevision(); }
57 void PrepareToCreateChangeset(Revision from, base::Closure done) override {
58 object_->PrepareToCreateChangeset(from, done);
59 }
60
61 private:
62 LazySyncable<ChangesetType>* object_;
63
64 DISALLOW_COPY_AND_ASSIGN(LazySyncableAdapter);
65 };
66
67 template <class ChangesetType>
68 std::unique_ptr<std::string>
69 LazySyncableAdapter<ChangesetType>::CreateChangeset(Revision from) const {
70 std::unique_ptr<ChangesetType> changeset = object_->CreateChangeset(from);
scf 2016/11/22 23:35:22 It feels to me that there is a bit of inefficiency
steimel 2016/11/23 15:56:43 That's pretty much exactly what Kevin had in his o
71 std::unique_ptr<std::string> output = base::MakeUnique<std::string>();
72 google::protobuf::io::StringOutputStream raw_output_stream(output.get());
73 google::protobuf::io::CodedOutputStream output_stream(&raw_output_stream);
74 changeset->Serialize(&output_stream);
75 return output;
76 }
77
78 template <class ChangesetType>
79 void LazySyncableAdapter<ChangesetType>::ApplyChangeset(
80 const std::string& changeset) {
81 std::unique_ptr<ChangesetType> parsed_changeset =
82 ParseChangesetFromString<ChangesetType>(changeset);
83 object_->ApplyChangeset(*parsed_changeset);
84 }
85
86 template <class ChangesetType>
87 bool LazySyncableAdapter<ChangesetType>::ValidateChangeset(
88 const std::string& changeset) const {
89 std::unique_ptr<ChangesetType> parsed_changeset =
90 ParseChangesetFromString<ChangesetType>(changeset);
91 return object_->ValidateChangeset(*parsed_changeset);
92 }
93
94 } // namespace helium
95 } // namespace blimp
96 #endif // BLIMP_HELIUM_LAZY_SYNCABLE_ADAPTER_H_
OLDNEW
« no previous file with comments | « blimp/helium/BUILD.gn ('k') | blimp/helium/lazy_syncable_adapter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698