summaryrefslogtreecommitdiff
path: root/chromium/headless/app/shell_navigation_request.cc
blob: 808df26b5f932922f13482b5d1931aa81ddc2336 (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
// Copyright 2017 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 "headless/app/shell_navigation_request.h"

#include <memory>

#include "content/public/browser/browser_thread.h"
#include "headless/app/headless_shell.h"

namespace headless {

ShellNavigationRequest::ShellNavigationRequest(
    base::WeakPtr<HeadlessShell> headless_shell,
    const std::string& interception_id)
    : headless_shell_(
          std::make_unique<base::WeakPtr<HeadlessShell>>(headless_shell)),
      interception_id_(interception_id) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}

ShellNavigationRequest::~ShellNavigationRequest() = default;

void ShellNavigationRequest::StartProcessing(base::Closure done_callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  // The devtools bindings can only be called on the UI thread.
  content::BrowserThread::PostTask(
      content::BrowserThread::UI, FROM_HERE,
      base::BindOnce(&ShellNavigationRequest::StartProcessingOnUiThread,
                     std::move(headless_shell_), interception_id_,
                     std::move(done_callback)));
}

// static
void ShellNavigationRequest::StartProcessingOnUiThread(
    std::unique_ptr<base::WeakPtr<HeadlessShell>> headless_shell,
    std::string interception_id,
    base::Closure done_callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!headless_shell)
    return;

  // Allow the navigation to proceed.
  (*headless_shell)
      ->devtools_client()
      ->GetNetwork()
      ->GetExperimental()
      ->ContinueInterceptedRequest(
          network::ContinueInterceptedRequestParams::Builder()
              .SetInterceptionId(interception_id)
              .Build(),
          base::BindOnce(
              &ShellNavigationRequest::ContinueInterceptedRequestResult,
              std::move(done_callback)));
}

// static
void ShellNavigationRequest::ContinueInterceptedRequestResult(
    base::Closure done_callback,
    std::unique_ptr<network::ContinueInterceptedRequestResult>) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // The |done_callback| must be fired on the IO thread.
  content::BrowserThread::PostTask(
      content::BrowserThread::IO, FROM_HERE,
      base::BindOnce(
          &ShellNavigationRequest::ContinueInterceptedRequestResultOnIoThread,
          std::move(done_callback)));
}

// static
void ShellNavigationRequest::ContinueInterceptedRequestResultOnIoThread(
    base::Closure done_callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
  done_callback.Run();
}

}  // namespace headless