summaryrefslogtreecommitdiffstats
path: root/chrome/browser/android/omnibox
diff options
context:
space:
mode:
authorapiccion@chromium.org <apiccion@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-03 12:17:35 +0000
committerapiccion@chromium.org <apiccion@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-03 12:17:35 +0000
commita679e42fffa709c69aec93eb4e7cbcd9fd1e9f55 (patch)
tree8340e2297cb6878556fde4c89ce8f3bf055d7811 /chrome/browser/android/omnibox
parent6790829574756765572a05fc9c94ec555020834e (diff)
downloadchromium_src-a679e42fffa709c69aec93eb4e7cbcd9fd1e9f55.zip
chromium_src-a679e42fffa709c69aec93eb4e7cbcd9fd1e9f55.tar.gz
chromium_src-a679e42fffa709c69aec93eb4e7cbcd9fd1e9f55.tar.bz2
Added OmniboxAndroid class to handle prerender related logic.
BUG=166665 Review URL: https://chromiumcodereview.appspot.com/21061005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@215507 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/android/omnibox')
-rw-r--r--chrome/browser/android/omnibox/omnibox_prerender.cc143
-rw-r--r--chrome/browser/android/omnibox/omnibox_prerender.h70
2 files changed, 213 insertions, 0 deletions
diff --git a/chrome/browser/android/omnibox/omnibox_prerender.cc b/chrome/browser/android/omnibox/omnibox_prerender.cc
new file mode 100644
index 0000000..216a719
--- /dev/null
+++ b/chrome/browser/android/omnibox/omnibox_prerender.cc
@@ -0,0 +1,143 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "omnibox_prerender.h"
+
+#include "base/android/jni_string.h"
+#include "base/logging.h"
+#include "chrome/browser/autocomplete/autocomplete_match.h"
+#include "chrome/browser/autocomplete/autocomplete_result.h"
+#include "chrome/browser/predictors/autocomplete_action_predictor.h"
+#include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/profile_android.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/browser/web_contents_view.h"
+#include "jni/OmniboxPrerender_jni.h"
+#include "url/gurl.h"
+
+using predictors::AutocompleteActionPredictor;
+using predictors::AutocompleteActionPredictorFactory;
+
+OmniboxPrerender::OmniboxPrerender(JNIEnv* env, jobject obj)
+ : weak_java_omnibox_(env, obj) {
+}
+
+OmniboxPrerender::~OmniboxPrerender() {
+}
+
+static jint Init(JNIEnv* env, jobject obj) {
+ OmniboxPrerender* omnibox = new OmniboxPrerender(env, obj);
+ return reinterpret_cast<jint>(omnibox);
+}
+
+bool RegisterOmniboxPrerender(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+void OmniboxPrerender::Clear(JNIEnv* env,
+ jobject obj,
+ jobject j_profile_android) {
+ Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile_android);
+ DCHECK(profile);
+ if (!profile)
+ return;
+ AutocompleteActionPredictor* action_predictor =
+ AutocompleteActionPredictorFactory::GetForProfile(profile);
+ action_predictor->ClearTransitionalMatches();
+}
+
+void OmniboxPrerender::InitializeForProfile(
+ JNIEnv* env,
+ jobject obj,
+ jobject j_profile_android) {
+ Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile_android);
+ // Initialize the AutocompleteActionPredictor for this profile.
+ // It needs to register for notifications as part of its initialization.
+ AutocompleteActionPredictorFactory::GetForProfile(profile);
+}
+
+void OmniboxPrerender::PrerenderMaybe(JNIEnv* env,
+ jobject obj,
+ jstring j_url,
+ jstring j_current_url,
+ jint jsource_match,
+ jobject j_profile_android,
+ jint native_web_contents) {
+ AutocompleteResult* autocomplete_result =
+ reinterpret_cast<AutocompleteResult*>(jsource_match);
+ Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile_android);
+ base::string16 url_string =
+ base::android::ConvertJavaStringToUTF16(env, j_url);
+ base::string16 current_url_string =
+ base::android::ConvertJavaStringToUTF16(env, j_current_url);
+ content::WebContents* web_contents =
+ reinterpret_cast<content::WebContents*>(native_web_contents);
+ // TODO(apiccion) Use a delegate for communicating with web_contents.
+ // This can happen in OmniboxTests since the results are generated
+ // in Java only.
+ if (!autocomplete_result)
+ return;
+ if (!profile)
+ return;
+
+ const AutocompleteResult::const_iterator default_match(
+ autocomplete_result->default_match());
+ if (default_match == autocomplete_result->end())
+ return;
+
+ AutocompleteActionPredictor* action_predictor =
+ AutocompleteActionPredictorFactory::GetForProfile(profile);
+ if (!action_predictor)
+ return;
+
+ AutocompleteActionPredictor::Action recommended_action =
+ AutocompleteActionPredictor::ACTION_NONE;
+ if (action_predictor) {
+ action_predictor->
+ RegisterTransitionalMatches(url_string, *autocomplete_result);
+ recommended_action =
+ action_predictor->RecommendAction(url_string, *default_match);
+ }
+
+ GURL current_url = GURL(current_url_string);
+ switch (recommended_action) {
+ case AutocompleteActionPredictor::ACTION_PRERENDER:
+ // Ask for prerendering if the destination URL is different than the
+ // current URL.
+ if (default_match->destination_url != current_url) {
+ DoPrerender(
+ *default_match,
+ profile,
+ web_contents);
+ }
+ break;
+ case AutocompleteActionPredictor::ACTION_PRECONNECT:
+ // TODO (apiccion) add preconnect logic
+ break;
+ case AutocompleteActionPredictor::ACTION_NONE:
+ break;
+ default:
+ NOTREACHED();
+ break;
+ }
+}
+
+void OmniboxPrerender::DoPrerender(const AutocompleteMatch& match,
+ Profile* profile,
+ content::WebContents* web_contents) {
+ DCHECK(profile);
+ if (!profile)
+ return;
+ DCHECK(web_contents);
+ if (!web_contents)
+ return;
+ gfx::Rect container_bounds;
+ web_contents->GetView()->GetContainerBounds(&container_bounds);
+ predictors::AutocompleteActionPredictorFactory::GetForProfile(profile)->
+ StartPrerendering(
+ match.destination_url,
+ web_contents->GetController().GetSessionStorageNamespaceMap(),
+ container_bounds.size());
+}
diff --git a/chrome/browser/android/omnibox/omnibox_prerender.h b/chrome/browser/android/omnibox/omnibox_prerender.h
new file mode 100644
index 0000000..c164021
--- /dev/null
+++ b/chrome/browser/android/omnibox/omnibox_prerender.h
@@ -0,0 +1,70 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_ANDROID_OMNIBOX_OMNIBOX_PRERENDER_H_
+#define CHROME_BROWSER_ANDROID_OMNIBOX_OMNIBOX_PRERENDER_H_
+
+#include "base/android/jni_helper.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/string16.h"
+
+class AutocompleteResult;
+class ProfielAndroid;
+class Profile;
+class TabAndroid;
+struct AutocompleteMatch;
+
+namespace content {
+class WebContents;
+}
+
+// This class is responsible for taking the user's omnibox input text,
+// AutocompleteResults and navigation actions and then feeding them to the
+// AutocompleteActionPredictor. The predictor uses it to update its
+// database and returns predictions on what page, if any, to pre-render
+// or pre-connect. This class then takes the corresponding action.
+class OmniboxPrerender {
+ public:
+ OmniboxPrerender(JNIEnv* env, jobject obj);
+ virtual ~OmniboxPrerender();
+
+ // Clears the transitional matches. This should be called when the user
+ // stops typing into the omnibox (e.g. when navigating away, closing the
+ // keyboard or changing tabs).
+ void Clear(JNIEnv* env, jobject obj, jobject j_profile_android);
+
+ // Initializes the underlying action predictor for a given profile instance.
+ // This should be called as soon as possible as the predictor must register
+ // for certain notifications to properly initialize before providing
+ // predictions and updated its learning database.
+ void InitializeForProfile(JNIEnv* env,
+ jobject obj,
+ jobject j_profile_android);
+
+ // Potentailly invokes a pre-render or pre-connect given the url typed into
+ // the omnibox and a corresponding autocomplete result. This should be
+ // invoked everytime the omnibox changes (e.g. As the user types characters
+ // this method should be invoked at least once per character).
+ void PrerenderMaybe(JNIEnv* env,
+ jobject obj,
+ jstring j_url,
+ jstring j_current_url,
+ jint jsource_match,
+ jobject j_profile_android,
+ jint native_web_contents);
+
+ private:
+
+ // Prerenders a given AutocompleteMatch's url.
+ void DoPrerender(const AutocompleteMatch& match,
+ Profile* profile,
+ content::WebContents* web_contents);
+ JavaObjectWeakGlobalRef weak_java_omnibox_;
+
+ DISALLOW_COPY_AND_ASSIGN(OmniboxPrerender);
+};
+
+bool RegisterOmniboxPrerender(JNIEnv* env);
+
+#endif // CHROME_BROWSER_ANDROID_OMNIBOX_OMNIBOX_PRERENDER_H_