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/android/omnibox/omnibox_prerender.h"
7 #include "base/android/jni_string.h"
8 #include "base/logging.h"
9 #include "chrome/browser/android/tab_android.h"
10 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
11 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_android.h"
14 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
15 #include "components/omnibox/browser/autocomplete_match.h"
16 #include "components/omnibox/browser/autocomplete_result.h"
17 #include "content/public/browser/web_contents.h"
18 #include "jni/OmniboxPrerender_jni.h"
21 using predictors::AutocompleteActionPredictor
;
22 using predictors::AutocompleteActionPredictorFactory
;
24 OmniboxPrerender::OmniboxPrerender(JNIEnv
* env
, jobject obj
)
25 : weak_java_omnibox_(env
, obj
) {
28 OmniboxPrerender::~OmniboxPrerender() {
31 static jlong
Init(JNIEnv
* env
, const JavaParamRef
<jobject
>& obj
) {
32 OmniboxPrerender
* omnibox
= new OmniboxPrerender(env
, obj
);
33 return reinterpret_cast<intptr_t>(omnibox
);
36 bool RegisterOmniboxPrerender(JNIEnv
* env
) {
37 return RegisterNativesImpl(env
);
40 void OmniboxPrerender::Clear(JNIEnv
* env
,
42 jobject j_profile_android
) {
43 Profile
* profile
= ProfileAndroid::FromProfileAndroid(j_profile_android
);
47 AutocompleteActionPredictor
* action_predictor
=
48 AutocompleteActionPredictorFactory::GetForProfile(profile
);
49 action_predictor
->ClearTransitionalMatches();
50 action_predictor
->CancelPrerender();
53 void OmniboxPrerender::InitializeForProfile(
56 jobject j_profile_android
) {
57 Profile
* profile
= ProfileAndroid::FromProfileAndroid(j_profile_android
);
58 // Initialize the AutocompleteActionPredictor for this profile.
59 // It needs to register for notifications as part of its initialization.
60 AutocompleteActionPredictorFactory::GetForProfile(profile
);
63 void OmniboxPrerender::PrerenderMaybe(JNIEnv
* env
,
66 jstring j_current_url
,
68 jobject j_profile_android
,
70 AutocompleteResult
* autocomplete_result
=
71 reinterpret_cast<AutocompleteResult
*>(jsource_match
);
72 Profile
* profile
= ProfileAndroid::FromProfileAndroid(j_profile_android
);
73 base::string16 url_string
=
74 base::android::ConvertJavaStringToUTF16(env
, j_url
);
75 base::string16 current_url_string
=
76 base::android::ConvertJavaStringToUTF16(env
, j_current_url
);
77 content::WebContents
* web_contents
=
78 TabAndroid::GetNativeTab(env
, j_tab
)->web_contents();
79 // TODO(apiccion) Use a delegate for communicating with web_contents.
80 // This can happen in OmniboxTests since the results are generated
82 if (!autocomplete_result
)
87 const AutocompleteResult::const_iterator
default_match(
88 autocomplete_result
->default_match());
89 if (default_match
== autocomplete_result
->end())
92 AutocompleteActionPredictor::Action recommended_action
=
93 AutocompleteActionPredictor::ACTION_NONE
;
94 InstantSearchPrerenderer
* prerenderer
=
95 InstantSearchPrerenderer::GetForProfile(profile
);
97 prerenderer
->IsAllowed(*default_match
, web_contents
)) {
98 recommended_action
= AutocompleteActionPredictor::ACTION_PRERENDER
;
100 AutocompleteActionPredictor
* action_predictor
=
101 AutocompleteActionPredictorFactory::GetForProfile(profile
);
102 if (!action_predictor
)
105 if (action_predictor
) {
107 RegisterTransitionalMatches(url_string
, *autocomplete_result
);
109 action_predictor
->RecommendAction(url_string
, *default_match
);
113 GURL current_url
= GURL(current_url_string
);
114 switch (recommended_action
) {
115 case AutocompleteActionPredictor::ACTION_PRERENDER
:
116 // Ask for prerendering if the destination URL is different than the
118 if (default_match
->destination_url
!= current_url
) {
125 case AutocompleteActionPredictor::ACTION_PRECONNECT
:
126 // TODO (apiccion) add preconnect logic
128 case AutocompleteActionPredictor::ACTION_NONE
:
136 void OmniboxPrerender::DoPrerender(const AutocompleteMatch
& match
,
138 content::WebContents
* web_contents
) {
142 DCHECK(web_contents
);
145 gfx::Rect container_bounds
= web_contents
->GetContainerBounds();
146 InstantSearchPrerenderer
* prerenderer
=
147 InstantSearchPrerenderer::GetForProfile(profile
);
148 if (prerenderer
&& prerenderer
->IsAllowed(match
, web_contents
)) {
150 web_contents
->GetController().GetDefaultSessionStorageNamespace(),
151 container_bounds
.size());
154 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile
)->
156 match
.destination_url
,
157 web_contents
->GetController().GetDefaultSessionStorageNamespace(),
158 container_bounds
.size());