[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" |
jdoerrie | e067999a | 2017-04-07 06:39:00 | [diff] [blame] | 8 | #include "base/memory/ptr_util.h" |
Tarun Bansal | 89684a7 | 2017-11-29 14:44:12 | [diff] [blame^] | 9 | #include "base/metrics/histogram_base.h" |
asvitkine | 8d51e9d | 2016-09-02 23:55:43 | [diff] [blame] | 10 | #include "base/metrics/histogram_macros.h" |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 11 | #include "base/metrics/statistics_recorder.h" |
[email protected] | 21aa9968 | 2013-06-11 07:17:01 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
Nicholas Verne | 2ba2b8fe | 2017-10-20 04:00:31 | [diff] [blame] | 13 | #include "content/common/renderer_host.mojom.h" |
| 14 | #include "content/public/common/service_names.mojom.h" |
| 15 | #include "content/renderer/render_thread_impl.h" |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 16 | #include "content/renderer/render_view_impl.h" |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 17 | #include "gin/handle.h" |
| 18 | #include "gin/object_template_builder.h" |
Nicholas Verne | 2ba2b8fe | 2017-10-20 04:00:31 | [diff] [blame] | 19 | #include "services/service_manager/public/cpp/connector.h" |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 20 | #include "third_party/WebKit/public/web/WebKit.h" |
[email protected] | d357694 | 2014-04-10 18:45:37 | [diff] [blame] | 21 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 22 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 23 | |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 24 | namespace content { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | bool CurrentRenderViewImpl(RenderViewImpl** out) { |
[email protected] | d357694 | 2014-04-10 18:45:37 | [diff] [blame] | 29 | blink::WebLocalFrame* web_frame = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 30 | blink::WebLocalFrame::FrameForCurrentContext(); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 31 | if (!web_frame) |
| 32 | return false; |
| 33 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 34 | blink::WebView* web_view = web_frame->View(); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 35 | if (!web_view) |
| 36 | return false; |
| 37 | |
| 38 | RenderViewImpl* render_view_impl = |
| 39 | RenderViewImpl::FromWebView(web_view); |
| 40 | if (!render_view_impl) |
| 41 | return false; |
| 42 | |
| 43 | *out = render_view_impl; |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | // Encodes a WebContentsLoadTime as JSON. |
| 48 | // Input: |
| 49 | // - |load_start_time| - time at which page load started. |
| 50 | // - |load_stop_time| - time at which page load stopped. |
| 51 | // - |result| - returned JSON. |
| 52 | // Example return value: |
| 53 | // {'load_start_ms': 1, 'load_duration_ms': 2.5} |
| 54 | // either value may be null if a web contents hasn't fully loaded. |
azarchs | 8cc4ec7 | 2014-09-16 19:23:03 | [diff] [blame] | 55 | // load_start_ms is represented as milliseconds since the unix epoch. |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 56 | void ConvertLoadTimeToJSON( |
[email protected] | 538dde87 | 2013-11-19 16:44:17 | [diff] [blame] | 57 | const base::Time& load_start_time, |
| 58 | const base::Time& load_stop_time, |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 59 | std::string *result) { |
[email protected] | 71677525 | 2013-06-14 17:58:00 | [diff] [blame] | 60 | base::DictionaryValue item; |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 61 | |
| 62 | if (load_start_time.is_null()) { |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 63 | item.Set("load_start_ms", std::make_unique<base::Value>()); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 64 | } else { |
azarchs | 8cc4ec7 | 2014-09-16 19:23:03 | [diff] [blame] | 65 | item.SetDouble("load_start_ms", (load_start_time - base::Time::UnixEpoch()) |
| 66 | .InMillisecondsF()); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 67 | } |
[email protected] | c81e3fd | 2013-08-05 16:30:22 | [diff] [blame] | 68 | if (load_start_time.is_null() || load_stop_time.is_null()) { |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 69 | item.Set("load_duration_ms", std::make_unique<base::Value>()); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 70 | } else { |
| 71 | item.SetDouble("load_duration_ms", |
[email protected] | 538dde87 | 2013-11-19 16:44:17 | [diff] [blame] | 72 | (load_stop_time - load_start_time).InMillisecondsF()); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 73 | } |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 74 | base::JSONWriter::Write(item, result); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | } // namespace |
| 78 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 79 | // static |
| 80 | gin::WrapperInfo StatsCollectionController::kWrapperInfo = { |
| 81 | gin::kEmbedderNativeGin |
| 82 | }; |
| 83 | |
| 84 | // static |
lukasza | df18ba76 | 2017-06-09 22:24:30 | [diff] [blame] | 85 | void StatsCollectionController::Install(blink::WebLocalFrame* frame) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 86 | v8::Isolate* isolate = blink::MainThreadIsolate(); |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 87 | v8::HandleScope handle_scope(isolate); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 88 | v8::Local<v8::Context> context = frame->MainWorldScriptContext(); |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 89 | if (context.IsEmpty()) |
| 90 | return; |
| 91 | |
| 92 | v8::Context::Scope context_scope(context); |
| 93 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 94 | gin::Handle<StatsCollectionController> controller = |
| 95 | gin::CreateHandle(isolate, new StatsCollectionController()); |
[email protected] | ad4d203 | 2014-04-28 13:50:59 | [diff] [blame] | 96 | if (controller.IsEmpty()) |
| 97 | return; |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 98 | v8::Local<v8::Object> global = context->Global(); |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 99 | global->Set(gin::StringToV8(isolate, "statsCollectionController"), |
| 100 | controller.ToV8()); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 103 | StatsCollectionController::StatsCollectionController() {} |
| 104 | |
| 105 | StatsCollectionController::~StatsCollectionController() {} |
| 106 | |
[email protected] | 13c977e6 | 2013-12-19 00:52:44 | [diff] [blame] | 107 | gin::ObjectTemplateBuilder StatsCollectionController::GetObjectTemplateBuilder( |
| 108 | v8::Isolate* isolate) { |
| 109 | return gin::Wrappable<StatsCollectionController>::GetObjectTemplateBuilder( |
| 110 | isolate) |
| 111 | .SetMethod("getHistogram", &StatsCollectionController::GetHistogram) |
| 112 | .SetMethod("getBrowserHistogram", |
| 113 | &StatsCollectionController::GetBrowserHistogram) |
| 114 | .SetMethod("tabLoadTiming", &StatsCollectionController::GetTabLoadTiming); |
| 115 | } |
| 116 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 117 | std::string StatsCollectionController::GetHistogram( |
| 118 | const std::string& histogram_name) { |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 119 | base::HistogramBase* histogram = |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 120 | base::StatisticsRecorder::FindHistogram(histogram_name); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 121 | std::string output; |
| 122 | if (!histogram) { |
| 123 | output = "{}"; |
| 124 | } else { |
Tarun Bansal | 89684a7 | 2017-11-29 14:44:12 | [diff] [blame^] | 125 | histogram->WriteJSON(&output, base::JSON_VERBOSITY_LEVEL_FULL); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 126 | } |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 127 | return output; |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 130 | std::string StatsCollectionController::GetBrowserHistogram( |
| 131 | const std::string& histogram_name) { |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 132 | std::string histogram_json; |
Nicholas Verne | 2ba2b8fe | 2017-10-20 04:00:31 | [diff] [blame] | 133 | RenderThreadImpl::current()->GetRendererHost()->GetBrowserHistogram( |
| 134 | histogram_name, &histogram_json); |
| 135 | |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 136 | return histogram_json; |
| 137 | } |
| 138 | |
| 139 | std::string StatsCollectionController::GetTabLoadTiming() { |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 140 | RenderViewImpl* render_view_impl = nullptr; |
Nicholas Verne | 2ba2b8fe | 2017-10-20 04:00:31 | [diff] [blame] | 141 | bool result = CurrentRenderViewImpl(&render_view_impl); |
| 142 | DCHECK(result); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 143 | |
| 144 | StatsCollectionObserver* observer = |
| 145 | render_view_impl->GetStatsCollectionObserver(); |
Nicholas Verne | 2ba2b8fe | 2017-10-20 04:00:31 | [diff] [blame] | 146 | DCHECK(observer); |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 147 | |
| 148 | std::string tab_timing_json; |
| 149 | ConvertLoadTimeToJSON( |
| 150 | observer->load_start_time(), observer->load_stop_time(), |
| 151 | &tab_timing_json); |
[email protected] | a79712f | 2013-12-13 21:33:25 | [diff] [blame] | 152 | return tab_timing_json; |
[email protected] | 27c521a | 2013-05-29 20:44:32 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace content |