1 // Copyright (c) 2012 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_PROVIDER_CHROME_BROWSER_PROVIDER_H_
6 #define CHROME_BROWSER_ANDROID_PROVIDER_CHROME_BROWSER_PROVIDER_H_
8 #include "base/android/jni_weak_ref.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/scoped_observer.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/task/cancelable_task_tracker.h"
14 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
15 #include "components/history/core/android/android_history_types.h"
16 #include "components/history/core/browser/history_service_observer.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
20 class AndroidHistoryProviderService;
32 // This class implements the native methods of ChromeBrowserProvider.java
33 class ChromeBrowserProvider : public BaseBookmarkModelObserver,
34 public content::NotificationObserver,
35 public history::HistoryServiceObserver {
37 ChromeBrowserProvider(JNIEnv* env, jobject obj);
38 void Destroy(JNIEnv*, jobject);
41 static bool RegisterChromeBrowserProvider(JNIEnv* env);
43 // Adds either a new bookmark or bookmark folder based on |is_folder|. The
44 // bookmark is added to the beginning of the specified parent and if the
45 // parent ID is not valid (i.e. < 0) then it will be added to the bookmark
47 jlong AddBookmark(JNIEnv* env, jobject,
53 // Removes a bookmark (or folder) with the specified ID.
54 jint RemoveBookmark(JNIEnv*, jobject, jlong id);
56 // Updates a bookmark (or folder) with the the new title and new URL.
57 // The |url| field will be ignored if the bookmark node is a folder.
58 // If a valid |parent_id| (>= 0) different from the currently specified
59 // parent is given, the node will be moved to that folder as the first
61 jint UpdateBookmark(JNIEnv* env,
68 // The below are methods to support Android public API.
69 // Bookmark and history APIs. -----------------------------------------------
70 jlong AddBookmarkFromAPI(JNIEnv* env,
81 base::android::ScopedJavaLocalRef<jobject> QueryBookmarkFromAPI(
84 jobjectArray projection,
86 jobjectArray selection_args,
89 jint UpdateBookmarkFromAPI(JNIEnv* env,
100 jobjectArray selection_args);
102 jint RemoveBookmarkFromAPI(JNIEnv* env,
105 jobjectArray selection_args);
107 jint RemoveHistoryFromAPI(JNIEnv* env,
110 jobjectArray selection_args);
112 jlong AddSearchTermFromAPI(JNIEnv* env,
117 base::android::ScopedJavaLocalRef<jobject> QuerySearchTermFromAPI(
120 jobjectArray projection,
122 jobjectArray selection_args,
125 jint RemoveSearchTermFromAPI(JNIEnv* env,
128 jobjectArray selection_args);
130 jint UpdateSearchTermFromAPI(JNIEnv* env,
135 jobjectArray selection_args);
137 // Custom provider API methods. ---------------------------------------------
138 jlong CreateBookmarksFolderOnce(JNIEnv* env,
143 void RemoveAllUserBookmarks(JNIEnv* env, jobject obj);
145 base::android::ScopedJavaLocalRef<jobject> GetEditableBookmarkFolders(
149 base::android::ScopedJavaLocalRef<jobject> GetBookmarkNode(
154 jboolean get_children);
156 base::android::ScopedJavaLocalRef<jobject> GetMobileBookmarksFolder(
160 jboolean IsBookmarkInMobileBookmarksBranch(JNIEnv* env,
164 jboolean BookmarkNodeExists(JNIEnv* env,
168 base::android::ScopedJavaLocalRef<jbyteArray> GetFaviconOrTouchIcon(
173 base::android::ScopedJavaLocalRef<jbyteArray> GetThumbnail(JNIEnv* env,
178 virtual ~ChromeBrowserProvider();
180 // Override BaseBookmarkModelObserver.
181 virtual void BookmarkModelChanged() override;
182 virtual void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) override;
183 virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) override;
185 // Deals with updates to the history service.
186 void OnHistoryChanged();
188 // Override HistoryServiceObserver.
189 virtual void OnURLVisited(HistoryService* history_service,
190 ui::PageTransition transition,
191 const history::URLRow& row,
192 const history::RedirectList& redirects,
193 base::Time visit_time) override;
195 // Override NotificationObserver.
196 virtual void Observe(int type,
197 const content::NotificationSource& source,
198 const content::NotificationDetails& details) override;
200 JavaObjectWeakGlobalRef weak_java_provider_;
202 // Profile must outlive this object.
204 // BookmarkModel, HistoryService and history::TopSites lifetime is bound to
205 // the lifetime of Profile, they are safe to use as long as the Profile is
208 BookmarkModel* bookmark_model_;
209 history::TopSites* top_sites_;
211 scoped_ptr<AndroidHistoryProviderService> service_;
213 base::CancelableTaskTracker cancelable_task_tracker_;
215 // Used to register/unregister notification observer.
216 content::NotificationRegistrar notification_registrar_;
217 ScopedObserver<HistoryService, HistoryServiceObserver>
218 history_service_observer_;
220 bool handling_extensive_changes_;
222 DISALLOW_COPY_AND_ASSIGN(ChromeBrowserProvider);
225 #endif // CHROME_BROWSER_ANDROID_PROVIDER_CHROME_BROWSER_PROVIDER_H_