Home | History | Annotate | Download | only in provider
      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.
      4 
      5 #ifndef CHROME_BROWSER_ANDROID_PROVIDER_CHROME_BROWSER_PROVIDER_H_
      6 #define CHROME_BROWSER_ANDROID_PROVIDER_CHROME_BROWSER_PROVIDER_H_
      7 
      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/synchronization/waitable_event.h"
     12 #include "base/task/cancelable_task_tracker.h"
     13 #include "chrome/browser/common/cancelable_request.h"
     14 #include "chrome/browser/history/android/android_history_types.h"
     15 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
     16 #include "content/public/browser/notification_observer.h"
     17 #include "content/public/browser/notification_registrar.h"
     18 
     19 class AndroidHistoryProviderService;
     20 class FaviconService;
     21 class Profile;
     22 
     23 namespace history {
     24 class TopSites;
     25 }
     26 
     27 namespace sql {
     28 class Statement;
     29 }
     30 
     31 // This class implements the native methods of ChromeBrowserProvider.java
     32 class ChromeBrowserProvider : public BaseBookmarkModelObserver,
     33                               public content::NotificationObserver {
     34  public:
     35   ChromeBrowserProvider(JNIEnv* env, jobject obj);
     36   void Destroy(JNIEnv*, jobject);
     37 
     38   // JNI registration.
     39   static bool RegisterChromeBrowserProvider(JNIEnv* env);
     40 
     41   // Adds either a new bookmark or bookmark folder based on |is_folder|.  The
     42   // bookmark is added to the beginning of the specified parent and if the
     43   // parent ID is not valid (i.e. < 0) then it will be added to the bookmark
     44   // bar.
     45   jlong AddBookmark(JNIEnv* env, jobject,
     46                     jstring jurl,
     47                     jstring jtitle,
     48                     jboolean is_folder,
     49                     jlong parent_id);
     50 
     51   // Removes a bookmark (or folder) with the specified ID.
     52   jint RemoveBookmark(JNIEnv*, jobject, jlong id);
     53 
     54   // Updates a bookmark (or folder) with the the new title and new URL.
     55   // The |url| field will be ignored if the bookmark node is a folder.
     56   // If a valid |parent_id| (>= 0) different from the currently specified
     57   // parent is given, the node will be moved to that folder as the first
     58   // child.
     59   jint UpdateBookmark(JNIEnv* env,
     60                       jobject,
     61                       jlong id,
     62                       jstring url,
     63                       jstring title,
     64                       jlong parent_id);
     65 
     66   // The below are methods to support Android public API.
     67   // Bookmark and history APIs. -----------------------------------------------
     68   jlong AddBookmarkFromAPI(JNIEnv* env,
     69                            jobject obj,
     70                            jstring url,
     71                            jobject created,
     72                            jobject isBookmark,
     73                            jobject date,
     74                            jbyteArray favicon,
     75                            jstring title,
     76                            jobject visits,
     77                            jlong parent_id);
     78 
     79   base::android::ScopedJavaLocalRef<jobject> QueryBookmarkFromAPI(
     80       JNIEnv* env,
     81       jobject obj,
     82       jobjectArray projection,
     83       jstring selections,
     84       jobjectArray selection_args,
     85       jstring sort_order);
     86 
     87   jint UpdateBookmarkFromAPI(JNIEnv* env,
     88                              jobject obj,
     89                              jstring url,
     90                              jobject created,
     91                              jobject isBookmark,
     92                              jobject date,
     93                              jbyteArray favicon,
     94                              jstring title,
     95                              jobject visits,
     96                              jlong parent_id,
     97                              jstring selections,
     98                              jobjectArray selection_args);
     99 
    100   jint RemoveBookmarkFromAPI(JNIEnv* env,
    101                              jobject obj,
    102                              jstring selections,
    103                              jobjectArray selection_args);
    104 
    105   jint RemoveHistoryFromAPI(JNIEnv* env,
    106                             jobject obj,
    107                             jstring selections,
    108                             jobjectArray selection_args);
    109 
    110   jlong AddSearchTermFromAPI(JNIEnv* env,
    111                              jobject obj,
    112                              jstring search_term,
    113                              jobject date);
    114 
    115   base::android::ScopedJavaLocalRef<jobject> QuerySearchTermFromAPI(
    116       JNIEnv* env,
    117       jobject obj,
    118       jobjectArray projection,
    119       jstring selections,
    120       jobjectArray selection_args,
    121       jstring sort_order);
    122 
    123   jint RemoveSearchTermFromAPI(JNIEnv* env,
    124                                jobject obj,
    125                                jstring selections,
    126                                jobjectArray selection_args);
    127 
    128   jint UpdateSearchTermFromAPI(JNIEnv* env,
    129                                jobject obj,
    130                                jstring search_term,
    131                                jobject date,
    132                                jstring selections,
    133                                jobjectArray selection_args);
    134 
    135   // Custom provider API methods. ---------------------------------------------
    136   jlong CreateBookmarksFolderOnce(JNIEnv* env,
    137                                   jobject obj,
    138                                   jstring title,
    139                                   jlong parent_id);
    140 
    141   void RemoveAllUserBookmarks(JNIEnv* env, jobject obj);
    142 
    143   base::android::ScopedJavaLocalRef<jobject> GetEditableBookmarkFolders(
    144       JNIEnv* env,
    145       jobject obj);
    146 
    147   base::android::ScopedJavaLocalRef<jobject> GetBookmarkNode(
    148       JNIEnv* env,
    149       jobject obj,
    150       jlong id,
    151       jboolean get_parent,
    152       jboolean get_children);
    153 
    154   base::android::ScopedJavaLocalRef<jobject> GetMobileBookmarksFolder(
    155       JNIEnv* env,
    156       jobject obj);
    157 
    158   jboolean IsBookmarkInMobileBookmarksBranch(JNIEnv* env,
    159                                              jobject obj,
    160                                              jlong id);
    161 
    162   jboolean BookmarkNodeExists(JNIEnv* env,
    163                               jobject obj,
    164                               jlong id);
    165 
    166   base::android::ScopedJavaLocalRef<jbyteArray> GetFaviconOrTouchIcon(
    167       JNIEnv* env,
    168       jobject obj,
    169       jstring url);
    170 
    171   base::android::ScopedJavaLocalRef<jbyteArray> GetThumbnail(JNIEnv* env,
    172                                                              jobject obj,
    173                                                              jstring url);
    174 
    175  private:
    176   virtual ~ChromeBrowserProvider();
    177 
    178   // Override BaseBookmarkModelObserver.
    179   virtual void BookmarkModelChanged() OVERRIDE;
    180   virtual void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) OVERRIDE;
    181   virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE;
    182 
    183   // Override NotificationObserver.
    184   virtual void Observe(int type,
    185                        const content::NotificationSource& source,
    186                        const content::NotificationDetails& details) OVERRIDE;
    187 
    188   JavaObjectWeakGlobalRef weak_java_provider_;
    189 
    190   Profile* profile_;
    191   BookmarkModel* bookmark_model_;
    192   history::TopSites* top_sites_;
    193 
    194   scoped_ptr<AndroidHistoryProviderService> service_;
    195   scoped_ptr<FaviconService> favicon_service_;
    196 
    197   // Different cancelable request consumers must be used for each service.
    198   // This because a synchronization issue that could lead to flaky assertion
    199   // failures otherwise.
    200   // TODO(leandrogracia): use a single consumer once http://crbug.com/85970
    201   // is fixed.
    202   CancelableRequestConsumer android_history_consumer_;
    203   CancelableRequestConsumer favicon_consumer_;
    204 
    205   base::CancelableTaskTracker cancelable_task_tracker_;
    206 
    207   // Used to register/unregister notification observer.
    208   content::NotificationRegistrar notification_registrar_;
    209 
    210   bool handling_extensive_changes_;
    211 
    212   DISALLOW_COPY_AND_ASSIGN(ChromeBrowserProvider);
    213 };
    214 
    215 #endif  // CHROME_BROWSER_ANDROID_PROVIDER_CHROME_BROWSER_PROVIDER_H_
    216