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