15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/app/android/content_main.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/at_exit.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_switches.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/debug/trace_event.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/lazy_instance.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/app/content_main.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/app/content_main_delegate.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/app/content_main_runner.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/content_switches.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jni/ContentMain_jni.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::LazyInstance;
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LazyInstance<scoped_ptr<ContentMainRunner> > g_content_runner =
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LAZY_INSTANCE_INITIALIZER;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LazyInstance<scoped_ptr<ContentMainDelegate> > g_content_main_delegate =
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LAZY_INSTANCE_INITIALIZER;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void InitApplicationContext(JNIEnv* env, jclass clazz, jobject context) {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::android::InitApplicationContext(env, scoped_context);
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static jint Start(JNIEnv* env, jclass clazz) {
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  TRACE_EVENT0("startup", "content::Start");
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // On Android we can have multiple requests to start the browser in process
40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // simultaneously. If we get an asynchonous request followed by a synchronous
41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // request then we have to call this a second time to finish starting the
42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // browser synchronously.
43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!g_content_runner.Get().get()) {
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ContentMainParams params(g_content_main_delegate.Get().get());
45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    g_content_runner.Get().reset(ContentMainRunner::Create());
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    g_content_runner.Get()->Initialize(params);
47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return g_content_runner.Get()->Run();
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SetContentMainDelegate(ContentMainDelegate* delegate) {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!g_content_main_delegate.Get().get());
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  g_content_main_delegate.Get().reset(delegate);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool RegisterContentMain(JNIEnv* env) {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return RegisterNativesImpl(env);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
61