Home | History | Annotate | Download | only in media
      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 
     12 namespace content {
     13 
     14 // Helper enum used for histogramming calls to WebRTC APIs from JavaScript.
     15 enum JavaScriptAPIName {
     16   WEBKIT_GET_USER_MEDIA,
     17   WEBKIT_PEER_CONNECTION,
     18   WEBKIT_DEPRECATED_PEER_CONNECTION,
     19   WEBKIT_RTC_PEER_CONNECTION,
     20   WEBKIT_GET_MEDIA_DEVICES,
     21   INVALID_NAME
     22 };
     23 
     24 // Helper method used to collect information about the number of times
     25 // different WebRTC APIs are called from JavaScript.
     26 //
     27 // This contributes to two histograms; the former is a raw count of
     28 // the number of times the APIs are called, and be viewed at
     29 // chrome://histograms/WebRTC.webkitApiCount.
     30 //
     31 // The latter is a count of the number of times the APIs are called
     32 // that gets incremented only once per "session" as established by the
     33 // PerSessionWebRTCAPIMetrics singleton below. It can be viewed at
     34 // chrome://histograms/WebRTC.webkitApiCountPerSession.
     35 void UpdateWebRTCMethodCount(JavaScriptAPIName api_name);
     36 
     37 // A singleton that keeps track of the number of MediaStreams being
     38 // sent over PeerConnections. It uses the transition to zero such
     39 // streams to demarcate the start of a new "session". Note that this
     40 // is a rough approximation of sessions, as you could conceivably have
     41 // multiple tabs using this renderer process, and each of them using
     42 // PeerConnections.
     43 //
     44 // The UpdateWebRTCMethodCount function above uses this class to log a
     45 // metric at most once per session.
     46 class CONTENT_EXPORT PerSessionWebRTCAPIMetrics : public base::NonThreadSafe {
     47  public:
     48   virtual ~PerSessionWebRTCAPIMetrics();
     49 
     50   static PerSessionWebRTCAPIMetrics* GetInstance();
     51 
     52   // Increment/decrement the number of streams being sent or received
     53   // over any current PeerConnection.
     54   void IncrementStreamCounter();
     55   void DecrementStreamCounter();
     56 
     57  protected:
     58   friend struct DefaultSingletonTraits<PerSessionWebRTCAPIMetrics>;
     59   friend void UpdateWebRTCMethodCount(JavaScriptAPIName);
     60 
     61   // Protected so that unit tests can test without this being a
     62   // singleton.
     63   PerSessionWebRTCAPIMetrics();
     64 
     65   // Overridable by unit tests.
     66   virtual void LogUsage(JavaScriptAPIName api_name);
     67 
     68   // Called by UpdateWebRTCMethodCount above. Protected rather than
     69   // private so that unit tests can call it.
     70   void LogUsageOnlyOnce(JavaScriptAPIName api_name);
     71 
     72  private:
     73   void ResetUsage();
     74 
     75   int num_streams_;
     76   bool has_used_api_[INVALID_NAME];
     77 
     78   DISALLOW_COPY_AND_ASSIGN(PerSessionWebRTCAPIMetrics);
     79 };
     80 
     81 } //  namespace content
     82 
     83 #endif  // CONTENT_RENDERER_MEDIA_WEBRTC_UMA_HISTOGRAMS_H_
     84