summaryrefslogtreecommitdiff
path: root/chromium/components/performance_manager/decorators/page_load_tracker_decorator.cc
blob: ece6df68140e029aed09267a577d1d9b66161845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/decorators/page_load_tracker_decorator.h"

#include <algorithm>

#include "base/compiler_specific.h"
#include "base/notreached.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/graph_impl.h"
#include "components/performance_manager/graph/node_attached_data_impl.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/public/graph/node_data_describer_registry.h"

namespace performance_manager {

// Provides PageLoadTracker machinery access to some internals of a
// PageNodeImpl.
class PageLoadTrackerAccess {
 public:
  static std::unique_ptr<NodeAttachedData>* GetUniquePtrStorage(
      PageNodeImpl* page_node) {
    return &page_node->GetPageLoadTrackerData(
        base::PassKey<PageLoadTrackerAccess>());
  }
};

namespace {

using LoadIdleState = PageLoadTrackerDecorator::Data::LoadIdleState;

class DataImpl : public PageLoadTrackerDecorator::Data,
                 public NodeAttachedDataImpl<DataImpl> {
 public:
  struct Traits : public NodeAttachedDataOwnedByNodeType<PageNodeImpl> {};

  explicit DataImpl(const PageNodeImpl* page_node) {}
  ~DataImpl() override = default;

  static std::unique_ptr<NodeAttachedData>* GetUniquePtrStorage(
      PageNodeImpl* page_node) {
    return PageLoadTrackerAccess::GetUniquePtrStorage(page_node);
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(DataImpl);
};

// static
const char* ToString(LoadIdleState state) {
  switch (state) {
    case LoadIdleState::kLoadingWaitingForResponse:
      return "kLoadingWaitingForResponse";
    case LoadIdleState::kLoadingWaitingForResponseTimedOut:
      return "kLoadingWaitingForResponseTimedOut";
    case LoadIdleState::kLoadingDidReceiveResponse:
      return "kLoadingDidReceiveResponse";
    case LoadIdleState::kLoadedNotIdling:
      return "kLoadedNotIdling";
    case LoadIdleState::kLoadedAndIdling:
      return "kLoadedAndIdling";
    case LoadIdleState::kLoadedAndIdle:
      return "kLoadedAndIdle";
  }
}

}  // namespace

// static
constexpr base::TimeDelta PageLoadTrackerDecorator::kWaitingForResponseTimeout;
constexpr base::TimeDelta PageLoadTrackerDecorator::kLoadedAndIdlingTimeout;
constexpr base::TimeDelta PageLoadTrackerDecorator::kWaitingForIdleTimeout;

PageLoadTrackerDecorator::PageLoadTrackerDecorator() {
  // Ensure the timeouts make sense relative to each other.
  static_assert(kWaitingForIdleTimeout > kLoadedAndIdlingTimeout,
                "timeouts must be well ordered");
}

PageLoadTrackerDecorator::~PageLoadTrackerDecorator() = default;

void PageLoadTrackerDecorator::OnNetworkAlmostIdleChanged(
    const FrameNode* frame_node) {
  UpdateLoadIdleStateFrame(FrameNodeImpl::FromNode(frame_node));
}

void PageLoadTrackerDecorator::OnPassedToGraph(Graph* graph) {
  RegisterObservers(graph);
  graph->GetNodeDataDescriberRegistry()->RegisterDescriber(this,
                                                           "PageLoadTrackerDecorator");
}

void PageLoadTrackerDecorator::OnTakenFromGraph(Graph* graph) {
  graph->GetNodeDataDescriberRegistry()->UnregisterDescriber(this);
  UnregisterObservers(graph);
}

base::Value PageLoadTrackerDecorator::DescribePageNodeData(
    const PageNode* page_node) const {
  auto* data = DataImpl::Get(PageNodeImpl::FromNode(page_node));
  if (data == nullptr)
    return base::Value();

  base::Value ret(base::Value::Type::DICTIONARY);
  ret.SetStringKey("load_idle_state", ToString(data->load_idle_state()));
  ret.SetBoolKey("is_loading", data->is_loading_);
  ret.SetBoolKey("received_response", data->received_response_);

  return ret;
}

void PageLoadTrackerDecorator::OnMainThreadTaskLoadIsLow(
    const ProcessNode* process_node) {
  UpdateLoadIdleStateProcess(ProcessNodeImpl::FromNode(process_node));
}

// static
void PageLoadTrackerDecorator::DidStartLoading(PageNodeImpl* page_node) {
  auto* data = DataImpl::GetOrCreate(page_node);

  // Typically, |data| is a newly created DataImpl. However, if a load starts
  // before the page reaches an idle state following the previous load, |data|
  // may indicate that the page is |kLoadedNotIdling| or |kLoadedAndIdling|.
  // In all cases, restart the state machine at |kLoadingWaitingForResponse|
  // and clear the |loading_started_| timestamp.
  DCHECK_NE(data->load_idle_state(),
            LoadIdleState::kLoadingWaitingForResponseTimedOut);
  DCHECK_NE(data->load_idle_state(), LoadIdleState::kLoadingDidReceiveResponse);
  DCHECK_NE(data->load_idle_state(), LoadIdleState::kLoadedAndIdle);
  DCHECK(data);
  DCHECK(!data->is_loading_);
  DCHECK(!data->received_response_);
  data->is_loading_ = true;
  data->SetLoadIdleState(page_node, LoadIdleState::kLoadingWaitingForResponse);
  data->loading_started_ = base::TimeTicks();
  UpdateLoadIdleStatePage(page_node);
}

// static
void PageLoadTrackerDecorator::DidReceiveResponse(PageNodeImpl* page_node) {
  auto* data = DataImpl::Get(page_node);

  DCHECK(data);
  DCHECK(data->is_loading_);
  DCHECK(!data->received_response_);
  data->received_response_ = true;

  UpdateLoadIdleStatePage(page_node);
}

// static
void PageLoadTrackerDecorator::DidStopLoading(PageNodeImpl* page_node) {
  auto* data = DataImpl::Get(page_node);

  DCHECK(data);
  DCHECK(data->is_loading_);
  data->is_loading_ = false;
  data->received_response_ = false;

  UpdateLoadIdleStatePage(page_node);
}

void PageLoadTrackerDecorator::RegisterObservers(Graph* graph) {
  // This observer presumes that it's been added before any nodes exist in the
  // graph.
  // TODO(chrisha): Add graph introspection functions to Graph.
  DCHECK(GraphImpl::FromGraph(graph)->nodes().empty());
  graph->AddFrameNodeObserver(this);
  graph->AddProcessNodeObserver(this);
}

void PageLoadTrackerDecorator::UnregisterObservers(Graph* graph) {
  graph->RemoveFrameNodeObserver(this);
  graph->RemoveProcessNodeObserver(this);
}

void PageLoadTrackerDecorator::UpdateLoadIdleStateFrame(
    FrameNodeImpl* frame_node) {
  // Only main frames are relevant in the load idle state.
  if (!frame_node->IsMainFrame())
    return;

  // Update the load idle state of the page associated with this frame.
  auto* page_node = frame_node->page_node();
  if (!page_node)
    return;
  UpdateLoadIdleStatePage(page_node);
}

// static
void PageLoadTrackerDecorator::UpdateLoadIdleStatePage(
    PageNodeImpl* page_node) {
  // Once the cycle is complete state transitions are no longer tracked for this
  // page. When this occurs the backing data store is deleted.
  auto* data = DataImpl::Get(page_node);
  if (data == nullptr)
    return;

  // Cancel any ongoing timers. A new timer will be set if necessary.
  data->timer_.Stop();
  const base::TimeTicks now = base::TimeTicks::Now();

  // If this is a new load, set the start time.
  if (data->loading_started_.is_null()) {
    DCHECK_EQ(data->load_idle_state(),
              LoadIdleState::kLoadingWaitingForResponse);
    data->loading_started_ = now;
  }

  // Determine if the overall timeout has fired.
  if ((data->load_idle_state() == LoadIdleState::kLoadedNotIdling ||
       data->load_idle_state() == LoadIdleState::kLoadedAndIdling) &&
      (now - data->loading_stopped_) >= kWaitingForIdleTimeout) {
    TransitionToLoadedAndIdle(page_node);
    return;
  }

  // Otherwise do normal state transitions.
  switch (data->load_idle_state()) {
    case LoadIdleState::kLoadingWaitingForResponse: {
      if (now - data->loading_started_ >= kWaitingForResponseTimeout) {
        data->SetLoadIdleState(
            page_node, LoadIdleState::kLoadingWaitingForResponseTimedOut);
      }

      FALLTHROUGH;
    }

    case LoadIdleState::kLoadingWaitingForResponseTimedOut: {
      if (data->received_response_) {
        data->SetLoadIdleState(page_node,
                               LoadIdleState::kLoadingDidReceiveResponse);
        return;
      }

      if (!data->is_loading_) {
        // Transition to kLoadedAndIdle when load stops without receiving a
        // response.
        TransitionToLoadedAndIdle(page_node);
        return;
      }

      // Schedule a state update to transition to
      // |kLoadingWaitingForResponseTimedOut| when the page has been waiting for
      // a response for too long.
      if (data->load_idle_state() ==
          LoadIdleState::kLoadingWaitingForResponse) {
        ScheduleDelayedUpdateLoadIdleStatePage(
            page_node, now,
            data->loading_started_ + kWaitingForResponseTimeout);
      }

      return;
    }

    case LoadIdleState::kLoadingDidReceiveResponse: {
      if (data->is_loading_) {
        // DidStopLoading() was not invoked yet.
        return;
      }
      // DidStartLoading() -> DidReceiveResponse() -> DidStopLoading() were all
      // invoked. Wait for the page to become idle.
      data->SetLoadIdleState(page_node, LoadIdleState::kLoadedNotIdling);
      data->loading_stopped_ = now;
      // Let the kLoadedNotIdling state transition evaluate, allowing an
      // immediate transition to kLoadedAndIdling if the page is already idling.
      FALLTHROUGH;
    }

    case LoadIdleState::kLoadedNotIdling: {
      if (!IsIdling(page_node)) {
        // Schedule a state update to transition to |kLoadedAndIdle| when the
        // page has been loaded but not idling for too long.
        ScheduleDelayedUpdateLoadIdleStatePage(
            page_node, now, data->loading_stopped_ + kWaitingForIdleTimeout);
        return;
      }

      data->SetLoadIdleState(page_node, LoadIdleState::kLoadedAndIdling);
      data->idling_started_ = now;
      FALLTHROUGH;
    }

    case LoadIdleState::kLoadedAndIdling: {
      if (!IsIdling(page_node)) {
        // If the page is not still idling then transition back a state.
        data->SetLoadIdleState(page_node, LoadIdleState::kLoadedNotIdling);
        // Schedule a state update to transition to |kLoadedAndIdle| when the
        // page has been loaded but not idling for too long.
        ScheduleDelayedUpdateLoadIdleStatePage(
            page_node, now, data->loading_stopped_ + kWaitingForIdleTimeout);
        return;
      }

      if (now - data->idling_started_ >= kLoadedAndIdlingTimeout) {
        // Idling has been happening long enough so make the last state
        // transition.
        TransitionToLoadedAndIdle(page_node);
        return;
      }

      // Schedule a state update to transition to |kLoadedAndIdle| when the page
      // has been idling long enough to be considered "loaded and idle".
      ScheduleDelayedUpdateLoadIdleStatePage(
          page_node, now, data->idling_started_ + kLoadedAndIdlingTimeout);
      return;
    }

    // This should never occur.
    case LoadIdleState::kLoadedAndIdle:
      NOTREACHED();
  }

  // All paths of the switch statement return.
  NOTREACHED();
}

// static
void PageLoadTrackerDecorator::ScheduleDelayedUpdateLoadIdleStatePage(
    PageNodeImpl* page_node,
    base::TimeTicks now,
    base::TimeTicks delayed_run_time) {
  auto* data = DataImpl::Get(page_node);
  DCHECK(data);
  DCHECK_GE(delayed_run_time, now);

  // |timer| is owned by |page_node| indirectly through |data|. Because of that,
  // the |timer| will be canceled if |page_node| is deleted, making the use of
  // Unretained() safe.
  data->timer_.Start(
      FROM_HERE, delayed_run_time - now,
      base::BindRepeating(&PageLoadTrackerDecorator::UpdateLoadIdleStatePage,
                          base::Unretained(page_node)));
}

void PageLoadTrackerDecorator::UpdateLoadIdleStateProcess(
    ProcessNodeImpl* process_node) {
  for (auto* frame_node : process_node->frame_nodes())
    UpdateLoadIdleStateFrame(frame_node);
}

// static
void PageLoadTrackerDecorator::TransitionToLoadedAndIdle(
    PageNodeImpl* page_node) {
  auto* data = DataImpl::Get(page_node);
  DCHECK(data);
  data->SetLoadIdleState(page_node, LoadIdleState::kLoadedAndIdle);
  // Destroy the metadata as there are no more transitions possible. The
  // machinery will start up again if a navigation occurs.
  DataImpl::Destroy(page_node);
}

// static
bool PageLoadTrackerDecorator::IsIdling(const PageNodeImpl* page_node) {
  // Get the frame node for the main frame associated with this page.
  const FrameNodeImpl* main_frame_node = page_node->GetMainFrameNodeImpl();
  if (!main_frame_node)
    return false;

  // Get the process node associated with this main frame.
  const auto* process_node = main_frame_node->process_node();
  if (!process_node)
    return false;

  // Note that it's possible for one misbehaving frame hosted in the same
  // process as this page's main frame to keep the main thread task low high.
  // In this case the IsIdling signal will be delayed, despite the task load
  // associated with this page's main frame actually being low. In the case
  // of session restore this is mitigated by having a timeout while waiting for
  // this signal.
  return main_frame_node->network_almost_idle() &&
         process_node->main_thread_task_load_is_low();
}

// static
PageLoadTrackerDecorator::Data*
PageLoadTrackerDecorator::Data::GetOrCreateForTesting(PageNodeImpl* page_node) {
  return DataImpl::GetOrCreate(page_node);
}

// static
PageLoadTrackerDecorator::Data* PageLoadTrackerDecorator::Data::GetForTesting(
    PageNodeImpl* page_node) {
  return DataImpl::Get(page_node);
}

// static
bool PageLoadTrackerDecorator::Data::DestroyForTesting(
    PageNodeImpl* page_node) {
  return DataImpl::Destroy(page_node);
}

void PageLoadTrackerDecorator::Data::SetLoadIdleState(
    PageNodeImpl* page_node,
    LoadIdleState load_idle_state) {
  load_idle_state_ = load_idle_state;

  switch (load_idle_state_) {
    case LoadIdleState::kLoadingWaitingForResponse:
    case LoadIdleState::kLoadingDidReceiveResponse: {
      page_node->SetLoadingState(PageNode::LoadingState::kLoading);
      break;
    }
    case LoadIdleState::kLoadingWaitingForResponseTimedOut: {
      page_node->SetLoadingState(PageNode::LoadingState::kLoadingTimedOut);
      break;
    }
    case LoadIdleState::kLoadedNotIdling:
    case LoadIdleState::kLoadedAndIdling: {
      page_node->SetLoadingState(PageNode::LoadingState::kLoadedBusy);
      break;
    }
    case LoadIdleState::kLoadedAndIdle: {
      page_node->SetLoadingState(PageNode::LoadingState::kLoadedIdle);
      break;
    }
  }
}

}  // namespace performance_manager