Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / android / most_visited_sites.h
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   // The source of the Most Visited sites.
60   enum MostVisitedSource {
61     TOP_SITES,
62     SUGGESTIONS_SERVICE
63   };
64
65   virtual ~MostVisitedSites();
66   void QueryMostVisitedURLs();
67
68   // Initialize the query to Top Sites. Called if the SuggestionsService is not
69   // enabled, or if it returns no data.
70   void InitiateTopSitesQuery();
71
72   // Callback for when data is available from TopSites.
73   void OnMostVisitedURLsAvailable(
74       base::android::ScopedJavaGlobalRef<jobject>* j_observer,
75       int num_sites,
76       const history::MostVisitedURLList& visited_list);
77
78   // Callback for when data is available from the SuggestionsService.
79   void OnSuggestionsProfileAvailable(
80       base::android::ScopedJavaGlobalRef<jobject>* j_observer,
81       const suggestions::SuggestionsProfile& suggestions_profile);
82
83   // Callback for when the local thumbnail lookup is complete.
84   void OnObtainedThumbnail(
85       base::android::ScopedJavaGlobalRef<jobject>* bitmap,
86       base::android::ScopedJavaGlobalRef<jobject>* j_callback);
87
88   // Requests a server thumbnail from the |suggestions_service|.
89   void GetSuggestionsThumbnailOnUIThread(
90       suggestions::SuggestionsService* suggestions_service,
91       const std::string& url_string,
92       base::android::ScopedJavaGlobalRef<jobject>* j_callback);
93
94   // Callback from the SuggestionsServer regarding the server thumbnail lookup.
95   void OnSuggestionsThumbnailAvailable(
96       base::android::ScopedJavaGlobalRef<jobject>* j_callback,
97       const GURL& url,
98       const SkBitmap* bitmap);
99
100   // Records specific UMA histogram metrics.
101   void RecordUMAMetrics();
102
103   // The profile whose most visited sites will be queried.
104   Profile* profile_;
105
106   // The observer to be notified when the list of most visited sites changes.
107   base::android::ScopedJavaGlobalRef<jobject> observer_;
108
109   // The maximum number of most visited sites to return.
110   int num_sites_;
111
112   // Whether the user is in a control group for the purposes of logging.
113   bool is_control_group_;
114
115   // Keeps track of whether the initial NTP load has been done.
116   bool initial_load_done_;
117
118   // Counters for UMA metrics.
119
120   // Number of tiles using a local thumbnail image for this NTP session.
121   int num_local_thumbs_;
122   // Number of tiles for which a server thumbnail is provided.
123   int num_server_thumbs_;
124   // Number of tiles for which no thumbnail is found/specified and a gray tile
125   // is used as the main tile.
126   int num_empty_thumbs_;
127
128   // Copy of the server suggestions (if enabled). Used for logging.
129   suggestions::SuggestionsProfile server_suggestions_;
130
131   content::NotificationRegistrar registrar_;
132
133   MostVisitedSource mv_source_;
134
135   // For callbacks may be run after destruction.
136   base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_;
137
138   DISALLOW_COPY_AND_ASSIGN(MostVisitedSites);
139 };
140
141 #endif  // CHROME_BROWSER_ANDROID_MOST_VISITED_SITES_H_