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_CONTENT_BRIDGE_H_
6 #define CHROME_BROWSER_ANDROID_FEED_FEED_CONTENT_BRIDGE_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/android/scoped_java_ref.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/feed/core/feed_content_database.h"
15 
16 namespace feed {
17 
18 class FeedContentDatabase;
19 
20 // Native counterpart of FeedContentBridge.java. Holds non-owning pointers
21 // to native implementation to which operations are delegated. Results are
22 // passed back by a single argument callback so
23 // base::android::RunBooleanCallbackAndroid() and
24 // base::android::RunObjectCallbackAndroid() can be used. This bridge is
25 // instantiated, owned, and destroyed from Java.
26 class FeedContentBridge {
27  public:
28   explicit FeedContentBridge(FeedContentDatabase* feed_Storage_database);
29   ~FeedContentBridge();
30 
31   void Destroy(JNIEnv* j_env, const base::android::JavaRef<jobject>& j_this);
32 
33   void LoadContent(JNIEnv* j_env,
34                    const base::android::JavaRef<jobject>& j_this,
35                    const base::android::JavaRef<jobjectArray>& j_keys,
36                    const base::android::JavaRef<jobject>& j_success_callback,
37                    const base::android::JavaRef<jobject>& j_failure_callback);
38   void LoadContentByPrefix(
39       JNIEnv* j_env,
40       const base::android::JavaRef<jobject>& j_this,
41       const base::android::JavaRef<jstring>& j_prefix,
42       const base::android::JavaRef<jobject>& j_success_callback,
43       const base::android::JavaRef<jobject>& j_failure_callback);
44   void LoadAllContentKeys(
45       JNIEnv* j_env,
46       const base::android::JavaRef<jobject>& j_this,
47       const base::android::JavaRef<jobject>& j_success_callback,
48       const base::android::JavaRef<jobject>& j_failure_callback);
49   void CommitContentMutation(JNIEnv* j_env,
50                              const base::android::JavaRef<jobject>& j_this,
51                              const base::android::JavaRef<jobject>& j_callback);
52 
53   void CreateContentMutation(JNIEnv* j_env,
54                              const base::android::JavaRef<jobject>& j_this);
55   void DeleteContentMutation(JNIEnv* j_env,
56                              const base::android::JavaRef<jobject>& j_this);
57   void AppendDeleteOperation(JNIEnv* j_env,
58                              const base::android::JavaRef<jobject>& j_this,
59                              const base::android::JavaRef<jstring>& j_key);
60   void AppendDeleteByPrefixOperation(
61       JNIEnv* j_env,
62       const base::android::JavaRef<jobject>& j_this,
63       const base::android::JavaRef<jstring>& j_prefix);
64   void AppendUpsertOperation(JNIEnv* j_env,
65                              const base::android::JavaRef<jobject>& j_this,
66                              const base::android::JavaRef<jstring>& j_key,
67                              const base::android::JavaRef<jbyteArray>& j_data);
68   void AppendDeleteAllOperation(JNIEnv* j_env,
69                                 const base::android::JavaRef<jobject>& j_this);
70 
71  private:
72   void OnLoadContentDone(
73       base::android::ScopedJavaGlobalRef<jobject> success_callback,
74       base::android::ScopedJavaGlobalRef<jobject> failure_callback,
75       bool success,
76       std::vector<FeedContentDatabase::KeyAndData> pairs);
77   void OnLoadAllContentKeysDone(
78       base::android::ScopedJavaGlobalRef<jobject> success_callback,
79       base::android::ScopedJavaGlobalRef<jobject> failure_callback,
80       bool success,
81       std::vector<std::string> keys);
82   void OnStorageCommitDone(base::android::ScopedJavaGlobalRef<jobject> callback,
83                            bool success);
84 
85   // This unique_ptr will hold a list of ContentOperations which are not
86   // committed yet. After commit to database, this unique_ptr will be reset.
87   std::unique_ptr<ContentMutation> content_mutation_;
88 
89   FeedContentDatabase* feed_content_database_;
90 
91   base::WeakPtrFactory<FeedContentBridge> weak_ptr_factory_{this};
92 
93   DISALLOW_COPY_AND_ASSIGN(FeedContentBridge);
94 };
95 
96 }  // namespace feed
97 
98 #endif  // CHROME_BROWSER_ANDROID_FEED_FEED_CONTENT_BRIDGE_H_
99