Mechanical rename of base::debug -> base::trace_event for /content
[chromium-blink-merge.git] / chrome / browser / prerender / external_prerender_handler_android.cc
blob7fa069dd24bc379477d03bb63389f71046ffcf59
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/prerender/external_prerender_handler_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/prerender/prerender_handle.h"
13 #include "chrome/browser/prerender/prerender_manager.h"
14 #include "chrome/browser/prerender/prerender_manager_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_android.h"
17 #include "content/public/browser/web_contents.h"
19 #include "jni/ExternalPrerenderHandler_jni.h"
21 using base::android::ConvertJavaStringToUTF16;
23 namespace prerender {
25 bool ExternalPrerenderHandlerAndroid::AddPrerender(JNIEnv* env,
26 jobject obj,
27 jobject jprofile,
28 jobject jweb_contents,
29 jstring jurl,
30 jstring jreferrer,
31 jint width,
32 jint height) {
33 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
35 GURL url = GURL(ConvertJavaStringToUTF16(env, jurl));
36 if (!url.is_valid())
37 return false;
39 GURL referrer_url = GURL(ConvertJavaStringToUTF16(env, jreferrer));
40 content::Referrer referrer = referrer_url.is_valid() ?
41 content::Referrer(referrer_url, blink::WebReferrerPolicyDefault) :
42 content::Referrer();
43 PrerenderManager* prerender_manager =
44 prerender::PrerenderManagerFactory::GetForProfile(profile);
45 if (!prerender_manager)
46 return false;
47 content::WebContents* web_contents =
48 content::WebContents::FromJavaWebContents(jweb_contents);
49 if (prerender_handle_.get()) {
50 prerender_handle_->OnNavigateAway();
52 prerender_handle_.reset(
53 prerender_manager->AddPrerenderFromExternalRequest(
54 url,
55 referrer,
56 web_contents->GetController().GetDefaultSessionStorageNamespace(),
57 gfx::Size(width, height)));
58 if (!prerender_handle_)
59 return false;
60 return true;
63 void ExternalPrerenderHandlerAndroid::CancelCurrentPrerender(JNIEnv* env,
64 jobject object) {
65 if (!prerender_handle_)
66 return;
67 prerender_handle_->OnCancel();
68 prerender_handle_.reset();
71 static jboolean HasPrerenderedUrl(JNIEnv* env,
72 jclass clazz,
73 jobject jprofile,
74 jstring jurl,
75 jobject jweb_contents) {
76 if (jurl == NULL)
77 return false;
79 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
80 GURL url = GURL(ConvertJavaStringToUTF16(env, jurl));
81 if (!url.is_valid())
82 return false;
83 prerender::PrerenderManager* prerender_manager =
84 prerender::PrerenderManagerFactory::GetForProfile(profile);
85 if (!prerender_manager)
86 return false;
87 content::WebContents* web_contents =
88 content::WebContents::FromJavaWebContents(jweb_contents);
89 return prerender_manager->HasPrerenderedUrl(url, web_contents);
92 static jboolean HasCookieStoreLoaded(JNIEnv* env,
93 jclass clazz,
94 jobject jprofile) {
95 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
96 prerender::PrerenderManager* prerender_manager =
97 prerender::PrerenderManagerFactory::GetForProfile(profile);
98 if (!prerender_manager)
99 return false;
100 return prerender_manager->cookie_store_loaded();
103 ExternalPrerenderHandlerAndroid::ExternalPrerenderHandlerAndroid() {}
105 ExternalPrerenderHandlerAndroid::~ExternalPrerenderHandlerAndroid() {}
107 static jlong Init(JNIEnv* env, jclass clazz) {
108 ExternalPrerenderHandlerAndroid* external_handler =
109 new ExternalPrerenderHandlerAndroid();
110 return reinterpret_cast<intptr_t>(external_handler);
113 bool ExternalPrerenderHandlerAndroid::RegisterExternalPrerenderHandlerAndroid(
114 JNIEnv* env) {
115 return RegisterNativesImpl(env);
118 } // namespace prerender