1 // Copyright 2018 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_FEED_FEED_SCHEDULER_BRIDGE_H_
6 #define CHROME_BROWSER_ANDROID_FEED_FEED_SCHEDULER_BRIDGE_H_
7 
8 #include <jni.h>
9 #include <stdint.h>
10 
11 #include "base/android/scoped_java_ref.h"
12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/feed/core/feed_scheduler_host.h"
15 
16 namespace base {
17 class TimeDelta;
18 }  // namespace base
19 
20 namespace feed {
21 
22 class FeedSchedulerHost;
23 
24 // Native counterpart of FeedSchedulerBridge.java. Holds non-owning pointers to
25 // native implementation, to which operations are delegated. Also capable of
26 // calling back into Java half.
27 class FeedSchedulerBridge {
28  public:
29   FeedSchedulerBridge(const base::android::JavaRef<jobject>& j_this,
30                       FeedSchedulerHost* scheduler_host);
31   ~FeedSchedulerBridge();
32 
33   void Destroy(JNIEnv* env, const base::android::JavaRef<jobject>& j_this);
34 
35   jint ShouldSessionRequestData(JNIEnv* env,
36                                 const base::android::JavaRef<jobject>& j_this,
37                                 const jboolean j_has_content,
38                                 const jlong j_content_creation_date_time_ms,
39                                 const jboolean j_has_outstanding_request);
40 
41   void OnReceiveNewContent(JNIEnv* env,
42                            const base::android::JavaRef<jobject>& j_this,
43                            const jlong j_content_creation_date_time_ms);
44 
45   void OnRequestError(JNIEnv* env,
46                       const base::android::JavaRef<jobject>& j_this,
47                       jint j_network_response_code);
48 
49   void OnForegrounded(JNIEnv* env,
50                       const base::android::JavaRef<jobject>& j_this);
51 
52   void OnFixedTimer(JNIEnv* env,
53                     const base::android::JavaRef<jobject>& j_this,
54                     const base::android::JavaRef<jobject>& j_callback);
55 
56   void OnSuggestionConsumed(JNIEnv* env,
57                             const base::android::JavaRef<jobject>& j_this);
58 
59   bool OnArticlesCleared(JNIEnv* env,
60                          const base::android::JavaRef<jobject>& j_this,
61                          jboolean j_suppress_refreshes);
62 
63  private:
64   // Callable by native code to invoke Java code. Sends a request to the Feed
65   // library to make the refresh call.
66   void TriggerRefresh();
67 
68   // Calls into Java logic to schedule a background task to wake up Chrome and
69   // call back into the FeedHostScheduler.
70   void ScheduleWakeUp(base::TimeDelta threshold_ms);
71 
72   // Cancels previously scheduled background task.
73   void CancelWakeUp();
74 
75   void FixedTimerHandlingDone(
76       base::android::ScopedJavaGlobalRef<jobject> j_callback);
77 
78   // Reference to the Java half of this bridge. Always valid.
79   base::android::ScopedJavaGlobalRef<jobject> j_this_;
80 
81   // Object to which all Java to native calls are delegated.
82   FeedSchedulerHost* scheduler_host_;
83 
84   base::WeakPtrFactory<FeedSchedulerBridge> weak_factory_{this};
85 
86   DISALLOW_COPY_AND_ASSIGN(FeedSchedulerBridge);
87 };
88 
89 }  // namespace feed
90 
91 #endif  // CHROME_BROWSER_ANDROID_FEED_FEED_SCHEDULER_BRIDGE_H_
92