1// Copyright (c) 2012 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 CONTENT_RENDERER_MEDIA_WEBRTC_UMA_HISTOGRAMS_H_
6#define CONTENT_RENDERER_MEDIA_WEBRTC_UMA_HISTOGRAMS_H_
7
8#include "base/memory/singleton.h"
9#include "base/threading/non_thread_safe.h"
10#include "content/common/content_export.h"
11#include "content/public/common/media_stream_request.h"
12
13namespace content {
14
15// Used to investigate where UserMediaRequests end up.
16// Only UserMediaRequests that do not log with LogUserMediaRequestResult
17// should call LogUserMediaRequestWithNoResult.
18//
19// Elements in this enum should not be deleted or rearranged; the only
20// permitted operation is to add new elements before
21// NUM_MEDIA_STREAM_REQUEST_WITH_NO_RESULT.
22enum MediaStreamRequestState {
23  MEDIA_STREAM_REQUEST_EXPLICITLY_CANCELLED = 0,
24  MEDIA_STREAM_REQUEST_NOT_GENERATED = 1,
25  MEDIA_STREAM_REQUEST_PENDING_MEDIA_TRACKS = 2,
26  NUM_MEDIA_STREAM_REQUEST_WITH_NO_RESULT
27};
28
29void LogUserMediaRequestWithNoResult(MediaStreamRequestState state);
30void LogUserMediaRequestResult(MediaStreamRequestResult result);
31
32// Helper enum used for histogramming calls to WebRTC APIs from JavaScript.
33enum JavaScriptAPIName {
34  WEBKIT_GET_USER_MEDIA,
35  WEBKIT_PEER_CONNECTION,
36  WEBKIT_DEPRECATED_PEER_CONNECTION,
37  WEBKIT_RTC_PEER_CONNECTION,
38  WEBKIT_GET_MEDIA_DEVICES,
39  INVALID_NAME
40};
41
42// Helper method used to collect information about the number of times
43// different WebRTC APIs are called from JavaScript.
44//
45// This contributes to two histograms; the former is a raw count of
46// the number of times the APIs are called, and be viewed at
47// chrome://histograms/WebRTC.webkitApiCount.
48//
49// The latter is a count of the number of times the APIs are called
50// that gets incremented only once per "session" as established by the
51// PerSessionWebRTCAPIMetrics singleton below. It can be viewed at
52// chrome://histograms/WebRTC.webkitApiCountPerSession.
53void UpdateWebRTCMethodCount(JavaScriptAPIName api_name);
54
55// A singleton that keeps track of the number of MediaStreams being
56// sent over PeerConnections. It uses the transition to zero such
57// streams to demarcate the start of a new "session". Note that this
58// is a rough approximation of sessions, as you could conceivably have
59// multiple tabs using this renderer process, and each of them using
60// PeerConnections.
61//
62// The UpdateWebRTCMethodCount function above uses this class to log a
63// metric at most once per session.
64class CONTENT_EXPORT PerSessionWebRTCAPIMetrics : public base::NonThreadSafe {
65 public:
66  virtual ~PerSessionWebRTCAPIMetrics();
67
68  static PerSessionWebRTCAPIMetrics* GetInstance();
69
70  // Increment/decrement the number of streams being sent or received
71  // over any current PeerConnection.
72  void IncrementStreamCounter();
73  void DecrementStreamCounter();
74
75 protected:
76  friend struct DefaultSingletonTraits<PerSessionWebRTCAPIMetrics>;
77  friend void UpdateWebRTCMethodCount(JavaScriptAPIName);
78
79  // Protected so that unit tests can test without this being a
80  // singleton.
81  PerSessionWebRTCAPIMetrics();
82
83  // Overridable by unit tests.
84  virtual void LogUsage(JavaScriptAPIName api_name);
85
86  // Called by UpdateWebRTCMethodCount above. Protected rather than
87  // private so that unit tests can call it.
88  void LogUsageOnlyOnce(JavaScriptAPIName api_name);
89
90 private:
91  void ResetUsage();
92
93  int num_streams_;
94  bool has_used_api_[INVALID_NAME];
95
96  DISALLOW_COPY_AND_ASSIGN(PerSessionWebRTCAPIMetrics);
97};
98
99} //  namespace content
100
101#endif  // CONTENT_RENDERER_MEDIA_WEBRTC_UMA_HISTOGRAMS_H_
102