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 #include "session/internal/ime_context.h"
34 
35 #include "base/logging.h"
36 #include "composer/composer.h"
37 #include "config/config_handler.h"
38 #include "session/internal/keymap_factory.h"
39 #include "session/session_converter_interface.h"
40 
41 namespace mozc {
42 namespace session {
43 
44 using commands::Request;
45 
ImeContext()46 ImeContext::ImeContext()
47     : create_time_(0),
48       last_command_time_(0),
49       key_event_transformer_(new KeyEventTransformer),
50       state_(NONE),
51       request_(&Request::default_instance()),
52       config_(&config::ConfigHandler::DefaultConfig()),
53       keymap_(config::ConfigHandler::GetDefaultKeyMap()) {
54 }
~ImeContext()55 ImeContext::~ImeContext() {}
56 
composer() const57 const composer::Composer &ImeContext::composer() const {
58   DCHECK(composer_.get());
59   return *composer_;
60 }
mutable_composer()61 composer::Composer *ImeContext::mutable_composer() {
62   DCHECK(composer_.get());
63   return composer_.get();
64 }
set_composer(composer::Composer * composer)65 void ImeContext::set_composer(composer::Composer *composer) {
66   DCHECK(composer);
67   composer_.reset(composer);
68 }
69 
converter() const70 const SessionConverterInterface &ImeContext::converter() const {
71   return *converter_;
72 }
mutable_converter()73 SessionConverterInterface *ImeContext::mutable_converter() {
74   return converter_.get();
75 }
set_converter(SessionConverterInterface * converter)76 void ImeContext::set_converter(SessionConverterInterface *converter) {
77   converter_.reset(converter);
78 }
79 
SetRequest(const commands::Request * request)80 void ImeContext::SetRequest(const commands::Request *request) {
81   request_ = request;
82   converter_->SetRequest(request_);
83   composer_->SetRequest(request_);
84 }
85 
GetRequest() const86 const commands::Request &ImeContext::GetRequest() const {
87   DCHECK(request_);
88   return *request_;
89 }
90 
SetConfig(const config::Config * config)91 void ImeContext::SetConfig(const config::Config *config) {
92   config_ = config;
93 
94   DCHECK(converter_.get());
95   converter_->SetConfig(config_);
96 
97   DCHECK(composer_.get());
98   composer_->SetConfig(config_);
99 
100   DCHECK(key_event_transformer_.get());
101   key_event_transformer_->ReloadConfig(*config_);
102 
103   keymap_ = config->session_keymap();
104   keymap::KeyMapFactory::GetKeyMapManager(keymap_);
105   keymap::KeyMapFactory::ReloadConfig(*config_);
106 }
107 
GetConfig() const108 const config::Config &ImeContext::GetConfig() const {
109   DCHECK(config_);
110   return *config_;
111 }
112 
113 // static
CopyContext(const ImeContext & src,ImeContext * dest)114 void ImeContext::CopyContext(const ImeContext &src, ImeContext *dest) {
115   DCHECK(dest);
116 
117   dest->set_create_time(src.create_time());
118   dest->set_last_command_time(src.last_command_time());
119 
120   dest->mutable_composer()->CopyFrom(src.composer());
121   dest->converter_.reset(src.converter().Clone());
122   dest->key_event_transformer_->CopyFrom(*src.key_event_transformer_);
123 
124   dest->set_state(src.state());
125 
126   dest->SetRequest(src.request_);
127   dest->SetConfig(src.config_);
128 
129   dest->mutable_client_capability()->CopyFrom(src.client_capability());
130   dest->mutable_application_info()->CopyFrom(src.application_info());
131   dest->mutable_output()->CopyFrom(src.output());
132 }
133 
134 }  // namespace session
135 }  // namespace mozc
136