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_CORE_BROWSER_WEEKLY_ACTIVITY_STORAGE_H_ 6 #define COMPONENTS_CONTEXTUAL_SEARCH_CORE_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 51 // Reads and returns values from persistent storage. 52 // If there is no stored value then 0 is returned. 53 virtual int ReadClicksForWeekRemainder(int week_remainder) = 0; 54 virtual int ReadImpressionsForWeekRemainder(int week_remainder) = 0; 55 virtual int ReadOldestWeekWritten() = 0; 56 virtual int ReadNewestWeekWritten() = 0; 57 // Writes values to persistent storage. 58 virtual void WriteClicksForWeekRemainder(int week_remainder, int value) = 0; 59 virtual void WriteImpressionsForWeekRemainder(int week_remainder, 60 int value) = 0; 61 virtual void WriteOldestWeekWritten(int value) = 0; 62 virtual void WriteNewestWeekWritten(int value) = 0; 63 64 private: 65 // Returns the key to bin information about the given week |which_week|. 66 int GetWeekRemainder(int which_week); 67 68 // Ensures that activity data is initialized for the given week |which_week|. 69 void EnsureHasActivity(int which_week); 70 71 // The number of weeks of data that this instance needs to support. 72 int weeks_needed_; 73 74 DISALLOW_COPY_AND_ASSIGN(WeeklyActivityStorage); 75 }; 76 77 } // namespace contextual_search 78 79 #endif // COMPONENTS_CONTEXTUAL_SEARCH_CORE_BROWSER_WEEKLY_ACTIVITY_STORAGE_H_ 80