| OLD | NEW |
| (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 COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_WEEKLY_ACTIVITY_STORAGE_H_ |
| 6 #define COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_WEEKLY_ACTIVITY_STORAGE_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <unordered_map> |
| 10 |
| 11 #include "base/macros.h" |
| 12 |
| 13 namespace contextual_search { |
| 14 |
| 15 // An abstract class that stores weekly user interaction data in device-specific |
| 16 // integer storage. Only a limited storage window is supported, set through the |
| 17 // constructor. Allows callers to read and write user actions to persistent |
| 18 // storage on the device by overriding the ReadStorage and WriteStorage calls. |
| 19 // A user view of some UX is an "Impression", and user interaction is considered |
| 20 // a "Click" even if the triggering gesture was something else. Together they |
| 21 // produce the Click-Through-Rate, or CTR. |
| 22 class WeeklyActivityStorage { |
| 23 public: |
| 24 // Constructs an instance that will manage at least |weeks_needed| weeks of |
| 25 // data. |
| 26 WeeklyActivityStorage(int weeks_needed); |
| 27 virtual ~WeeklyActivityStorage(); |
| 28 |
| 29 // Advances the accessible storage range to end at the given |week_number|. |
| 30 // Since only a limited number of storage weeks are supported, advancing to |
| 31 // a different week makes data from weeks than the range size inaccessible. |
| 32 // This must be called for each week before reading or writing any data |
| 33 // for that week. |
| 34 // HasData will return true for all the weeks that still have accessible data. |
| 35 void AdvanceToWeek(int week_number); |
| 36 |
| 37 // Returns the number of clicks for the given week. |
| 38 int ReadClicks(int week_number); |
| 39 // Writes |value| into the number of clicks for the given |week_number|. |
| 40 void WriteClicks(int week_number, int value); |
| 41 |
| 42 // Returns the number of impressions for the given week. |
| 43 int ReadImpressions(int week_number); |
| 44 // Writes |value| into the number of impressions for the given |week_number|. |
| 45 void WriteImpressions(int week_number, int value); |
| 46 |
| 47 // Returns whether the given |week_number| has data, based on whether |
| 48 // InitData has ever been called for that week. |
| 49 bool HasData(int week_number); |
| 50 // Clears the click and impression counters for the given |week_number|. |
| 51 void ClearData(int week_number); |
| 52 |
| 53 // Reads and returns the value keyed by |storage_bucket|. |
| 54 // If there is no stored value associated with the given bucket then 0 is |
| 55 // returned. |
| 56 virtual int ReadStorage(std::string storage_bucket) = 0; |
| 57 // Overwrites the |value| to the storage bucket keyed by |storage_bucket|, |
| 58 // regardless of whether there is an existing value in the given bucket. |
| 59 virtual void WriteStorage(std::string storage_bucket, int value) = 0; |
| 60 |
| 61 private: |
| 62 // Returns the string key of the storage bin for the given week |which_week|. |
| 63 std::string GetWeekKey(int which_week); |
| 64 // Returns the string key for the "clicks" storage bin for the given week |
| 65 // |which_week|. |
| 66 std::string GetWeekClicksKey(int which_week); |
| 67 // Returns the string key for the "impressions" storage bin for the given week |
| 68 // |which_week|. |
| 69 std::string GetWeekImpressionsKey(int which_week); |
| 70 |
| 71 // Reads and returns the integer keyed by |storage_key|. |
| 72 // If there is no value for the given key then 0 is returned. |
| 73 int ReadInt(std::string storage_key); |
| 74 // Writes the integer |value| to the storage bucket keyed by |storage_key|. |
| 75 void WriteInt(std::string storage_key, int value); |
| 76 |
| 77 // Ensures that activity data is initialized for the given week |which_week|. |
| 78 void EnsureHasActivity(int which_week); |
| 79 |
| 80 // The number of weeks of data that this instance needs to support. |
| 81 int weeks_needed_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(WeeklyActivityStorage); |
| 84 }; |
| 85 |
| 86 } // namespace contextual_search |
| 87 |
| 88 #endif // COMPONENTS_CONTEXTUAL_SEARCH_BROWSER_WEEKLY_ACTIVITY_STORAGE_H_ |
| OLD | NEW |