Home | History | Annotate | Download | only in domain_reliability
      1 // Copyright 2014 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_DOMAIN_RELIABILITY_CONTEXT_H_
      6 #define COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
      7 
      8 #include <deque>
      9 #include <vector>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/time/time.h"
     14 #include "components/domain_reliability/config.h"
     15 #include "components/domain_reliability/domain_reliability_export.h"
     16 #include "components/domain_reliability/scheduler.h"
     17 
     18 class GURL;
     19 
     20 namespace domain_reliability {
     21 
     22 struct DomainReliabilityBeacon;
     23 class DomainReliabilityDispatcher;
     24 class DomainReliabilityUploader;
     25 class MockableTime;
     26 
     27 // The per-domain context for the Domain Reliability client; includes the
     28 // domain's config and per-resource beacon queues.
     29 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContext {
     30  public:
     31   DomainReliabilityContext(
     32       MockableTime* time,
     33       const DomainReliabilityScheduler::Params& scheduler_params,
     34       const std::string& upload_reporter_string,
     35       DomainReliabilityDispatcher* dispatcher,
     36       DomainReliabilityUploader* uploader,
     37       scoped_ptr<const DomainReliabilityConfig> config);
     38   ~DomainReliabilityContext();
     39 
     40   // Notifies the context of a beacon on its domain(s); may or may not save the
     41   // actual beacon to be uploaded, depending on the sample rates in the config,
     42   // but will increment one of the request counters in any case.
     43   void OnBeacon(const GURL& url, const DomainReliabilityBeacon& beacon);
     44 
     45   // Called to clear browsing data, since beacons are like browsing history.
     46   void ClearBeacons();
     47 
     48   void GetQueuedDataForTesting(
     49       size_t resource_index,
     50       std::vector<DomainReliabilityBeacon>* beacons_out,
     51       uint32* successful_requests_out,
     52       uint32* failed_requests_out) const;
     53 
     54   const DomainReliabilityConfig& config() const { return *config_.get(); }
     55 
     56   // Maximum number of beacons queued per context; if more than this many are
     57   // queued; the oldest beacons will be removed.
     58   static const size_t kMaxQueuedBeacons;
     59 
     60  private:
     61   class ResourceState;
     62 
     63   typedef ScopedVector<ResourceState> ResourceStateVector;
     64   typedef ResourceStateVector::const_iterator ResourceStateIterator;
     65 
     66   void InitializeResourceStates();
     67   void ScheduleUpload(base::TimeDelta min_delay, base::TimeDelta max_delay);
     68   void StartUpload();
     69   void OnUploadComplete(bool success);
     70 
     71   scoped_ptr<const base::Value> CreateReport(base::TimeTicks upload_time) const;
     72 
     73   // Remembers the current state of the context when an upload starts. Can be
     74   // called multiple times in a row (without |CommitUpload|) if uploads fail
     75   // and are retried.
     76   void MarkUpload();
     77 
     78   // Uses the state remembered by |MarkUpload| to remove successfully uploaded
     79   // data but keep beacons and request counts added after the upload started.
     80   // N.B.: There is no equivalent "RollbackUpload" that needs to be called on a
     81   // failed upload.
     82   void CommitUpload();
     83 
     84   // Finds and removes the oldest beacon. DCHECKs if there is none. (Called
     85   // when there are too many beacons queued.)
     86   void RemoveOldestBeacon();
     87 
     88   scoped_ptr<const DomainReliabilityConfig> config_;
     89   MockableTime* time_;
     90   const std::string& upload_reporter_string_;
     91   DomainReliabilityScheduler scheduler_;
     92   DomainReliabilityDispatcher* dispatcher_;
     93   DomainReliabilityUploader* uploader_;
     94 
     95   // Each ResourceState in |states_| corresponds to the Resource of the same
     96   // index in the config.
     97   ResourceStateVector states_;
     98   size_t beacon_count_;
     99   size_t uploading_beacon_count_;
    100   base::TimeTicks upload_time_;
    101   base::TimeTicks last_upload_time_;
    102 
    103   base::WeakPtrFactory<DomainReliabilityContext> weak_factory_;
    104 
    105   DISALLOW_COPY_AND_ASSIGN(DomainReliabilityContext);
    106 };
    107 
    108 }  // namespace domain_reliability
    109 
    110 #endif  // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_H_
    111