Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / domain_reliability / util.h
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_UTIL_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_
7
8 #include <map>
9
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/time/time.h"
13 #include "base/tracked_objects.h"
14 #include "components/domain_reliability/domain_reliability_export.h"
15
16 namespace domain_reliability {
17
18 // Attempts to convert a net error and an HTTP response code into the status
19 // string that should be recorded in a beacon. Returns true if it could.
20 //
21 // N.B.: This functions as the whitelist of "safe" errors to report; network-
22 //       local errors are purposefully not converted to avoid revealing
23 //       information about the local network to the remote server.
24 bool GetDomainReliabilityBeaconStatus(
25     int net_error,
26     int http_response_code,
27     std::string* beacon_status_out);
28
29 // Mockable wrapper around TimeTicks::Now and Timer. Mock version is in
30 // test_util.h.
31 // TODO(ttuttle): Rename to Time{Provider,Source,?}.
32 class DOMAIN_RELIABILITY_EXPORT MockableTime {
33  public:
34   // Mockable wrapper around (a subset of) base::Timer.
35   class DOMAIN_RELIABILITY_EXPORT Timer {
36    public:
37     virtual ~Timer();
38
39     virtual void Start(const tracked_objects::Location& posted_from,
40                        base::TimeDelta delay,
41                        const base::Closure& user_task) = 0;
42     virtual void Stop() = 0;
43     virtual bool IsRunning() = 0;
44
45    protected:
46     Timer();
47   };
48
49   virtual ~MockableTime();
50
51   // Returns base::Time::Now() or a mocked version thereof.
52   virtual base::Time Now() = 0;
53   // Returns base::TimeTicks::Now() or a mocked version thereof.
54   virtual base::TimeTicks NowTicks() = 0;
55   // Returns a new Timer, or a mocked version thereof.
56   virtual scoped_ptr<MockableTime::Timer> CreateTimer() = 0;
57
58  protected:
59   MockableTime();
60
61  private:
62   DISALLOW_COPY_AND_ASSIGN(MockableTime);
63 };
64
65 // Implementation of MockableTime that passes through to
66 // base::Time{,Ticks}::Now() and base::Timer.
67 class DOMAIN_RELIABILITY_EXPORT ActualTime : public MockableTime {
68  public:
69   ActualTime();
70
71   virtual ~ActualTime();
72
73   // MockableTime implementation:
74   virtual base::Time Now() OVERRIDE;
75   virtual base::TimeTicks NowTicks() OVERRIDE;
76   virtual scoped_ptr<MockableTime::Timer> CreateTimer() OVERRIDE;
77 };
78
79 }  // namespace domain_reliability
80
81 #endif  // COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_