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

content / app / android / content_main_android.cc [blame]

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

#include "content/app/android/content_main_android.h"

#include <memory>

#include "base/android/binder.h"
#include "base/android/binder_box.h"
#include "base/lazy_instance.h"
#include "base/no_destructor.h"
#include "base/trace_event/trace_event.h"
#include "content/public/app/content_main.h"
#include "content/public/app/content_main_delegate.h"
#include "content/public/app/content_main_runner.h"
#include "content/public/common/content_client.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "content/public/android/content_app_jni/ContentMain_jni.h"

using base::LazyInstance;
using base::android::JavaParamRef;

namespace content {

namespace {

ContentMainRunner* GetContentMainRunner() {
  static base::NoDestructor<std::unique_ptr<ContentMainRunner>> runner{
      ContentMainRunner::Create()};
  return runner->get();
}

LazyInstance<std::unique_ptr<ContentMainDelegate>>::DestructorAtExit
    g_content_main_delegate = LAZY_INSTANCE_INITIALIZER;

}  // namespace

class ContentClientCreator {
 public:
  static void Create(ContentMainDelegate* delegate) {
    ContentClient* client = delegate->CreateContentClient();
    DCHECK(client);
    SetContentClient(client);
  }
};

static void JNI_ContentMain_SetBindersFromParent(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& binder_box) {
  base::android::SetBindersFromParent(
      base::android::UnpackBinderBox(env, binder_box)
          .value_or(std::vector<base::android::BinderRef>()));
}

// TODO(qinmin/hanxi): split this function into 2 separate methods: One to
// start the minimal browser and one to start the remainder of the browser
// process. The first method should always be called upon browser start, and
// the second method can be deferred. See http://crbug.com/854209.
static jint JNI_ContentMain_Start(JNIEnv* env, jboolean start_minimal_browser) {
  TRACE_EVENT0("startup", "content::Start");
  ContentMainParams params(g_content_main_delegate.Get().get());
  params.minimal_browser_mode = start_minimal_browser;
  return RunContentProcess(std::move(params), GetContentMainRunner());
}

void SetContentMainDelegate(ContentMainDelegate* delegate) {
  DCHECK(!g_content_main_delegate.Get().get());
  g_content_main_delegate.Get().reset(delegate);
  // The ContentClient needs to be set early so that it can be used by the
  // content library loader hooks.
  if (!GetContentClient())
    ContentClientCreator::Create(delegate);
}

ContentMainDelegate* GetContentMainDelegateForTesting() {
  DCHECK(g_content_main_delegate.Get().get());
  return g_content_main_delegate.Get().get();
}

}  // namespace content