[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 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 | |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 7 | #include "base/json/json_writer.h" |
| 8 | #include "base/metrics/histogram.h" |
| 9 | #include "base/metrics/statistics_recorder.h" |
[email protected] | 21aa9968 | 2013-06-11 07:17:01 | [diff] [blame] | 10 | #include "base/strings/string_util.h" |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 11 | #include "content/common/child_process_messages.h" |
| 12 | #include "content/renderer/render_view_impl.h" |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 13 | #include "gin/handle.h" |
| 14 | #include "gin/object_template_builder.h" |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 15 | #include "third_party/WebKit/public/web/WebKit.h" |
[email protected] | d357694 | 2014-04-10 18:45:37 | [diff] [blame] | 16 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 17 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 18 | |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 19 | namespace content { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | bool CurrentRenderViewImpl(RenderViewImpl** out) { |
[email protected] | d357694 | 2014-04-10 18:45:37 | [diff] [blame] | 24 | blink::WebLocalFrame* web_frame = |
| 25 | blink::WebLocalFrame::frameForCurrentContext(); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 26 | if (!web_frame) |
| 27 | return false; |
| 28 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 29 | blink::WebView* web_view = web_frame->view(); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 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. |
azarchs | 8cc4ec7 | 2014-09-16 19:23:03 | [diff] [blame] | 50 | // load_start_ms is represented as milliseconds since the unix epoch. |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 51 | void ConvertLoadTimeToJSON( |
[email protected] | 538dde87 | 2013-11-19 16:44:17 | [diff] [blame] | 52 | const base::Time& load_start_time, |
| 53 | const base::Time& load_stop_time, |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 54 | std::string *result) { |
[email protected] | 71677525 | 2013-06-14 17:58:00 | [diff] [blame] | 55 | base::DictionaryValue item; |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 56 | |
| 57 | if (load_start_time.is_null()) { |
[email protected] | 538dde87 | 2013-11-19 16:44:17 | [diff] [blame] | 58 | item.Set("load_start_ms", base::Value::CreateNullValue()); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 59 | } else { |
azarchs | 8cc4ec7 | 2014-09-16 19:23:03 | [diff] [blame] | 60 | item.SetDouble("load_start_ms", (load_start_time - base::Time::UnixEpoch()) |
| 61 | .InMillisecondsF()); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 62 | } |
[email protected] | c81e3fd | 2013-08-05 16:30:22 | [diff] [blame] | 63 | if (load_start_time.is_null() || load_stop_time.is_null()) { |
[email protected] | 71677525 | 2013-06-14 17:58:00 | [diff] [blame] | 64 | item.Set("load_duration_ms", base::Value::CreateNullValue()); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 65 | } else { |
| 66 | item.SetDouble("load_duration_ms", |
[email protected] | 538dde87 | 2013-11-19 16:44:17 | [diff] [blame] | 67 | (load_stop_time - load_start_time).InMillisecondsF()); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 68 | } |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame^] | 69 | base::JSONWriter::Write(item, result); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | } // namespace |
| 73 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 74 | // static |
| 75 | gin::WrapperInfo StatsCollectionController::kWrapperInfo = { |
| 76 | gin::kEmbedderNativeGin |
| 77 | }; |
| 78 | |
| 79 | // static |
| 80 | void StatsCollectionController::Install(blink::WebFrame* frame) { |
| 81 | v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 82 | v8::HandleScope handle_scope(isolate); |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 83 | v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 84 | if (context.IsEmpty()) |
| 85 | return; |
| 86 | |
| 87 | v8::Context::Scope context_scope(context); |
| 88 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 89 | gin::Handle<StatsCollectionController> controller = |
| 90 | gin::CreateHandle(isolate, new StatsCollectionController()); |
[email protected] | ad4d203 | 2014-04-28 13:50:59 | [diff] [blame] | 91 | if (controller.IsEmpty()) |
| 92 | return; |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 93 | v8::Local<v8::Object> global = context->Global(); |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 94 | global->Set(gin::StringToV8(isolate, "statsCollectionController"), |
| 95 | controller.ToV8()); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 98 | StatsCollectionController::StatsCollectionController() {} |
| 99 | |
| 100 | StatsCollectionController::~StatsCollectionController() {} |
| 101 | |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 102 | gin::ObjectTemplateBuilder StatsCollectionController::GetObjectTemplateBuilder( |
| 103 | v8::Isolate* isolate) { |
| 104 | return gin::Wrappable<StatsCollectionController>::GetObjectTemplateBuilder( |
| 105 | isolate) |
| 106 | .SetMethod("getHistogram", &StatsCollectionController::GetHistogram) |
| 107 | .SetMethod("getBrowserHistogram", |
| 108 | &StatsCollectionController::GetBrowserHistogram) |
| 109 | .SetMethod("tabLoadTiming", &StatsCollectionController::GetTabLoadTiming); |
| 110 | } |
| 111 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 112 | std::string StatsCollectionController::GetHistogram( |
| 113 | const std::string& histogram_name) { |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 114 | base::HistogramBase* histogram = |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 115 | base::StatisticsRecorder::FindHistogram(histogram_name); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 116 | std::string output; |
| 117 | if (!histogram) { |
| 118 | output = "{}"; |
| 119 | } else { |
| 120 | histogram->WriteJSON(&output); |
| 121 | } |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 122 | return output; |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 123 | } |
| 124 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 125 | std::string StatsCollectionController::GetBrowserHistogram( |
| 126 | const std::string& histogram_name) { |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 127 | RenderViewImpl *render_view_impl = NULL; |
| 128 | if (!CurrentRenderViewImpl(&render_view_impl)) { |
| 129 | NOTREACHED(); |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 130 | return std::string(); |
| 131 | } |
| 132 | |
| 133 | std::string histogram_json; |
| 134 | render_view_impl->Send(new ChildProcessHostMsg_GetBrowserHistogram( |
| 135 | histogram_name, &histogram_json)); |
| 136 | return histogram_json; |
| 137 | } |
| 138 | |
| 139 | std::string StatsCollectionController::GetTabLoadTiming() { |
| 140 | RenderViewImpl *render_view_impl = NULL; |
| 141 | if (!CurrentRenderViewImpl(&render_view_impl)) { |
| 142 | NOTREACHED(); |
| 143 | return std::string(); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | StatsCollectionObserver* observer = |
| 147 | render_view_impl->GetStatsCollectionObserver(); |
| 148 | if (!observer) { |
| 149 | NOTREACHED(); |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 150 | return std::string(); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | std::string tab_timing_json; |
| 154 | ConvertLoadTimeToJSON( |
| 155 | observer->load_start_time(), observer->load_stop_time(), |
| 156 | &tab_timing_json); |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 157 | return tab_timing_json; |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | } // namespace content |