DeviceAtlas C++ Api documentation
devatlas.h
1 #ifndef MOBI_MTLD_DA
2 #define MOBI_MTLD_DA
3 #include <mtld/common.h>
4 
5 #define DA_CONTEXT_HAS_REFINE 1
6 #define API_MAJOR_VERSION 2
7 #define API_MINOR_VERSION 3
8 #define API_REVISION_VERSION 0
9 
10 /*
11  * Copyright (c) DeviceAtlas Limited 2022. All Rights Reserved.
12  */
13 
14 namespace Mobi { namespace Mtld { namespace Da {
15 using namespace std;
16 /*
17  * Forward declarations of classes.
18  */
19 
20 class Node;
21 class NodeParse;
22 class REInfo;
23 class RuleGroup;
24 class RuleSet;
25 class UAR;
26 class CPR;
27 struct ChildrenParse;
28 struct ValueParse;
29 
30 
37 enum TreeID {
38  UA,
39  UAPROF,
40  TREEID_MAX = 16 // space for 16 trees without having to recompile.
41 };
42 
43 enum LangID {
44  Default = 0,
45  PHP = 1,
46  Java = 2,
47  DotNET = 3,
48  Cplusplus = 4,
49  Python = 5,
50  Ruby = 6
51 };
52 
53 /*
54  * Basic information from the Version Descriptor
55  */
56 struct VersionInfo {
57  std::string ver;
58  std::string gen;
59  std::string rev;
60  std::string usr;
61  std::string tm;
62  time_t utc;
63 };
64 
65 struct DaConfig {
66  bool include_ua_props;
67  bool include_lang_props;
68  bool include_matched_props;
69  DaConfig()
70  : include_ua_props(true)
71  , include_lang_props(true)
72  , include_matched_props(false) {}
73  DaConfig(bool include_ua_props_, bool include_lang_props_,
74  bool include_matched_props_)
75  : include_ua_props(include_ua_props_)
76  , include_lang_props(include_lang_props_)
77  , include_matched_props(include_matched_props_) {}
78 };
79 
80 DA_EXPORT extern const char *languageHeader;
81 DA_EXPORT extern const char *defaultHeaderTreePriorities[];
82 
89 struct DA_EXPORT HttpHeaders : public CommonHttpHeaders{
90  std::string evidence[TREEID_MAX];
91  std::string chevidence[TREEID_MAX];
92  std::string userlanguage;
93  size_t uaindex;
94  size_t uajsontreewalk;
95  size_t chpltversion;
96  size_t chplt;
97  HttpHeaders(const char *userAgent, const char *acceptLanguage = 0);
98  HttpHeaders(const HeaderMap & headers, const char ***priorities = defaultHeaderPriorities);
99 };
100 
106 using TaggedRegexMap = std::map<LangID, std::vector<REInfo *> >;
107 using ResetWalkMap = std::unordered_map<size_t, std::string>;
108 class DA_EXPORT DeviceAtlas : public CommonDeviceAtlas {
109  friend class DAParse;
110  friend class NodeParse;
111  friend class ReplacementStringParser;
112  friend class RuleGroup;
113  friend struct CPRParser;
114  friend struct RGParser;
115  friend struct UARParser;
116  friend struct AttrVecParser;
117  friend struct CPRulesParser;
118  friend struct UAStockParser;
119  friend struct ResetWalkParser;
120  friend struct DataParse;
121  friend struct ValueParse;
122 
123  VersionInfo vi;
124  const std::vector<REInfo *> *languageReplacementStrings;
125 
126  std::shared_ptr<CPR> cpr;
127  const Property *propid;
128  const Property *proponame;
129  const Property *propoversion;
130  const Property *propoversionnm;
131  const Property *propisbw;
132  TaggedRegexMap replacementStrings;
133  ResetWalkMap resetWalkStrings;
134 
135  vector<string> stockUaFromTree;
136 
137  void initAtlas(std::istream &, const char *[]);
138  void resetRoots();
139  void resetPriorities();
140 
141 public:
142  const Property *getpropId() const { return propid; }
143  const Property *getpropOname() const { return proponame; }
144  const Property *getpropOversion() const { return propoversion; }
145  const Property *getpropOversionNm() const { return propoversionnm; }
146  const Property *getpropIsbw() const { return propisbw; }
147 
148  Node *roots[TREEID_MAX];
149  unsigned long flags;
150 private:
151  DaConfig dcfg;
152  std::shared_ptr<UAR> uar;
153 public:
154  unsigned maxDepth;
155  void doReplacements(const std::vector<size_t> *, char *, size_t) const;
156 
161  struct {
162  size_t _user_agent_matched;
163  size_t _user_agent_unmatched;
164  // dynamic language properties
165  size_t _language_locale;
166  size_t _language;
167  } synthetic;
168 
169  void setConfig(DaConfig &_dcfg) { dcfg = _dcfg; }
170  bool hasUAProps() const { return dcfg.include_ua_props == true; }
171  bool hasLangProps() const { return dcfg.include_lang_props == true; }
172  bool hasMatchedProps() const { return dcfg.include_matched_props == true; }
173 
174  const std::shared_ptr<UAR> getUAR() const { return uar; }
175  const std::shared_ptr<CPR> getCPR() const { return cpr; }
176 
184  static const char *treeUA[];
185  static const char *treeAll[];
186  static const char **getTreeUA();
187  static const char **getTreeAll();
188 
189  enum {
190  NoREFold = 1 << 0
191  , Debug = 1 << 1
192  , OverwriteUnconflicted = 1 << 2
193  };
194  DeviceAtlas() : CommonDeviceAtlas(), propid(0) { resetRoots(); }
195  DeviceAtlas(DaConfig &_dcfg) : CommonDeviceAtlas(), propid(0), dcfg(_dcfg) { resetRoots(); }
203  DeviceAtlas(std::istream &, const char *treeFields[] = getTreeUA(), unsigned long flags = 0);
204  ~DeviceAtlas();
209  void loadDataFromFile(const char *, const char *treeFields[] = getTreeUA(), unsigned long flags = 0);
214  void loadDataFromStream(std::istream &, const char *treeFields[] = getTreeUA(), unsigned long flags = 0);
215  const Value *getValue(size_t) const;
216  const std::string &getResetWalkString(size_t) const;
221  int getTreeRevision() const;
222  time_t getTreeGeneration() const;
223  int getDataRevision() const { return getTreeRevision(); }
224  time_t getDataGeneration() const { return getTreeGeneration(); }
225 };
226 
227 class LocalProperties;
228 
229 class DA_EXPORT DeliveryContext : public CommonDeliveryContext {
230 private:
235  Value user_agent_matched;
236  Value user_agent_unmatched;
237  const DeviceAtlas &da;
238  size_t dapropcount;
239  size_t dalocalcount;
240 
241 
250  bool doConflicts;
251 
252 
253  // the node in the atlas where the search terminated.
254  const Node *node;
255  LocalProperties *localProperties;
256 
257 
258  // The number of properties currently in conflict
259  unsigned conflictCount;
260 
261  // indexed by property: indicates a found-but-conflicted property.
262  bool *conflicts;
263 
264  void getRulesetWithProps(const char *&uastrng);
265  void getRuleset(const char *&uastrng);
266  void runRules(const char *txt, const RuleSet &rules);
267  void doExcls();
268  void doDesktopOs(const HttpHeaders &);
269 
270 public:
271  // Create a context reflecting the properties of the device
272  // identified by the HttpHeaders object.
273  DeliveryContext(const DeviceAtlas &da_, const HttpHeaders &);
274  ~DeliveryContext();
275  std::vector<size_t> *rfskp;
276 
277  const Property &getPropertyDescr(const char *propname, PropertyType *addAs);
278  const Property &getPropertyDescr(size_t idx) const;
279  void setProperty(const Property &, const Value*);
280  void setProperty(size_t, const Value *);
281  const Value *findProperty(const Property &p) const;
282  const Value *findProperty(size_t) const;
283 
284  const DeviceAtlas &getAtlas() const { return da; }
285 
286  // Refine the tree with retrieved client-side properties.
287  void refine(const std::vector<const Value *> &clientprops);
288 
289  // this variant does ad-hoc decoding similar to the other .mobi APIs.
290  void refine(const char *cookieprops);
291  const Value *findGlobalProperty(size_t) const;
292  const Value *findLocalProperty(size_t) const;
293 
294  class const_iterator;
295  friend class const_iterator;
297  public:
298  typedef std::pair<const Property *, const Value *> PVPair;
299  private:
300  const DeliveryContext &props;
301  size_t idx;
302  size_t propcount;
303  friend class DeliveryContext;
304  PVPair builtin;
305  public:
306  const_iterator(const DeliveryContext &props_, size_t idx_);
307  const PVPair operator * () const;
308  const PVPair *operator -> ();
309  const_iterator &operator++();
310  bool operator==(const const_iterator &rhs) const;
311  bool operator!=(const const_iterator &rhs) const;
312  };
313 
314  // called for each node during the search, from the find() template on Node
315  bool operator()(const Node &cur, char *str, size_t offset);
316  // std-style const iterator for all properties.
317  const_iterator begin() const;
318  const_iterator end() const;
319  const_iterator find(const Property &) const;
320  const_iterator find(const char *) const;
321  size_t size() const;
322  size_t totalsize() const;
323  bool empty();
324  const Value *operator[](const Property &) const;
325  const Value *operator[](const char *propname) const;
326 };
327 
328 }}}
329 
330 // pretty-printer for stream output
331 #endif
Definition: dajson.cpp:597
Definition: devatlas.h:108
Definition: devatlas.h:56
Definition: common.h:232
Definition: dajson.cpp:244
Definition: dajson.cpp:613
Definition: devatlas_priv.h:85
Definition: common.h:192
Definition: dajson.cpp:629
Definition: dajson.cpp:463
Definition: dajson.cpp:342
Definition: devatlas_priv.h:16
Definition: dajson.cpp:566
Definition: dajson.cpp:174
Definition: devatlas_priv.h:261
Definition: dajson.cpp:198
Definition: common.h:242
Definition: binary.h:13
Definition: devatlas.h:229
Definition: devatlas_priv.h:269
Definition: devatlas.h:65
Definition: dajson.cpp:499
HttpHeaders represents the evidence used to look up the DeviceAtlas for properties of a specific clie...
Definition: devatlas.h:89
Definition: common.h:142
Definition: dajson.cpp:538