1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // ImeContext class contains the whole internal variables representing
31 // a session.
32 
33 #ifndef MOZC_SESSION_INTERNAL_IME_CONTEXT_H_
34 #define MOZC_SESSION_INTERNAL_IME_CONTEXT_H_
35 
36 #include <memory>
37 
38 #include "base/port.h"
39 #include "protocol/commands.pb.h"
40 #include "protocol/config.pb.h"
41 #include "session/internal/key_event_transformer.h"
42 
43 namespace mozc {
44 
45 namespace composer {
46 class Composer;
47 }  // namespace composer
48 
49 namespace session {
50 class SessionConverterInterface;
51 
52 class ImeContext {
53  public:
54   ImeContext();
55   virtual ~ImeContext();
56 
create_time()57   uint64 create_time() const {
58     return create_time_;
59   }
set_create_time(uint64 create_time)60   void set_create_time(uint64 create_time) {
61     create_time_ = create_time;
62   }
63 
last_command_time()64   uint64 last_command_time() const {
65     return last_command_time_;
66   }
set_last_command_time(uint64 last_command_time)67   void set_last_command_time(uint64 last_command_time) {
68     last_command_time_ = last_command_time;
69   }
70 
71   // Note that before using getter methods,
72   // |composer_| must be set non-null value.
73   const composer::Composer &composer() const;
74   composer::Composer *mutable_composer();
75   void set_composer(composer::Composer *composer);
76 
77   const SessionConverterInterface &converter() const;
78   SessionConverterInterface *mutable_converter();
79   void set_converter(SessionConverterInterface *converter);
80 
key_event_transformer()81   const KeyEventTransformer &key_event_transformer() const {
82     return *key_event_transformer_;
83   }
84 
85   enum State {
86     NONE = 0,
87     DIRECT = 1,
88     PRECOMPOSITION = 2,
89     COMPOSITION = 4,
90     CONVERSION = 8,
91   };
state()92   State state() const {
93     return state_;
94   }
set_state(State state)95   void set_state(State state) {
96     state_ = state;
97   }
98 
99   // Returns the current keymap.  This might be temporary and different from
100   // the keymap in the config.
keymap()101   config::Config::SessionKeymap keymap() const {
102     return keymap_;
103   }
set_keymap(config::Config::SessionKeymap keymap)104   void set_keymap(config::Config::SessionKeymap keymap) {
105     keymap_ = keymap;
106   }
107 
108   void SetRequest(const commands::Request *request);
109   const commands::Request &GetRequest() const;
110 
111   void SetConfig(const config::Config *config);
112   const config::Config &GetConfig() const;
113 
client_capability()114   const commands::Capability &client_capability() const {
115     return client_capability_;
116   }
mutable_client_capability()117   commands::Capability *mutable_client_capability() {
118     return &client_capability_;
119   }
120 
application_info()121   const commands::ApplicationInfo &application_info() const {
122     return application_info_;
123   }
mutable_application_info()124   commands::ApplicationInfo *mutable_application_info() {
125     return &application_info_;
126   }
127 
128   // Note that this may not be the latest info: this is likely to be a snapshot
129   // of during the precomposition state and may not be updated during
130   // composition/conversion state.
client_context()131   const commands::Context &client_context() const {
132     return client_context_;
133   }
mutable_client_context()134   commands::Context *mutable_client_context() {
135     return &client_context_;
136   }
137 
output()138   const commands::Output &output() const {
139     return output_;
140   }
mutable_output()141   commands::Output *mutable_output() {
142     return &output_;
143   }
144 
145   // Copy |source| context to |destination| context.
146   // TODO(hsumita): Renames it as CopyFrom and make it non-static to keep
147   // consistency with other classes.
148   static void CopyContext(const ImeContext &src, ImeContext *dest);
149 
150  private:
151   // TODO(team): Actual use of |create_time_| is to keep the time when the
152   // session holding this instance is created and not the time when this
153   // instance is created. We may want to move out |create_time_| from ImeContext
154   // to Session, or somewhere more appropriate.
155   uint64 create_time_;
156   uint64 last_command_time_;
157 
158   std::unique_ptr<composer::Composer> composer_;
159 
160   std::unique_ptr<SessionConverterInterface> converter_;
161 
162   std::unique_ptr<KeyEventTransformer> key_event_transformer_;
163 
164   State state_;
165 
166   const commands::Request *request_;
167   const config::Config *config_;
168 
169   config::Config::SessionKeymap keymap_;
170 
171   commands::Capability client_capability_;
172 
173   commands::ApplicationInfo application_info_;
174 
175   commands::Context client_context_;
176 
177   // Storing the last output consisting of the last result and the
178   // last performed command.
179   commands::Output output_;
180 
181   DISALLOW_COPY_AND_ASSIGN(ImeContext);
182 };
183 
184 }  // namespace session
185 }  // namespace mozc
186 
187 #endif  // MOZC_SESSION_INTERNAL_IME_CONTEXT_H_
188