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

headless / lib / browser / headless_browser_main_parts.cc [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "headless/lib/browser/headless_browser_main_parts.h"

#include <memory.h>
#include <stdio.h>

#include "base/run_loop.h"
#include "build/build_config.h"
#include "components/headless/clipboard/headless_clipboard.h"
#include "components/headless/select_file_dialog/headless_select_file_dialog.h"
#include "content/public/common/result_codes.h"
#include "headless/lib/browser/headless_browser_impl.h"
#include "headless/lib/browser/headless_devtools.h"
#include "headless/lib/browser/headless_screen.h"
#include "headless/lib/browser/headless_screen_orientation_delegate.h"

namespace headless {

HeadlessBrowserMainParts::HeadlessBrowserMainParts(HeadlessBrowserImpl& browser)
    : browser_(browser) {}

HeadlessBrowserMainParts::~HeadlessBrowserMainParts() = default;

int HeadlessBrowserMainParts::PreMainMessageLoopRun() {
  SetHeadlessClipboardForCurrentThread();
  screen_orientation_delegate_ =
      std::make_unique<HeadlessScreenOrientationDelegate>();
  browser_->PreMainMessageLoopRun();
  MaybeStartLocalDevToolsHttpHandler();
  HeadlessSelectFileDialogFactory::SetUp();
  return content::RESULT_CODE_NORMAL_EXIT;
}

void HeadlessBrowserMainParts::WillRunMainMessageLoop(
    std::unique_ptr<base::RunLoop>& run_loop) {
  browser_->WillRunMainMessageLoop(*run_loop);
}

void HeadlessBrowserMainParts::PostMainMessageLoopRun() {
  // HeadlessBrowserImpl::Shutdown() is supposed to remove all browser contexts
  // and therefore all associated web contents, however crbug.com/1342152
  // implies it may not be happening.
  CHECK_EQ(0U, browser_->GetAllBrowserContexts().size());
  if (devtools_http_handler_started_) {
    StopLocalDevToolsHttpHandler();
    devtools_http_handler_started_ = false;
  }
  browser_->PostMainMessageLoopRun();
}

void HeadlessBrowserMainParts::MaybeStartLocalDevToolsHttpHandler() {
  if (!browser_->ShouldStartDevToolsServer()) {
    return;
  }

  StartLocalDevToolsHttpHandler(&*browser_);
  devtools_http_handler_started_ = true;
}

}  // namespace headless