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_helper.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 "chrome/browser/bookmarks/base_bookmark_model_observer.h"
     13 #include "chrome/browser/common/cancelable_request.h"
     14 #include "chrome/browser/history/android/android_history_types.h"
     15 #include "chrome/common/cancelable_task_tracker.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 RemoveAllBookmarks(JNIEnv* env, jobject obj);
    142 
    143   base::android::ScopedJavaLocalRef<jobject> GetAllBookmarkFolders(JNIEnv* env,
    144                                                                    jobject obj);
    145 
    146   base::android::ScopedJavaLocalRef<jobject> GetBookmarkNode(
    147       JNIEnv* env,
    148       jobject obj,
    149       jlong id,
    150       jboolean get_parent,
    151       jboolean get_children);
    152 
    153   base::android::ScopedJavaLocalRef<jobject> GetMobileBookmarksFolder(
    154       JNIEnv* env,
    155       jobject obj);
    156 
    157   jboolean IsBookmarkInMobileBookmarksBranch(JNIEnv* env,
    158                                              jobject obj,
    159                                              jlong id);
    160 
    161   jboolean BookmarkNodeExists(JNIEnv* env,
    162                               jobject obj,
    163                               jlong id);
    164 
    165   base::android::ScopedJavaLocalRef<jbyteArray> GetFaviconOrTouchIcon(
    166       JNIEnv* env,
    167       jobject obj,
    168       jstring url);
    169 
    170   base::android::ScopedJavaLocalRef<jbyteArray> GetThumbnail(JNIEnv* env,
    171                                                              jobject obj,
    172                                                              jstring url);
    173 
    174  private:
    175   virtual ~ChromeBrowserProvider();
    176 
    177   // Override BaseBookmarkModelObserver.
    178   virtual void BookmarkModelChanged() OVERRIDE;
    179   virtual void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) OVERRIDE;
    180   virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE;
    181 
    182   // Override NotificationObserver.
    183   virtual void Observe(int type,
    184                        const content::NotificationSource& source,
    185                        const content::NotificationDetails& details) OVERRIDE;
    186 
    187   JavaObjectWeakGlobalRef weak_java_provider_;
    188 
    189   Profile* profile_;
    190   BookmarkModel* bookmark_model_;
    191   history::TopSites* top_sites_;
    192 
    193   scoped_ptr<AndroidHistoryProviderService> service_;
    194   scoped_ptr<FaviconService> favicon_service_;
    195 
    196   // Different cancelable request consumers must be used for each service.
    197   // This because a synchronization issue that could lead to flaky assertion
    198   // failures otherwise.
    199   // TODO(leandrogracia): use a single consumer once http://crbug.com/85970
    200   // is fixed.
    201   CancelableRequestConsumer android_history_consumer_;
    202   CancelableRequestConsumer favicon_consumer_;
    203 
    204   CancelableTaskTracker cancelable_task_tracker_;
    205 
    206   // Used to register/unregister notification observer.
    207   content::NotificationRegistrar notification_registrar_;
    208 
    209   bool handling_extensive_changes_;
    210 
    211   DISALLOW_COPY_AND_ASSIGN(ChromeBrowserProvider);
    212 };
    213 
    214 #endif  // CHROME_BROWSER_ANDROID_PROVIDER_CHROME_BROWSER_PROVIDER_H_
    215