Home | History | Annotate | Download | only in renderer
      1 // Copyright 2013 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/renderer/stats_collection_controller.h"
      6 
      7 #include "base/json/json_writer.h"
      8 #include "base/metrics/histogram.h"
      9 #include "base/metrics/statistics_recorder.h"
     10 #include "base/strings/string_util.h"
     11 #include "content/common/child_process_messages.h"
     12 #include "content/renderer/render_view_impl.h"
     13 #include "gin/handle.h"
     14 #include "gin/object_template_builder.h"
     15 #include "third_party/WebKit/public/web/WebKit.h"
     16 #include "third_party/WebKit/public/web/WebLocalFrame.h"
     17 #include "third_party/WebKit/public/web/WebView.h"
     18 
     19 namespace content {
     20 
     21 namespace {
     22 
     23 bool CurrentRenderViewImpl(RenderViewImpl** out) {
     24   blink::WebLocalFrame* web_frame =
     25       blink::WebLocalFrame::frameForCurrentContext();
     26   if (!web_frame)
     27     return false;
     28 
     29   blink::WebView* web_view = web_frame->view();
     30   if (!web_view)
     31     return false;
     32 
     33   RenderViewImpl* render_view_impl =
     34       RenderViewImpl::FromWebView(web_view);
     35   if (!render_view_impl)
     36     return false;
     37 
     38   *out = render_view_impl;
     39   return true;
     40 }
     41 
     42 // Encodes a WebContentsLoadTime as JSON.
     43 // Input:
     44 // - |load_start_time| - time at which page load started.
     45 // - |load_stop_time| - time at which page load stopped.
     46 // - |result| - returned JSON.
     47 // Example return value:
     48 // {'load_start_ms': 1, 'load_duration_ms': 2.5}
     49 // either value may be null if a web contents hasn't fully loaded.
     50 // load_start_ms is represented as milliseconds since system boot.
     51 void ConvertLoadTimeToJSON(
     52     const base::Time& load_start_time,
     53     const base::Time& load_stop_time,
     54     std::string *result) {
     55   base::DictionaryValue item;
     56 
     57   if (load_start_time.is_null()) {
     58     item.Set("load_start_ms", base::Value::CreateNullValue());
     59   } else {
     60     item.SetDouble("load_start_ms", load_start_time.ToInternalValue() / 1000);
     61   }
     62   if (load_start_time.is_null() || load_stop_time.is_null()) {
     63     item.Set("load_duration_ms", base::Value::CreateNullValue());
     64   } else {
     65     item.SetDouble("load_duration_ms",
     66         (load_stop_time - load_start_time).InMillisecondsF());
     67   }
     68   base::JSONWriter::Write(&item, result);
     69 }
     70 
     71 }  // namespace
     72 
     73 // static
     74 gin::WrapperInfo StatsCollectionController::kWrapperInfo = {
     75     gin::kEmbedderNativeGin
     76 };
     77 
     78 // static
     79 void StatsCollectionController::Install(blink::WebFrame* frame) {
     80   v8::Isolate* isolate = blink::mainThreadIsolate();
     81   v8::HandleScope handle_scope(isolate);
     82   v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
     83   if (context.IsEmpty())
     84     return;
     85 
     86   v8::Context::Scope context_scope(context);
     87 
     88   gin::Handle<StatsCollectionController> controller =
     89       gin::CreateHandle(isolate, new StatsCollectionController());
     90   if (controller.IsEmpty())
     91     return;
     92   v8::Handle<v8::Object> global = context->Global();
     93   global->Set(gin::StringToV8(isolate, "statsCollectionController"),
     94               controller.ToV8());
     95 }
     96 
     97 StatsCollectionController::StatsCollectionController() {}
     98 
     99 StatsCollectionController::~StatsCollectionController() {}
    100 
    101 gin::ObjectTemplateBuilder StatsCollectionController::GetObjectTemplateBuilder(
    102     v8::Isolate* isolate) {
    103   return gin::Wrappable<StatsCollectionController>::GetObjectTemplateBuilder(
    104       isolate)
    105       .SetMethod("getHistogram", &StatsCollectionController::GetHistogram)
    106       .SetMethod("getBrowserHistogram",
    107                  &StatsCollectionController::GetBrowserHistogram)
    108       .SetMethod("tabLoadTiming", &StatsCollectionController::GetTabLoadTiming);
    109 }
    110 
    111 std::string StatsCollectionController::GetHistogram(
    112     const std::string& histogram_name) {
    113   base::HistogramBase* histogram =
    114       base::StatisticsRecorder::FindHistogram(histogram_name);
    115   std::string output;
    116   if (!histogram) {
    117     output = "{}";
    118   } else {
    119     histogram->WriteJSON(&output);
    120   }
    121   return output;
    122 }
    123 
    124 std::string StatsCollectionController::GetBrowserHistogram(
    125     const std::string& histogram_name) {
    126   RenderViewImpl *render_view_impl = NULL;
    127   if (!CurrentRenderViewImpl(&render_view_impl)) {
    128     NOTREACHED();
    129     return std::string();
    130   }
    131 
    132   std::string histogram_json;
    133   render_view_impl->Send(new ChildProcessHostMsg_GetBrowserHistogram(
    134       histogram_name, &histogram_json));
    135   return histogram_json;
    136 }
    137 
    138 std::string StatsCollectionController::GetTabLoadTiming() {
    139   RenderViewImpl *render_view_impl = NULL;
    140   if (!CurrentRenderViewImpl(&render_view_impl)) {
    141     NOTREACHED();
    142     return std::string();
    143   }
    144 
    145   StatsCollectionObserver* observer =
    146       render_view_impl->GetStatsCollectionObserver();
    147   if (!observer) {
    148     NOTREACHED();
    149     return std::string();
    150   }
    151 
    152   std::string tab_timing_json;
    153   ConvertLoadTimeToJSON(
    154       observer->load_start_time(), observer->load_stop_time(),
    155       &tab_timing_json);
    156   return tab_timing_json;
    157 }
    158 
    159 }  // namespace content
    160