• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBTEXTCLASSIFIER_UTILS_INTENTS_JNI_LUA_H_
18 #define LIBTEXTCLASSIFIER_UTILS_INTENTS_JNI_LUA_H_
19 
20 #include <map>
21 #include <vector>
22 
23 #include "utils/base/statusor.h"
24 #include "utils/i18n/locale.h"
25 #include "utils/intents/remote-action-template.h"
26 #include "utils/java/jni-base.h"
27 #include "utils/java/jni-cache.h"
28 #include "utils/lua-utils.h"
29 #include "utils/resources.h"
30 #include "utils/strings/stringpiece.h"
31 #include "utils/variant.h"
32 
33 namespace libtextclassifier3 {
34 
35 // An Android specific Lua environment with JNI backed callbacks.
36 class JniLuaEnvironment : public LuaEnvironment {
37  public:
38   JniLuaEnvironment(const Resources& resources, const JniCache* jni_cache,
39                     const jobject context,
40                     const std::vector<Locale>& device_locales);
41   // Environment setup.
42   bool Initialize();
43 
44   // Runs an intent generator snippet.
45   bool RunIntentGenerator(const std::string& generator_snippet,
46                           std::vector<RemoteActionTemplate>* remote_actions);
47 
48  protected:
49   virtual void SetupExternalHook();
50   bool PreallocateConstantJniStrings();
51 
52   int HandleExternalCallback();
53   int HandleAndroidCallback();
54   int HandleUserRestrictionsCallback();
55   int HandleUrlEncode();
56   int HandleUrlSchema();
57   int HandleHash();
58   int HandleFormat();
59   int HandleAndroidStringResources();
60   int HandleUrlHost();
61 
62   // Checks and retrieves string resources from the model.
63   bool LookupModelStringResource() const;
64 
65   // Reads and create a RemoteAction result from Lua.
66   RemoteActionTemplate ReadRemoteActionTemplateResult() const;
67 
68   // Reads the extras from the Lua result.
69   std::map<std::string, Variant> ReadExtras() const;
70 
71   // Retrieves user manager if not previously done.
72   bool RetrieveUserManager();
73 
74   // Retrieves system resources if not previously done.
75   bool RetrieveSystemResources();
76 
77   // Parse the url string by using Uri.parse from Java.
78   StatusOr<ScopedLocalRef<jobject>> ParseUri(StringPiece url) const;
79 
80   // Read remote action templates from lua generator.
81   int ReadRemoteActionTemplates(std::vector<RemoteActionTemplate>* result);
82 
83   const Resources& resources_;
84   JNIEnv* jenv_;
85   const JniCache* jni_cache_;
86   const jobject context_;
87   std::vector<Locale> device_locales_;
88 
89   ScopedGlobalRef<jobject> usermanager_;
90   // Whether we previously attempted to retrieve the UserManager before.
91   bool usermanager_retrieved_;
92 
93   ScopedGlobalRef<jobject> system_resources_;
94   // Whether we previously attempted to retrieve the system resources.
95   bool system_resources_resources_retrieved_;
96 
97   // Cached JNI references for Java strings `string` and `android`.
98   ScopedGlobalRef<jstring> string_;
99   ScopedGlobalRef<jstring> android_;
100 };
101 
102 }  // namespace libtextclassifier3
103 
104 #endif  // LIBTEXTCLASSIFIER_UTILS_INTENTS_JNI_LUA_H_
105