1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // Upload util for usage stats
31 // example:
32 //
33 // UploadUtil uploader;
34 // std::vector<pair<string, string> > params;
35 // params.push_back(make_pair("hl", "ja"));
36 // params.push_back(make_pair("v", "0.0.0.0"));
37 // uploader.SetHeader("Daily", 100, params);
38 // uploader.AddBooleanValue("Boolean", false);
39 // bool result = uploader.AttemptUpload();
40 
41 #ifndef MOZC_USAGE_STATS_UPLOAD_UTIL_H_
42 #define MOZC_USAGE_STATS_UPLOAD_UTIL_H_
43 
44 #include <string>
45 #include <utility>
46 #include <vector>
47 
48 #include "base/port.h"
49 
50 namespace mozc {
51 namespace usage_stats {
52 class UploadUtil {
53  public:
54   UploadUtil();
55   virtual ~UploadUtil();
56 
57   // Sets usage stats header and optional url params.
58   // usage stats data format is:
59   // <type>&<elapsed_sec>&<name1>=<val1> ...
60   // type is transmission type (Daily, Weekly, etc.)
61   // elapsed_sec is the time elapsed since the last transmission.
62   void SetHeader(
63       const string &type, int elapsed_sec,
64       const std::vector<std::pair<string, string> > &optional_url_params);
65 
66   // Enables using HTTPS. Default is false.
67   void SetUseHttps(bool use_https);
68 
69   // Adds count data.
70   // &<name>:c=<count>
71   void AddCountValue(const string &name, uint32 count);
72 
73   // Adds timing data.
74   // &<name>:t=<num_timings>;<avg_time>;<min_time>;<max_time>
75   void AddTimingValue(const string &name, uint32 num_timings, uint32 avg_time,
76                       uint32 min_time, uint32 max_time);
77 
78   // Adds integer data.
79   // &<name>:i=<value>
80   void AddIntegerValue(const string &name, int int_value);
81 
82   // Adds boolean data.
83   // &<name>:b=<value>
84   void AddBooleanValue(const string &name, bool boolean_value);
85 
86   // Remove all data
87   void RemoveAllValues();
88 
89   // Returns false when upload failed.
90   bool Upload();
91 
92  private:
93   string stat_header_;
94   string stat_values_;
95   std::vector<std::pair<string, string> > optional_url_params_;
96   bool use_https_;
97 
98   DISALLOW_COPY_AND_ASSIGN(UploadUtil);
99 };
100 
101 }  // namespace usage_stats
102 }  // namespace mozc
103 
104 #endif  // MOZC_USAGE_STATS_UPLOAD_UTIL_H_
105