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