Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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 #include "content/public/browser/user_metrics.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/bind.h"
     10 #include "base/lazy_instance.h"
     11 #include "content/public/browser/browser_thread.h"
     12 
     13 namespace content {
     14 namespace {
     15 
     16 base::LazyInstance<std::vector<ActionCallback> > g_action_callbacks =
     17     LAZY_INSTANCE_INITIALIZER;
     18 
     19 // Forward declare because of circular dependency.
     20 void CallRecordOnUI(const std::string& action);
     21 
     22 void Record(const char *action) {
     23   if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
     24     BrowserThread::PostTask(
     25         BrowserThread::UI,
     26         FROM_HERE,
     27         base::Bind(&CallRecordOnUI, action));
     28     return;
     29   }
     30 
     31   for (size_t i = 0; i < g_action_callbacks.Get().size(); i++)
     32     g_action_callbacks.Get()[i].Run(action);
     33 }
     34 
     35 void CallRecordOnUI(const std::string& action) {
     36   Record(action.c_str());
     37 }
     38 
     39 }  // namespace
     40 
     41 void RecordAction(const UserMetricsAction& action) {
     42   Record(action.str_);
     43 }
     44 
     45 void RecordComputedAction(const std::string& action) {
     46   Record(action.c_str());
     47 }
     48 
     49 void AddActionCallback(const ActionCallback& callback) {
     50   g_action_callbacks.Get().push_back(callback);
     51 }
     52 
     53 void RemoveActionCallback(const ActionCallback& callback) {
     54   for (size_t i = 0; i < g_action_callbacks.Get().size(); i++) {
     55     if (g_action_callbacks.Get()[i].Equals(callback)) {
     56       g_action_callbacks.Get().erase(g_action_callbacks.Get().begin() + i);
     57       return;
     58     }
     59   }
     60 }
     61 
     62 }  // namespace content
     63