Home | History | Annotate | Download | only in android
      1 // Copyright 2013 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_MOST_VISITED_SITES_H_
      6 #define CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_
      7 
      8 #include <jni.h>
      9 
     10 #include "base/android/scoped_java_ref.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "chrome/browser/history/history_types.h"
     14 #include "chrome/browser/profiles/profile.h"
     15 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h"
     16 #include "content/public/browser/notification_observer.h"
     17 #include "content/public/browser/notification_registrar.h"
     18 
     19 namespace suggestions {
     20 class SuggestionsService;
     21 }
     22 
     23 // Provides the list of most visited sites and their thumbnails to Java.
     24 class MostVisitedSites : public content::NotificationObserver {
     25  public:
     26   typedef base::Callback<
     27       void(base::android::ScopedJavaGlobalRef<jobject>* bitmap,
     28            base::android::ScopedJavaGlobalRef<jobject>* j_callback)>
     29       LookupSuccessCallback;
     30 
     31   explicit MostVisitedSites(Profile* profile);
     32   void Destroy(JNIEnv* env, jobject obj);
     33   void OnLoadingComplete(JNIEnv* env, jobject obj);
     34   void SetMostVisitedURLsObserver(JNIEnv* env,
     35                                   jobject obj,
     36                                   jobject j_observer,
     37                                   jint num_sites);
     38   void GetURLThumbnail(JNIEnv* env,
     39                        jobject obj,
     40                        jstring url,
     41                        jobject j_callback);
     42   void BlacklistUrl(JNIEnv* env, jobject obj, jstring j_url);
     43   void RecordOpenedMostVisitedItem(JNIEnv* env, jobject obj, jint index);
     44 
     45   // content::NotificationObserver implementation.
     46   virtual void Observe(int type,
     47                        const content::NotificationSource& source,
     48                        const content::NotificationDetails& details) OVERRIDE;
     49 
     50   // Registers JNI methods.
     51   static bool Register(JNIEnv* env);
     52 
     53  private:
     54   virtual ~MostVisitedSites();
     55   void QueryMostVisitedURLs();
     56 
     57   // Initialize the query to Top Sites. Called if the SuggestionsService is not
     58   // enabled, or if it returns no data.
     59   void InitiateTopSitesQuery();
     60 
     61   // Callback for when data is available from TopSites.
     62   void OnMostVisitedURLsAvailable(
     63       base::android::ScopedJavaGlobalRef<jobject>* j_observer,
     64       int num_sites,
     65       const history::MostVisitedURLList& visited_list);
     66 
     67   // Callback for when data is available from the SuggestionsService.
     68   void OnSuggestionsProfileAvailable(
     69       base::android::ScopedJavaGlobalRef<jobject>* j_observer,
     70       const suggestions::SuggestionsProfile& suggestions_profile);
     71 
     72   // Callback for when the local thumbnail lookup is complete.
     73   void OnObtainedThumbnail(
     74       base::android::ScopedJavaGlobalRef<jobject>* bitmap,
     75       base::android::ScopedJavaGlobalRef<jobject>* j_callback);
     76 
     77   // Requests a server thumbnail from the |suggestions_service|.
     78   void GetSuggestionsThumbnailOnUIThread(
     79       suggestions::SuggestionsService* suggestions_service,
     80       const std::string& url_string,
     81       base::android::ScopedJavaGlobalRef<jobject>* j_callback);
     82 
     83   // Callback from the SuggestionsServer regarding the server thumbnail lookup.
     84   void OnSuggestionsThumbnailAvailable(
     85       base::android::ScopedJavaGlobalRef<jobject>* j_callback,
     86       const GURL& url,
     87       const SkBitmap* bitmap);
     88 
     89   // Records specific UMA histogram metrics.
     90   void RecordUMAMetrics();
     91 
     92   // The profile whose most visited sites will be queried.
     93   Profile* profile_;
     94 
     95   // The observer to be notified when the list of most visited sites changes.
     96   base::android::ScopedJavaGlobalRef<jobject> observer_;
     97 
     98   // The maximum number of most visited sites to return.
     99   int num_sites_;
    100 
    101   // Whether the user is in a control group for the purposes of logging.
    102   bool is_control_group_;
    103 
    104   // Counters for UMA metrics.
    105 
    106   // Number of tiles using a local thumbnail image for this NTP session.
    107   int num_local_thumbs_;
    108   // Number of tiles for which a server thumbnail is provided.
    109   int num_server_thumbs_;
    110   // Number of tiles for which no thumbnail is found/specified and a gray tile
    111   // is used as the main tile.
    112   int num_empty_thumbs_;
    113 
    114   // Copy of the server suggestions (if enabled). Used for logging.
    115   suggestions::SuggestionsProfile server_suggestions_;
    116 
    117   // For callbacks may be run after destruction.
    118   base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_;
    119 
    120   content::NotificationRegistrar registrar_;
    121 
    122   // The source of the Most Visited sites.
    123   enum MostVisitedSource {
    124     TOP_SITES,
    125     SUGGESTIONS_SERVICE
    126   };
    127   MostVisitedSource mv_source_;
    128 
    129   DISALLOW_COPY_AND_ASSIGN(MostVisitedSites);
    130 };
    131 
    132 #endif  // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_
    133