DevTools: emit "init" lifecycle event when document gets opened

Change-Id: I558a3d897a56f3fb05e14a1525c8330199d023ec
Reviewed-on: https://chromium-review.googlesource.com/c/1338947
Reviewed-by: Pavel Feldman <pfeldman@chromium.org>
Commit-Queue: Andrey Lushnikov <lushnikov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608658}
diff --git a/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/page/page-lifecycleEvents-document-open-expected.txt b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/page/page-lifecycleEvents-document-open-expected.txt
new file mode 100644
index 0000000..a6eea5c
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/page/page-lifecycleEvents-document-open-expected.txt
@@ -0,0 +1,9 @@
+Tests that Page.lifecycleEvent is issued for important events.
+[
+    [0] : init
+    [1] : load
+    [2] : DOMContentLoaded
+    [3] : networkAlmostIdle
+    [4] : networkIdle
+]
+
diff --git a/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/page/page-lifecycleEvents-document-open.js b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/page/page-lifecycleEvents-document-open.js
new file mode 100644
index 0000000..1ec1c5c
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/page/page-lifecycleEvents-document-open.js
@@ -0,0 +1,26 @@
+(async function(testRunner) {
+  var {page, session, dp} = await testRunner.startBlank(
+      `Tests that Page.lifecycleEvent is issued for important events.`);
+
+  await dp.Page.enable();
+  await dp.Page.setLifecycleEventsEnabled({ enabled: true });
+
+  var events = [];
+  dp.Page.onLifecycleEvent(event => {
+    // Filter out firstMeaningfulPaint and friends.
+    if (event.params.name.startsWith('first'))
+      return;
+    events.push(event);
+    if (event.params.name === 'networkIdle') {
+      var names = events.map(event => event.params.name);
+      testRunner.log(names);
+      testRunner.completeTest();
+    }
+  });
+
+  var response = await session.evaluate(`
+    document.open();
+    document.write('Hello, world');
+    document.close();
+  `);
+})
diff --git a/third_party/blink/renderer/core/loader/frame_loader.cc b/third_party/blink/renderer/core/loader/frame_loader.cc
index d984607..04428e9b 100644
--- a/third_party/blink/renderer/core/loader/frame_loader.cc
+++ b/third_party/blink/renderer/core/loader/frame_loader.cc
@@ -349,6 +349,8 @@
 }
 
 void FrameLoader::DidExplicitOpen() {
+  probe::lifecycleEvent(frame_, GetDocumentLoader(), "init",
+                        CurrentTimeTicksInSeconds());
   // Calling document.open counts as committing the first real document load.
   if (!state_machine_.CommittedFirstRealDocumentLoad())
     state_machine_.AdvanceTo(FrameLoaderStateMachine::kCommittedFirstRealLoad);