1 // Copyright 2015 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 #ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_MANAGER_H_
6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_MANAGER_H_
8 #include "base/android/jni_android.h"
9 #include "base/task/cancelable_task_tracker.h"
10 #include "chrome/browser/android/contextualsearch/contextual_search_context.h"
11 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h"
15 } // namespace content
17 namespace web_contents_delegate_android
{
18 class WebContentsDelegateAndroid
;
19 } // namespace web_contents_delegate_android
21 // Manages the native extraction and request logic for Contextual Search,
22 // and interacts with the Java ContextualSearchManager for UX.
23 // Most of the work is done by the associated ContextualSearchDelegate.
24 class ContextualSearchManager
{
26 // Constructs a native manager associated with the Java manager.
27 ContextualSearchManager(JNIEnv
* env
, jobject obj
);
28 virtual ~ContextualSearchManager();
30 // Called by the Java ContextualSearchManager when it is being destroyed.
31 void Destroy(JNIEnv
* env
, jobject obj
);
33 // Starts the request to get the search terms to use for the given selection,
34 // by accessing our server with the content of the page (from the given
35 // content view core object).
36 // Any outstanding server requests are canceled.
37 // When the server responds with the search term, the Java object is notified
39 // calling OnSearchTermResolutionResponse().
40 void StartSearchTermResolutionRequest(
44 jboolean j_use_resolved_search_term
,
45 jobject j_base_content_view_core
,
46 jboolean j_may_send_base_page_url
);
48 // Gathers the surrounding text around the selection and saves it locally.
49 // Does not send a search term resolution request to the server.
50 void GatherSurroundingText(JNIEnv
* env
,
53 jboolean j_use_resolved_search_term
,
54 jobject j_base_content_view_core
,
55 jboolean j_may_send_base_page_url
);
57 // Removes a search URL from history. |search_start_time_ms| represents the
58 // time at which |search_url| was committed.
59 void RemoveLastSearchVisit(JNIEnv
* env
,
62 jlong search_start_time_ms
);
64 // Takes ownership of the WebContents associated with the given
65 // |ContentViewCore| which holds the Contextual Search Results.
66 void SetWebContents(JNIEnv
* env
, jobject obj
, jobject jcontent_view_core
,
67 jobject jweb_contents_delegate
);
69 // Destroys the WebContents.
70 void DestroyWebContents(JNIEnv
* env
, jobject jobj
);
72 // Release ownership of WebContents.
73 void ReleaseWebContents(JNIEnv
* env
, jobject jobj
);
75 // Destroys the WebContents of a ContentViewCore.
76 void DestroyWebContentsFromContentViewCore(JNIEnv
* env
,
78 jobject jcontent_view_core
);
80 // Sets the delegate used to convert navigations to intents.
81 void SetInterceptNavigationDelegate(JNIEnv
* env
,
84 jobject jweb_contents
);
87 // TODO(donnd): encapsulate these response parameters?
88 void OnSearchTermResolutionResponse(bool is_invalid
,
90 const std::string
& search_term
,
91 const std::string
& display_text
,
92 const std::string
& alternate_term
,
94 int selection_start_adjust
,
95 int selection_end_adjust
);
97 // Calls back to Java with the surrounding text to be displayed.
98 void OnSurroundingTextAvailable(const std::string
& before_text
,
99 const std::string
& after_text
);
101 // Calls back to Java with notification for Icing selection.
102 void OnIcingSelectionAvailable(const std::string
& encoding
,
103 const base::string16
& surrounding_text
,
107 // Our global reference to the Java ContextualSearchManager.
108 base::android::ScopedJavaGlobalRef
<jobject
> java_manager_
;
110 // The delegate we're using the do the real work.
111 scoped_ptr
<ContextualSearchDelegate
> delegate_
;
113 // Used if we need to clear history.
114 base::CancelableTaskTracker history_task_tracker_
;
116 // The WebContents that holds the Contextual Search Results.
117 scoped_ptr
<content::WebContents
> web_contents_
;
118 scoped_ptr
<web_contents_delegate_android::WebContentsDelegateAndroid
>
119 web_contents_delegate_
;
121 DISALLOW_COPY_AND_ASSIGN(ContextualSearchManager
);
124 bool RegisterContextualSearchManager(JNIEnv
* env
);
126 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_MANAGER_H_