1 // Copyright (c) 2011 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 // Enumerate the various item subtypes that are supported by sync.
6 // Each sync object is expected to have an immutable object type.
7 // An object's type is inferred from the type of data it holds.
8
9 #ifndef CHROME_BROWSER_SYNC_SYNCABLE_MODEL_TYPE_H_
10 #define CHROME_BROWSER_SYNC_SYNCABLE_MODEL_TYPE_H_
11 #pragma once
12
13 #include <bitset>
14 #include <set>
15 #include <string>
16
17 #include "base/logging.h"
18 #include "base/time.h"
19
20 class ListValue;
21 class StringValue;
22
23 namespace sync_pb {
24 class EntitySpecifics;
25 class SyncEntity;
26 }
27
28 namespace syncable {
29
30 enum ModelType {
31 // Object type unknown. Objects may transition through
32 // the unknown state during their initial creation, before
33 // their properties are set. After deletion, object types
34 // are generally preserved.
35 UNSPECIFIED,
36 // A permanent folder whose children may be of mixed
37 // datatypes (e.g. the "Google Chrome" folder).
38 TOP_LEVEL_FOLDER,
39
40 // ------------------------------------ Start of "real" model types.
41 // The model types declared before here are somewhat special, as they
42 // they do not correspond to any browser data model. The remaining types
43 // are bona fide model types; all have a related browser data model and
44 // can be represented in the protocol using an extension to the
45 // EntitySpecifics protocol buffer.
46 //
47 // A bookmark folder or a bookmark URL object.
48 BOOKMARKS,
49 FIRST_REAL_MODEL_TYPE = BOOKMARKS, // Declared 2nd, for debugger prettiness.
50
51 // A preference folder or a preference object.
52 PREFERENCES,
53 // A password folder or password object.
54 PASSWORDS,
55 // An AutofillProfile Object
56 AUTOFILL_PROFILE,
57 // An autofill folder or an autofill object.
58 AUTOFILL,
59
60 // A themes folder or a themes object.
61 THEMES,
62 // A typed_url folder or a typed_url object.
63 TYPED_URLS,
64 // An extension folder or an extension object.
65 EXTENSIONS,
66 // An object represeting a set of Nigori keys.
67 NIGORI,
68 // An object representing a browser session.
69 SESSIONS,
70 // An app folder or an app object.
71 APPS,
72
73 MODEL_TYPE_COUNT,
74 };
75
76 typedef std::bitset<MODEL_TYPE_COUNT> ModelTypeBitSet;
77 typedef std::set<ModelType> ModelTypeSet;
78
ModelTypeFromInt(int i)79 inline ModelType ModelTypeFromInt(int i) {
80 DCHECK_GE(i, 0);
81 DCHECK_LT(i, MODEL_TYPE_COUNT);
82 return static_cast<ModelType>(i);
83 }
84
85 void AddDefaultExtensionValue(syncable::ModelType datatype,
86 sync_pb::EntitySpecifics* specifics);
87
88 // Extract the model type of a SyncEntity protocol buffer. ModelType is a
89 // local concept: the enum is not in the protocol. The SyncEntity's ModelType
90 // is inferred from the presence of particular datatype extensions in the
91 // entity specifics.
92 ModelType GetModelType(const sync_pb::SyncEntity& sync_entity);
93
94 // Extract the model type from an EntitySpecifics extension. Note that there
95 // are some ModelTypes (like TOP_LEVEL_FOLDER) that can't be inferred this way;
96 // prefer using GetModelType where possible.
97 ModelType GetModelTypeFromSpecifics(const sync_pb::EntitySpecifics& specifics);
98
99 // Determine a model type from the field number of its associated
100 // EntitySpecifics extension.
101 ModelType GetModelTypeFromExtensionFieldNumber(int field_number);
102
103 // Return the field number of the EntitySpecifics extension associated with
104 // a model type.
105 int GetExtensionFieldNumberFromModelType(ModelType model_type);
106
107 // Returns a string that represents the name of |model_type|.
108 std::string ModelTypeToString(ModelType model_type);
109
110 // Handles all model types, and not just real ones.
111 //
112 // Caller takes ownership of returned value.
113 StringValue* ModelTypeToValue(ModelType model_type);
114
115 std::string ModelTypeSetToString(const ModelTypeSet& model_types);
116
117 // Returns the ModelType corresponding to the name |model_type_string|.
118 ModelType ModelTypeFromString(const std::string& model_type_string);
119
120 // Converts a string into a model type bitset. If successful, returns true. If
121 // failed to parse string, returns false and model_types is unspecified.
122 bool ModelTypeBitSetFromString(
123 const std::string& model_type_bitset_string,
124 ModelTypeBitSet* model_types);
125
126 // Convert a ModelTypeSet to a ModelTypeBitSet.
127 ModelTypeBitSet ModelTypeBitSetFromSet(const ModelTypeSet& set);
128
129 // Caller takes ownership of returned list.
130 ListValue* ModelTypeBitSetToValue(const ModelTypeBitSet& model_types);
131
132 // Caller takes ownership of returned list.
133 ListValue* ModelTypeSetToValue(const ModelTypeSet& model_types);
134
135 // Returns a string corresponding to the syncable tag for this datatype.
136 std::string ModelTypeToRootTag(ModelType type);
137
138 // Posts timedeltas to histogram of datatypes. Allows tracking of the frequency
139 // at which datatypes cause syncs.
140 void PostTimeToTypeHistogram(ModelType model_type, base::TimeDelta time);
141
142 // Convert a real model type to a notification type (used for
143 // subscribing to server-issued notifications). Returns true iff
144 // |model_type| was a real model type and |notification_type| was
145 // filled in.
146 bool RealModelTypeToNotificationType(ModelType model_type,
147 std::string* notification_type);
148
149 // Converts a notification type to a real model type. Returns true
150 // iff |notification_type| was the notification type of a real model
151 // type and |model_type| was filled in.
152 bool NotificationTypeToRealModelType(const std::string& notification_type,
153 ModelType* model_type);
154
155 } // namespace syncable
156
157 #endif // CHROME_BROWSER_SYNC_SYNCABLE_MODEL_TYPE_H_
158