Welcome to mirror list, hosted at ThFree Co, Russian Federation.

local_ads_manager.hpp « map - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b790918c06d76a54798142490116c7bc8b0b48d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#pragma once

#include "local_ads/statistics.hpp"

#include "drape_frontend/custom_features_context.hpp"
#include "drape_frontend/drape_engine_safe_ptr.hpp"

#include "drape/pointers.hpp"

#include "geometry/rect2d.hpp"
#include "geometry/screenbase.hpp"

#include "indexer/feature.hpp"
#include "indexer/ftypes_mapping.hpp"
#include "indexer/mwm_set.hpp"

#include "base/thread.hpp"

#include <atomic>
#include <chrono>
#include <functional>
#include <map>
#include <mutex>
#include <set>
#include <string>
#include <vector>

namespace feature
{
class TypesHolder;
}

class BookmarkManager;

class LocalAdsManager final
{
public:
  using GetMwmsByRectFn = std::function<std::vector<MwmSet::MwmId>(m2::RectD const &)>;
  using GetMwmIdByNameFn = std::function<MwmSet::MwmId(std::string const &)>;
  using ReadFeatureTypeFn = std::function<void(FeatureType const &)>;
  using ReadFeaturesFn = std::function<void(ReadFeatureTypeFn const &,
                                            std::vector<FeatureID> const & features)>;
  using GetFeatureByIdFn = std::function<bool(FeatureID const &, FeatureType &)>;
  using Timestamp = local_ads::Timestamp;

  LocalAdsManager(GetMwmsByRectFn && getMwmsByRectFn, GetMwmIdByNameFn && getMwmIdByName,
                  ReadFeaturesFn && readFeaturesFn, GetFeatureByIdFn && getFeatureByIDFn);
  LocalAdsManager(LocalAdsManager && /* localAdsManager */) = default;

  void Startup(BookmarkManager * bmManager);
  void SetDrapeEngine(ref_ptr<df::DrapeEngine> engine);
  void UpdateViewport(ScreenBase const & screen);

  void OnDownloadCountry(std::string const & countryName);
  void OnDeleteCountry(std::string const & countryName);

  void Invalidate();

  local_ads::Statistics & GetStatistics() { return m_statistics; }
  local_ads::Statistics const & GetStatistics() const { return m_statistics; }
    
  bool Contains(FeatureID const & featureId) const;
  bool IsSupportedType(feature::TypesHolder const & types) const;

  std::string GetCompanyUrl(FeatureID const & featureId) const;

private:
  enum class RequestType
  {
    Download,
    Delete
  };
  using Request = std::pair<MwmSet::MwmId, RequestType>;

  void ProcessRequests(std::set<Request> && campaignMwms);

  void ReadCampaignFile(std::string const & campaignFile);
  void WriteCampaignFile(std::string const & campaignFile);

  void UpdateFeaturesCache(df::CustomFeatures && features);
  void ClearLocalAdsForMwm(MwmSet::MwmId const &mwmId);

  void FillSupportedTypes();

  // Returned value means if downloading process finished correctly or was interrupted
  // by some reason.
  bool DownloadCampaign(MwmSet::MwmId const & mwmId, std::vector<uint8_t> & bytes);
  
  void RequestCampaigns(std::vector<MwmSet::MwmId> && mwmIds);

  GetMwmsByRectFn const m_getMwmsByRectFn;
  GetMwmIdByNameFn const m_getMwmIdByNameFn;
  ReadFeaturesFn const m_readFeaturesFn;
  GetFeatureByIdFn const m_getFeatureByIdFn;

  std::atomic<BookmarkManager *> m_bmManager;

  df::DrapeEngineSafePtr m_drapeEngine;

  std::map<std::string, bool> m_campaigns;
  struct CampaignInfo
  {
    Timestamp m_created;
    std::vector<uint8_t> m_data;
  };
  std::map<std::string, CampaignInfo> m_info;
  std::mutex m_campaignsMutex;

  df::CustomFeatures m_featuresCache;
  mutable std::mutex m_featuresCacheMutex;

  ftypes::HashSetMatcher<uint32_t> m_supportedTypes;

  struct BackoffStats
  {
    BackoffStats() = default;
    BackoffStats(std::chrono::steady_clock::time_point lastDownloading,
                 std::chrono::seconds currentTimeout,
                 uint8_t attemptsCount, bool fileIsAbsent)
      : m_lastDownloading(lastDownloading)
      , m_currentTimeout(currentTimeout)
      , m_attemptsCount(attemptsCount)
      , m_fileIsAbsent(fileIsAbsent)
    {}

    std::chrono::steady_clock::time_point m_lastDownloading = {};
    std::chrono::seconds m_currentTimeout = std::chrono::seconds(0);
    uint8_t m_attemptsCount = 0;
    bool m_fileIsAbsent = false;

    bool CanRetry() const;
  };
  std::map<MwmSet::MwmId, BackoffStats> m_failedDownloads;
  std::set<MwmSet::MwmId> m_downloadingMwms;
  
  local_ads::Statistics m_statistics;
};