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"
13 // Manages the native extraction and request logic for Contextual Search,
14 // and interacts with the Java ContextualSearchManager for UX.
15 // Most of the work is done by the associated ContextualSearchDelegate.
16 class ContextualSearchManager
{
18 // Constructs a native manager associated with the Java manager.
19 ContextualSearchManager(JNIEnv
* env
, jobject obj
);
20 virtual ~ContextualSearchManager();
22 // Called by the Java ContextualSearchManager when it is being destroyed.
23 void Destroy(JNIEnv
* env
, jobject obj
);
25 // Starts the request to get the search terms to use for the given selection,
26 // by accessing our server with the content of the page (from the given
27 // content view core object).
28 // Any outstanding server requests are canceled.
29 // When the server responds with the search term, the Java object is notified
31 // calling OnSearchTermResolutionResponse().
32 void StartSearchTermResolutionRequest(
36 jboolean j_use_resolved_search_term
,
37 jobject j_base_content_view_core
,
38 jboolean j_may_send_base_page_url
);
40 // Gathers the surrounding text around the selection and saves it locally.
41 // Does not send a search term resolution request to the server.
42 void GatherSurroundingText(JNIEnv
* env
,
45 jboolean j_use_resolved_search_term
,
46 jobject j_base_content_view_core
,
47 jboolean j_may_send_base_page_url
);
50 // TODO(donnd): encapsulate these response parameters?
51 void OnSearchTermResolutionResponse(bool is_invalid
,
53 const std::string
& search_term
,
54 const std::string
& display_text
,
55 const std::string
& alternate_term
,
57 int selection_start_adjust
,
58 int selection_end_adjust
);
60 // Calls back to Java with the surrounding text to be displayed.
61 void OnSurroundingTextAvailable(const std::string
& before_text
,
62 const std::string
& after_text
);
64 // Calls back to Java with notification for Icing selection.
65 void OnIcingSelectionAvailable(const std::string
& encoding
,
66 const base::string16
& surrounding_text
,
70 // Our global reference to the Java ContextualSearchManager.
71 base::android::ScopedJavaGlobalRef
<jobject
> java_manager_
;
73 // The delegate we're using the do the real work.
74 scoped_ptr
<ContextualSearchDelegate
> delegate_
;
76 DISALLOW_COPY_AND_ASSIGN(ContextualSearchManager
);
79 bool RegisterContextualSearchManager(JNIEnv
* env
);
81 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_MANAGER_H_