OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/android/bottombar/contextualsearch/contextual_search_pa
nel.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/android/jni_string.h" | |
10 #include "base/callback.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/time/time.h" | |
13 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h" | |
14 #include "chrome/browser/android/tab_android.h" | |
15 #include "chrome/browser/history/history_service_factory.h" | |
16 #include "chrome/browser/profiles/profile_manager.h" | |
17 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
18 #include "chrome/browser/ui/android/window_android_helper.h" | |
19 #include "components/history/core/browser/history_service.h" | |
20 #include "components/navigation_interception/intercept_navigation_delegate.h" | |
21 #include "components/variations/variations_associated_data.h" | |
22 #include "components/web_contents_delegate_android/web_contents_delegate_android
.h" | |
23 #include "content/public/browser/android/content_view_core.h" | |
24 #include "content/public/browser/web_contents.h" | |
25 #include "jni/ContextualSearchPanel_jni.h" | |
26 #include "net/url_request/url_fetcher_impl.h" | |
27 | |
28 using content::ContentViewCore; | |
29 | |
30 namespace { | |
31 | |
32 const int kHistoryDeletionWindowSeconds = 2; | |
33 | |
34 // Because we need a callback, this needs to exist. | |
35 void OnHistoryDeletionDone() { | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 // This class manages the native behavior of the Contextual Search feature. | |
41 // Instances of this class are owned by the Java ContextualSearchPanel. | |
42 ContextualSearchPanel::ContextualSearchPanel(JNIEnv* env, jobject obj) { | |
43 java_manager_.Reset(env, obj); | |
44 Java_ContextualSearchPanel_setNativePanelContentPtr( | |
45 env, obj, reinterpret_cast<intptr_t>(this)); | |
46 } | |
47 | |
48 ContextualSearchPanel::~ContextualSearchPanel() { | |
49 JNIEnv* env = base::android::AttachCurrentThread(); | |
50 Java_ContextualSearchPanel_clearNativePanelContentPtr( | |
51 env, java_manager_.obj()); | |
52 } | |
53 | |
54 void ContextualSearchPanel::Destroy(JNIEnv* env, jobject obj) { delete this; } | |
55 | |
56 void ContextualSearchPanel::RemoveLastHistoryEntry( | |
57 JNIEnv* env, | |
58 jobject obj, | |
59 jstring search_url, | |
60 jlong search_start_time_ms) { | |
61 // The deletion window is from the time a search URL was put in history, up | |
62 // to a short amount of time later. | |
63 base::Time begin_time = base::Time::FromJsTime(search_start_time_ms); | |
64 base::Time end_time = begin_time + | |
65 base::TimeDelta::FromSeconds(kHistoryDeletionWindowSeconds); | |
66 | |
67 history::HistoryService* service = HistoryServiceFactory::GetForProfile( | |
68 ProfileManager::GetActiveUserProfile(), | |
69 ServiceAccessType::EXPLICIT_ACCESS); | |
70 if (service) { | |
71 // NOTE(mathp): We are only removing |search_url| from the local history | |
72 // because search results that are not promoted to a Tab do not make it to | |
73 // the web history, only local. | |
74 std::set<GURL> restrict_set; | |
75 restrict_set.insert( | |
76 GURL(base::android::ConvertJavaStringToUTF8(env, search_url))); | |
77 service->ExpireHistoryBetween( | |
78 restrict_set, | |
79 begin_time, | |
80 end_time, | |
81 base::Bind(&OnHistoryDeletionDone), | |
82 &history_task_tracker_); | |
83 } | |
84 } | |
85 | |
86 void ContextualSearchPanel::SetWebContents(JNIEnv* env, | |
87 jobject obj, | |
88 jobject jcontent_view_core, | |
89 jobject jweb_contents_delegate) { | |
90 content::ContentViewCore* content_view_core = | |
91 content::ContentViewCore::GetNativeContentViewCore(env, | |
92 jcontent_view_core); | |
93 DCHECK(content_view_core); | |
94 DCHECK(content_view_core->GetWebContents()); | |
95 | |
96 // NOTE(pedrosimonetti): Takes ownership of the WebContents associated | |
97 // with the ContentViewCore. This is to make sure that the WebContens | |
98 // and the Compositor are in the same process. | |
99 // TODO(pedrosimonetti): Confirm with dtrainor@ if the comment above | |
100 // is accurate. | |
101 web_contents_.reset(content_view_core->GetWebContents()); | |
102 // TODO(pedrosimonetti): confirm if we need this after promoting it | |
103 // to a real tab. | |
104 TabAndroid::AttachTabHelpers(web_contents_.get()); | |
105 WindowAndroidHelper::FromWebContents(web_contents_.get()) | |
106 ->SetWindowAndroid(content_view_core->GetWindowAndroid()); | |
107 web_contents_delegate_.reset( | |
108 new web_contents_delegate_android::WebContentsDelegateAndroid( | |
109 env, jweb_contents_delegate)); | |
110 web_contents_->SetDelegate(web_contents_delegate_.get()); | |
111 } | |
112 | |
113 void ContextualSearchPanel::DestroyWebContents(JNIEnv* env, jobject jobj) { | |
114 DCHECK(web_contents_.get()); | |
115 web_contents_.reset(); | |
116 // |web_contents_delegate_| may already be NULL at this point. | |
117 web_contents_delegate_.reset(); | |
118 } | |
119 | |
120 void ContextualSearchPanel::SetInterceptNavigationDelegate( | |
121 JNIEnv* env, | |
122 jobject obj, | |
123 jobject delegate, | |
124 jobject jweb_contents) { | |
125 content::WebContents* web_contents = | |
126 content::WebContents::FromJavaWebContents(jweb_contents); | |
127 DCHECK(web_contents); | |
128 navigation_interception::InterceptNavigationDelegate::Associate( | |
129 web_contents, | |
130 make_scoped_ptr(new navigation_interception::InterceptNavigationDelegate( | |
131 env, delegate))); | |
132 } | |
133 | |
134 bool RegisterContextualSearchPanel(JNIEnv* env) { | |
135 return RegisterNativesImpl(env); | |
136 } | |
137 | |
138 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
139 ContextualSearchPanel* manager = new ContextualSearchPanel(env, obj); | |
140 return reinterpret_cast<intptr_t>(manager); | |
141 } | |
OLD | NEW |