BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / prerender / external_prerender_handler_android.cc
blobe02dfeef135cd230be91c101b26752a3bd450b7b
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 const JavaParamRef<jclass>& clazz,
73 const JavaParamRef<jobject>& jprofile,
74 const JavaParamRef<jstring>& jurl,
75 const JavaParamRef<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 ExternalPrerenderHandlerAndroid::ExternalPrerenderHandlerAndroid() {}
94 ExternalPrerenderHandlerAndroid::~ExternalPrerenderHandlerAndroid() {}
96 static jlong Init(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
97 ExternalPrerenderHandlerAndroid* external_handler =
98 new ExternalPrerenderHandlerAndroid();
99 return reinterpret_cast<intptr_t>(external_handler);
102 bool ExternalPrerenderHandlerAndroid::RegisterExternalPrerenderHandlerAndroid(
103 JNIEnv* env) {
104 return RegisterNativesImpl(env);
107 } // namespace prerender