Update from the upstream.
* Code refactoring.
Hiroyuki Komatsu
3 years ago
145 | 145 | oss_android = ["//android/jni:mozc_lib"], |
146 | 146 | oss_linux = [ |
147 | 147 | "//gui/tool:mozc_tool", |
148 | "//unix/emacs:mozc_emacs_helper", | |
149 | "//unix/ibus:ibus_mozc", | |
148 | 150 | "//renderer:mozc_renderer", |
149 | 151 | "//server:mozc_server", |
150 | "//unix/ibus:ibus_mozc", | |
151 | 152 | ], |
152 | 153 | ), |
153 | 154 | ) |
68 | 68 | } |
69 | 69 | #elif defined(OS_NACL) |
70 | 70 | const time_t localtime_sec = modified_sec + timezone_offset_sec_; |
71 | if (gmtime_r(&localtime_sec, output) == NULL) { | |
71 | if (gmtime_r(&localtime_sec, output) == nullptr) { | |
72 | 72 | return false; |
73 | 73 | } |
74 | 74 | #else // !OS_WIN && !OS_NACL |
75 | if (gmtime_r(&modified_sec, output) == NULL) { | |
75 | if (gmtime_r(&modified_sec, output) == nullptr) { | |
76 | 76 | return false; |
77 | 77 | } |
78 | 78 | #endif |
210 | 210 | // "Compiler warning C4355: 'this': used in base member initializer list" |
211 | 211 | // http://msdn.microsoft.com/en-us/library/3c594ae3.aspx |
212 | 212 | // example: |
213 | // Foo::Foo() : x(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(y(this)) {} | |
213 | // Foo::Foo() : x(nullptr), ALLOW_THIS_IN_INITIALIZER_LIST(y(this)) {} | |
214 | 214 | #if defined(_MSC_VER) |
215 | 215 | #define ALLOW_THIS_IN_INITIALIZER_LIST(code) \ |
216 | 216 | __pragma(warning(push)) __pragma(warning(disable : 4355)) \ |
103 | 103 | return ifs; |
104 | 104 | } |
105 | 105 | delete ifs; |
106 | return NULL; | |
106 | return nullptr; | |
107 | 107 | } |
108 | 108 | } |
109 | 109 | // user://foo.bar.txt |
117 | 117 | return ifs; |
118 | 118 | } |
119 | 119 | delete ifs; |
120 | return NULL; | |
120 | return nullptr; | |
121 | 121 | // file:///foo.map |
122 | 122 | } else if (Util::StartsWith(filename, kFilePrefix)) { |
123 | 123 | const std::string new_filename = RemovePrefix(kFilePrefix, filename); |
127 | 127 | return ifs; |
128 | 128 | } |
129 | 129 | delete ifs; |
130 | return NULL; | |
130 | return nullptr; | |
131 | 131 | } else if (Util::StartsWith(filename, kMemoryPrefix)) { |
132 | 132 | std::istringstream *ifs = new std::istringstream( |
133 | 133 | Singleton<OnMemoryFileMap>::get()->get(filename), mode); |
136 | 136 | return ifs; |
137 | 137 | } |
138 | 138 | delete ifs; |
139 | return NULL; | |
139 | return nullptr; | |
140 | 140 | } else { |
141 | 141 | LOG(WARNING) << filename << " has no prefix. open from localfile"; |
142 | 142 | InputFileStream *ifs = new InputFileStream(filename.c_str(), mode); |
145 | 145 | return ifs; |
146 | 146 | } |
147 | 147 | delete ifs; |
148 | return NULL; | |
149 | } | |
150 | ||
151 | return NULL; | |
148 | return nullptr; | |
149 | } | |
150 | ||
151 | return nullptr; | |
152 | 152 | } |
153 | 153 | |
154 | 154 | bool ConfigFileStream::AtomicUpdate(const std::string &filename, |
168 | 168 | // OSX's basic_info_t has no filed |creation_time|, we cannot use it. |
169 | 169 | // The initial value might be different from the real CPU load. |
170 | 170 | struct timeval tv; |
171 | gettimeofday(&tv, NULL); | |
171 | gettimeofday(&tv, nullptr); | |
172 | 172 | |
173 | 173 | const uint64 total_times = 1000000ULL * tv.tv_sec + tv.tv_usec; |
174 | 174 | const uint64 cpu_times = TimeValueTToInt64(task_times_info.user_time) + |
77 | 77 | |
78 | 78 | // The CRITICAL_SECTION struct used for creating or deleting ExceptionHandler in |
79 | 79 | // a mutually exclusive manner. |
80 | CRITICAL_SECTION *g_critical_section = NULL; | |
81 | ||
82 | google_breakpad::ExceptionHandler *g_handler = NULL; | |
80 | CRITICAL_SECTION *g_critical_section = nullptr; | |
81 | ||
82 | google_breakpad::ExceptionHandler *g_handler = nullptr; | |
83 | 83 | |
84 | 84 | // Returns the name of the build mode. |
85 | 85 | std::wstring GetBuildMode() { |
93 | 93 | } |
94 | 94 | |
95 | 95 | // Reduces the size of the string |str| to a max of 64 chars (Extra 1 char is |
96 | // trimmed for NULL-terminator so effective characters are 63 characters). | |
96 | // trimmed for nullptr-terminator so effective characters are 63 characters). | |
97 | 97 | // Required because breakpad's CustomInfoEntry raises an invalid_parameter error |
98 | 98 | // if the string we want to set is longer than 64 characters, including |
99 | // NULL-terminator. | |
99 | // nullptr-terminator. | |
100 | 100 | std::wstring TrimToBreakpadMax(const std::wstring &str) { |
101 | 101 | std::wstring shorter(str); |
102 | 102 | return shorter.substr(0, |
119 | 119 | { |
120 | 120 | int num_args = 0; |
121 | 121 | wchar_t **args = ::CommandLineToArgvW(::GetCommandLineW(), &num_args); |
122 | if (args != NULL) { | |
122 | if (args != nullptr) { | |
123 | 123 | if (num_args > 1) { |
124 | 124 | switch1.set_value(TrimToBreakpadMax(args[1]).c_str()); |
125 | 125 | } |
152 | 152 | public: |
153 | 153 | explicit ScopedCriticalSection(CRITICAL_SECTION *critical_section) |
154 | 154 | : critical_section_(critical_section) { |
155 | if (critical_section_ != NULL) { | |
155 | if (critical_section_ != nullptr) { | |
156 | 156 | EnterCriticalSection(critical_section_); |
157 | 157 | } |
158 | 158 | } |
159 | 159 | ~ScopedCriticalSection() { |
160 | if (critical_section_ != NULL) { | |
160 | if (critical_section_ != nullptr) { | |
161 | 161 | LeaveCriticalSection(critical_section_); |
162 | 162 | } |
163 | 163 | } |
168 | 168 | |
169 | 169 | // get the handle to the module containing the given executing address |
170 | 170 | HMODULE GetModuleHandleFromAddress(void *address) { |
171 | // address may be NULL | |
171 | // address may be nullptr | |
172 | 172 | MEMORY_BASIC_INFORMATION mbi; |
173 | 173 | SIZE_T result = VirtualQuery(address, &mbi, sizeof(mbi)); |
174 | 174 | if (0 == result) { |
175 | return NULL; | |
175 | return nullptr; | |
176 | 176 | } |
177 | 177 | return static_cast<HMODULE>(mbi.AllocationBase); |
178 | 178 | } |
184 | 184 | |
185 | 185 | // Check to see if an address is in the current module. |
186 | 186 | bool IsAddressInCurrentModule(void *address) { |
187 | // address may be NULL | |
187 | // address may be nullptr | |
188 | 188 | return GetCurrentModuleHandle() == GetModuleHandleFromAddress(address); |
189 | 189 | } |
190 | 190 | |
225 | 225 | |
226 | 226 | bool FilterHandler(void *context, EXCEPTION_POINTERS *exinfo, |
227 | 227 | MDRawAssertionInfo *assertion) { |
228 | if (exinfo == NULL) { | |
228 | if (exinfo == nullptr) { | |
229 | 229 | // We do not catch CRT error in release build. |
230 | 230 | #ifdef MOZC_NO_LOGGING |
231 | 231 | return false; |
254 | 254 | ScopedCriticalSection critical_section(g_critical_section); |
255 | 255 | DCHECK_GE(g_reference_count, 0); |
256 | 256 | ++g_reference_count; |
257 | if (g_reference_count == 1 && g_handler == NULL) { | |
257 | if (g_reference_count == 1 && g_handler == nullptr) { | |
258 | 258 | const string acrashdump_directory = SystemUtil::GetCrashReportDirectory(); |
259 | 259 | // create a crash dump directory if not exist. |
260 | 260 | if (!FileUtil::FileExists(acrashdump_directory)) { |
265 | 265 | Util::UTF8ToWide(acrashdump_directory, &crashdump_directory); |
266 | 266 | |
267 | 267 | google_breakpad::ExceptionHandler::FilterCallback filter_callback = |
268 | check_address ? FilterHandler : NULL; | |
268 | check_address ? FilterHandler : nullptr; | |
269 | 269 | const auto kCrashDumpType = static_cast<MINIDUMP_TYPE>( |
270 | 270 | MiniDumpWithUnloadedModules | MiniDumpWithProcessThreadData); |
271 | 271 | g_handler = new google_breakpad::ExceptionHandler( |
272 | 272 | crashdump_directory.c_str(), filter_callback, |
273 | NULL, // MinidumpCallback | |
274 | NULL, // callback_context | |
273 | nullptr, // MinidumpCallback | |
274 | nullptr, // callback_context | |
275 | 275 | google_breakpad::ExceptionHandler::HANDLER_ALL, kCrashDumpType, |
276 | 276 | GetCrashHandlerPipeName().c_str(), GetCustomInfo()); |
277 | 277 | |
292 | 292 | ScopedCriticalSection critical_section(g_critical_section); |
293 | 293 | --g_reference_count; |
294 | 294 | DCHECK_GE(g_reference_count, 0); |
295 | if (g_reference_count == 0 && g_handler != NULL) { | |
295 | if (g_reference_count == 0 && g_handler != nullptr) { | |
296 | 296 | delete g_handler; |
297 | g_handler = NULL; | |
297 | g_handler = nullptr; | |
298 | 298 | return true; |
299 | 299 | } |
300 | 300 | return false; |
161 | 161 | return false; |
162 | 162 | } |
163 | 163 | |
164 | if (iv != NULL) { | |
164 | if (iv != nullptr) { | |
165 | 165 | memcpy(data_->iv, iv, iv_size()); |
166 | 166 | } else { |
167 | 167 | memset(data_->iv, '\0', iv_size()); |
184 | 184 | Encryptor::Key::~Key() {} |
185 | 185 | |
186 | 186 | bool Encryptor::EncryptString(const Encryptor::Key &key, std::string *data) { |
187 | if (data == NULL || data->empty()) { | |
188 | LOG(ERROR) << "data is NULL or empty"; | |
187 | if (data == nullptr || data->empty()) { | |
188 | LOG(ERROR) << "data is nullptr or empty"; | |
189 | 189 | return false; |
190 | 190 | } |
191 | 191 | size_t size = data->size(); |
200 | 200 | } |
201 | 201 | |
202 | 202 | bool Encryptor::DecryptString(const Encryptor::Key &key, std::string *data) { |
203 | if (data == NULL || data->empty()) { | |
204 | LOG(ERROR) << "data is NULL or empty"; | |
203 | if (data == nullptr || data->empty()) { | |
204 | LOG(ERROR) << "data is nullptr or empty"; | |
205 | 205 | return false; |
206 | 206 | } |
207 | 207 | size_t size = data->size(); |
222 | 222 | return false; |
223 | 223 | } |
224 | 224 | |
225 | if (buf == NULL || buf_size == NULL || *buf_size == 0) { | |
225 | if (buf == nullptr || buf_size == nullptr || *buf_size == 0) { | |
226 | 226 | LOG(ERROR) << "invalid buffer given"; |
227 | 227 | return false; |
228 | 228 | } |
251 | 251 | return false; |
252 | 252 | } |
253 | 253 | |
254 | if (buf == NULL || buf_size == NULL || *buf_size == 0) { | |
254 | if (buf == nullptr || buf_size == nullptr || *buf_size == 0) { | |
255 | 255 | LOG(ERROR) << "invalid buffer given"; |
256 | 256 | return false; |
257 | 257 | } |
303 | 303 | input.cbData = static_cast<DWORD>(plain_text.size()); |
304 | 304 | |
305 | 305 | DATA_BLOB output; |
306 | const BOOL result = ::CryptProtectData(&input, L"", NULL, NULL, NULL, | |
306 | const BOOL result = ::CryptProtectData(&input, L"", nullptr, nullptr, nullptr, | |
307 | 307 | CRYPTPROTECT_UI_FORBIDDEN, &output); |
308 | 308 | if (!result) { |
309 | 309 | LOG(ERROR) << "CryptProtectData failed: " << ::GetLastError(); |
326 | 326 | input.cbData = static_cast<DWORD>(cipher_text.length()); |
327 | 327 | |
328 | 328 | DATA_BLOB output; |
329 | const BOOL result = ::CryptUnprotectData(&input, NULL, NULL, NULL, NULL, | |
330 | CRYPTPROTECT_UI_FORBIDDEN, &output); | |
329 | const BOOL result = | |
330 | ::CryptUnprotectData(&input, nullptr, nullptr, nullptr, nullptr, | |
331 | CRYPTPROTECT_UI_FORBIDDEN, &output); | |
331 | 332 | if (!result) { |
332 | 333 | LOG(ERROR) << "CryptUnprotectData failed: " << ::GetLastError(); |
333 | 334 | return false; |
44 | 44 | // Make a session key from password and salt. |
45 | 45 | // You can also set an initialization vector whose |
46 | 46 | // size must be iv_size(). |
47 | // if iv is NULL, default iv is used. | |
47 | // if iv is nullptr, default iv is used. | |
48 | 48 | bool DeriveFromPassword(const std::string &password, |
49 | 49 | const std::string &salt, const uint8 *iv); |
50 | 50 | |
51 | 51 | // use default iv. |
52 | 52 | bool DeriveFromPassword(const std::string &password, |
53 | 53 | const std::string &salt) { |
54 | return DeriveFromPassword(password, salt, NULL); | |
54 | return DeriveFromPassword(password, salt, nullptr); | |
55 | 55 | } |
56 | 56 | |
57 | 57 | // use empty salt and default iv |
58 | 58 | bool DeriveFromPassword(const std::string &password) { |
59 | return DeriveFromPassword(password, "", NULL); | |
59 | return DeriveFromPassword(password, "", nullptr); | |
60 | 60 | } |
61 | 61 | |
62 | 62 | // return block size. the result should be 16byte with AES |
68 | 68 | } |
69 | 69 | |
70 | 70 | const uint8 *iv = FLAGS_iv.empty() |
71 | ? NULL | |
71 | ? nullptr | |
72 | 72 | : reinterpret_cast<const uint8 *>(FLAGS_iv.data()); |
73 | 73 | |
74 | 74 | if (!FLAGS_input_file.empty() && !FLAGS_output_file.empty()) { |
212 | 212 | EXPECT_NE(original, encrypted4); |
213 | 213 | |
214 | 214 | // invalid values |
215 | EXPECT_FALSE(Encryptor::EncryptString(key1, NULL)); | |
216 | EXPECT_FALSE(Encryptor::DecryptString(key1, NULL)); | |
215 | EXPECT_FALSE(Encryptor::EncryptString(key1, nullptr)); | |
216 | EXPECT_FALSE(Encryptor::DecryptString(key1, nullptr)); | |
217 | 217 | |
218 | 218 | // empty string |
219 | 219 | std::string empty_string; |
48 | 48 | InputMultiFile::~InputMultiFile() { ifs_.reset(); } |
49 | 49 | |
50 | 50 | bool InputMultiFile::ReadLine(std::string *line) { |
51 | if (ifs_.get() == NULL) { | |
51 | if (ifs_.get() == nullptr) { | |
52 | 52 | return false; |
53 | 53 | } |
54 | 54 | do { |
182 | 182 | } // namespace |
183 | 183 | |
184 | 184 | ReaderWriterMutex::ReaderWriterMutex() { |
185 | pthread_rwlock_init(AsPthreadRWLockT(&opaque_buffer_), NULL); | |
185 | pthread_rwlock_init(AsPthreadRWLockT(&opaque_buffer_), nullptr); | |
186 | 186 | } |
187 | 187 | |
188 | 188 | ReaderWriterMutex::~ReaderWriterMutex() { |
212 | 212 | // Fallback implementations as ReaderWriterLock is not available. |
213 | 213 | ReaderWriterMutex::ReaderWriterMutex() { |
214 | 214 | // Non-recursive lock is OK. |
215 | pthread_mutex_init(AsPthreadMutexT(&opaque_buffer_), NULL); | |
215 | pthread_mutex_init(AsPthreadMutexT(&opaque_buffer_), nullptr); | |
216 | 216 | } |
217 | 217 | |
218 | 218 | ReaderWriterMutex::~ReaderWriterMutex() { |
179 | 179 | } |
180 | 180 | |
181 | 181 | bool PlainPasswordManager::GetPassword(std::string *password) const { |
182 | if (password == NULL) { | |
183 | LOG(ERROR) << "password is NULL"; | |
182 | if (password == nullptr) { | |
183 | LOG(ERROR) << "password is nullptr"; | |
184 | 184 | return false; |
185 | 185 | } |
186 | 186 | |
230 | 230 | } |
231 | 231 | |
232 | 232 | bool WinMacPasswordManager::GetPassword(string *password) const { |
233 | if (password == NULL) { | |
234 | LOG(ERROR) << "password is NULL"; | |
233 | if (password == nullptr) { | |
234 | LOG(ERROR) << "password is nullptr"; | |
235 | 235 | return false; |
236 | 236 | } |
237 | 237 | |
278 | 278 | public: |
279 | 279 | PasswordManagerImpl() { |
280 | 280 | password_manager_ = Singleton<DefaultPasswordManager>::get(); |
281 | DCHECK(password_manager_ != NULL); | |
281 | DCHECK(password_manager_ != nullptr); | |
282 | 282 | } |
283 | 283 | |
284 | 284 | bool InitPassword() { |
74 | 74 | #include <crt_externs.h> |
75 | 75 | static char **environ = *_NSGetEnviron(); |
76 | 76 | #elif !defined(OS_WIN) |
77 | // Defined somewhere in libc. We can't pass NULL as the 6th argument of | |
77 | // Defined somewhere in libc. We can't pass nullptr as the 6th argument of | |
78 | 78 | // posix_spawn() since Qt applications use (at least) DISPLAY and QT_IM_MODULE |
79 | 79 | // environment variables. |
80 | 80 | extern char **environ; |
133 | 133 | si.dwFlags = STARTF_FORCEOFFFEEDBACK; |
134 | 134 | PROCESS_INFORMATION pi = {0}; |
135 | 135 | |
136 | // If both |lpApplicationName| and |lpCommandLine| are non-NULL, | |
136 | // If both |lpApplicationName| and |lpCommandLine| are non-nullptr, | |
137 | 137 | // the argument array of the process will be shifted. |
138 | 138 | // http://support.microsoft.com/kb/175986 |
139 | 139 | const bool create_process_succeeded = |
140 | 140 | ::CreateProcessW( |
141 | NULL, wpath2.get(), NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, | |
142 | NULL, | |
141 | nullptr, wpath2.get(), nullptr, nullptr, FALSE, | |
142 | CREATE_DEFAULT_ERROR_MODE, nullptr, | |
143 | 143 | // NOTE: Working directory will be locked by the system. |
144 | 144 | // We use system directory to spawn process so that users will not |
145 | 145 | // to be aware of any undeletable directory. (http://b/2017482) |
165 | 165 | for (size_t i = 0; i < arg_tmp.size(); ++i) { |
166 | 166 | argv[i + 1] = arg_tmp[i].c_str(); |
167 | 167 | } |
168 | argv[arg_tmp.size() + 1] = NULL; | |
168 | argv[arg_tmp.size() + 1] = nullptr; | |
169 | 169 | |
170 | 170 | struct stat statbuf; |
171 | 171 | if (::stat(path.c_str(), &statbuf) != 0) { |
214 | 214 | // the return value of posix_spawn is basically determined |
215 | 215 | // by the return value of fork(). |
216 | 216 | const int result = |
217 | ::posix_spawn(&tmp_pid, path.c_str(), NULL, NULL, | |
217 | ::posix_spawn(&tmp_pid, path.c_str(), nullptr, nullptr, | |
218 | 218 | const_cast<char *const *>(argv.get()), environ); |
219 | 219 | if (result == 0) { |
220 | 220 | VLOG(1) << "posix_spawn: child pid is " << tmp_pid; |
222 | 222 | LOG(ERROR) << "posix_spawn failed: " << strerror(result); |
223 | 223 | } |
224 | 224 | |
225 | if (pid != NULL) { | |
225 | if (pid != nullptr) { | |
226 | 226 | *pid = tmp_pid; |
227 | 227 | } |
228 | 228 | return result == 0; |
249 | 249 | #ifdef OS_WIN |
250 | 250 | DWORD processe_id = static_cast<DWORD>(pid); |
251 | 251 | ScopedHandle handle(::OpenProcess(SYNCHRONIZE, FALSE, processe_id)); |
252 | if (handle.get() == NULL) { | |
252 | if (handle.get() == nullptr) { | |
253 | 253 | LOG(ERROR) << "Cannot get handle id"; |
254 | 254 | return true; |
255 | 255 | } |
299 | 299 | { |
300 | 300 | ScopedHandle handle( |
301 | 301 | ::OpenProcess(SYNCHRONIZE, FALSE, static_cast<DWORD>(pid))); |
302 | if (NULL == handle.get()) { | |
302 | if (nullptr == handle.get()) { | |
303 | 303 | const DWORD error = ::GetLastError(); |
304 | 304 | if (error == ERROR_ACCESS_DENIED) { |
305 | 305 | LOG(ERROR) << "OpenProcess failed: " << error; |
340 | 340 | { |
341 | 341 | ScopedHandle handle( |
342 | 342 | ::OpenThread(SYNCHRONIZE, FALSE, static_cast<DWORD>(thread_id))); |
343 | if (NULL == handle.get()) { | |
343 | if (nullptr == handle.get()) { | |
344 | 344 | const DWORD error = ::GetLastError(); |
345 | 345 | if (error == ERROR_ACCESS_DENIED) { |
346 | 346 | LOG(ERROR) << "OpenThread failed: " << error; |
57 | 57 | namespace { |
58 | 58 | |
59 | 59 | std::string CreateProcessMutexFileName(const char *name) { |
60 | name = (name == NULL) ? "NULL" : name; | |
60 | name = (name == nullptr) ? "nullptr" : name; | |
61 | 61 | |
62 | 62 | #ifdef OS_WIN |
63 | 63 | string basename; |
102 | 102 | } |
103 | 103 | handle_.reset(::CreateFileW(wfilename.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
104 | 104 | &serucity_attributes, CREATE_ALWAYS, kAttribute, |
105 | NULL)); | |
105 | nullptr)); | |
106 | 106 | ::LocalFree(serucity_attributes.lpSecurityDescriptor); |
107 | 107 | |
108 | locked_ = (handle_.get() != NULL); | |
108 | locked_ = (handle_.get() != nullptr); | |
109 | 109 | |
110 | 110 | if (!locked_) { |
111 | 111 | VLOG(1) << "already locked"; |
115 | 115 | if (!message.empty()) { |
116 | 116 | DWORD size = 0; |
117 | 117 | if (!::WriteFile(handle_.get(), message.data(), message.size(), &size, |
118 | NULL)) { | |
118 | nullptr)) { | |
119 | 119 | const int last_error = ::GetLastError(); |
120 | 120 | LOG(ERROR) << "Cannot write message: " << message |
121 | 121 | << ", last_error:" << last_error; |
128 | 128 | } |
129 | 129 | |
130 | 130 | bool ProcessMutex::UnLock() { |
131 | handle_.reset(NULL); | |
131 | handle_.reset(nullptr); | |
132 | 132 | FileUtil::Unlink(filename_); |
133 | 133 | locked_ = false; |
134 | 134 | return true; |
212 | 212 | bool Lock(const std::string &filename, int *fd) { |
213 | 213 | scoped_lock l(&mutex_); |
214 | 214 | |
215 | if (fd == NULL) { | |
216 | LOG(ERROR) << "fd is NULL"; | |
215 | if (fd == nullptr) { | |
216 | LOG(ERROR) << "fd is nullptr"; | |
217 | 217 | return false; |
218 | 218 | } |
219 | 219 |
156 | 156 | } |
157 | 157 | |
158 | 158 | // Get process token |
159 | HANDLE hProcessToken = NULL; | |
159 | HANDLE hProcessToken = nullptr; | |
160 | 160 | if (!::OpenProcessToken(::GetCurrentProcess(), |
161 | 161 | TOKEN_QUERY | TOKEN_QUERY_SOURCE, &hProcessToken)) { |
162 | 162 | return RunLevel::DENY; |
171 | 171 | } |
172 | 172 | |
173 | 173 | // Get thread token (if any) |
174 | HANDLE hThreadToken = NULL; | |
174 | HANDLE hThreadToken = nullptr; | |
175 | 175 | if (!::OpenThreadToken(::GetCurrentThread(), TOKEN_QUERY, TRUE, |
176 | 176 | &hThreadToken) && |
177 | 177 | ERROR_NO_TOKEN != ::GetLastError()) { |
181 | 181 | ScopedHandle thread_token(hThreadToken); |
182 | 182 | |
183 | 183 | // Thread token (if any) must not a service account. |
184 | if (NULL != thread_token.get()) { | |
184 | if (nullptr != thread_token.get()) { | |
185 | 185 | bool is_service_thread = false; |
186 | 186 | if (!WinUtil::IsServiceUser(thread_token.get(), &is_service_thread)) { |
187 | 187 | // Returns DENY conservatively. |
202 | 202 | } |
203 | 203 | |
204 | 204 | // Thread token must be created by sandbox. |
205 | if (NULL == thread_token.get()) { | |
205 | if (nullptr == thread_token.get()) { | |
206 | 206 | return RunLevel::DENY; |
207 | 207 | } |
208 | 208 | |
218 | 218 | std::wstring dir; |
219 | 219 | Util::UTF8ToWide(user_dir, &dir); |
220 | 220 | ScopedHandle dir_handle(::CreateFile(dir.c_str(), READ_CONTROL | WRITE_DAC, |
221 | 0, NULL, OPEN_EXISTING, | |
221 | 0, nullptr, OPEN_EXISTING, | |
222 | 222 | FILE_FLAG_BACKUP_SEMANTICS, 0)); |
223 | if (NULL != dir_handle.get()) { | |
223 | if (nullptr != dir_handle.get()) { | |
224 | 224 | BYTE buffer[sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE]; |
225 | 225 | DWORD size = 0; |
226 | 226 | if (::GetTokenInformation(thread_token.get(), TokenUser, buffer, |
302 | 302 | |
303 | 303 | JOBOBJECT_EXTENDED_LIMIT_INFORMATION JobExtLimitInfo; |
304 | 304 | // Get the job information of the current process |
305 | if (!::QueryInformationJobObject(NULL, JobObjectExtendedLimitInformation, | |
305 | if (!::QueryInformationJobObject(nullptr, JobObjectExtendedLimitInformation, | |
306 | 306 | &JobExtLimitInfo, sizeof(JobExtLimitInfo), |
307 | NULL)) { | |
307 | nullptr)) { | |
308 | 308 | return false; |
309 | 309 | } |
310 | 310 | |
324 | 324 | bool RunLevel::IsElevatedByUAC() { |
325 | 325 | #ifdef OS_WIN |
326 | 326 | // Get process token |
327 | HANDLE hProcessToken = NULL; | |
327 | HANDLE hProcessToken = nullptr; | |
328 | 328 | if (!::OpenProcessToken(::GetCurrentProcess(), |
329 | 329 | TOKEN_QUERY | TOKEN_QUERY_SOURCE, &hProcessToken)) { |
330 | 330 | return false; |
341 | 341 | #ifdef OS_WIN |
342 | 342 | HKEY key = 0; |
343 | 343 | LONG result = |
344 | ::RegCreateKeyExW(HKEY_CURRENT_USER, kElevatedProcessDisabledKey, 0, NULL, | |
345 | 0, KEY_WRITE, NULL, &key, NULL); | |
344 | ::RegCreateKeyExW(HKEY_CURRENT_USER, kElevatedProcessDisabledKey, 0, | |
345 | nullptr, 0, KEY_WRITE, nullptr, &key, nullptr); | |
346 | 346 | |
347 | 347 | if (ERROR_SUCCESS != result) { |
348 | 348 | return false; |
364 | 364 | #ifdef OS_WIN |
365 | 365 | HKEY key = 0; |
366 | 366 | LONG result = ::RegOpenKeyExW(HKEY_CURRENT_USER, kElevatedProcessDisabledKey, |
367 | NULL, KEY_READ, &key); | |
367 | 0, KEY_READ, &key); | |
368 | 368 | if (ERROR_SUCCESS != result) { |
369 | 369 | return false; |
370 | 370 | } |
373 | 373 | DWORD value_size = sizeof(value); |
374 | 374 | DWORD value_type = 0; |
375 | 375 | result = |
376 | ::RegQueryValueEx(key, kElevatedProcessDisabledName, NULL, &value_type, | |
376 | ::RegQueryValueEx(key, kElevatedProcessDisabledName, nullptr, &value_type, | |
377 | 377 | reinterpret_cast<BYTE *>(&value), &value_size); |
378 | 378 | ::RegCloseKey(key); |
379 | 379 |
123 | 123 | : setting_(setting), |
124 | 124 | skip_count_(0), |
125 | 125 | backoff_count_(0), |
126 | timer_(NULL), | |
126 | timer_(nullptr), | |
127 | 127 | running_(false) {} |
128 | 128 | |
129 | ~Job() { set_timer(NULL); } | |
129 | ~Job() { set_timer(nullptr); } | |
130 | 130 | |
131 | 131 | const Scheduler::JobSetting setting() const { return setting_; } |
132 | 132 | |
141 | 141 | uint32 backoff_count() const { return backoff_count_; } |
142 | 142 | |
143 | 143 | void set_timer(QueueTimer *timer) { |
144 | if (timer_ != NULL) { | |
144 | if (timer_ != nullptr) { | |
145 | 145 | delete timer_; |
146 | 146 | } |
147 | 147 | timer_ = timer; |
183 | 183 | DCHECK_NE(0, job_setting.default_interval()); |
184 | 184 | DCHECK_NE(0, job_setting.max_interval()); |
185 | 185 | // do not use DCHECK_NE as a type checker raises an error. |
186 | DCHECK(job_setting.callback() != NULL); | |
186 | DCHECK(job_setting.callback() != nullptr); | |
187 | 187 | } |
188 | 188 | |
189 | 189 | virtual bool AddJob(const Scheduler::JobSetting &job_setting) { |
209 | 209 | // TODO(hsumita): Make Job class uncopiable. |
210 | 210 | job->set_timer(new QueueTimer(std::bind(TimerCallback, job), delay, |
211 | 211 | job_setting.default_interval())); |
212 | if (job->timer() == NULL) { | |
212 | if (job->timer() == nullptr) { | |
213 | 213 | LOG(ERROR) << "failed to create QueueTimer"; |
214 | 214 | return false; |
215 | 215 | } |
245 | 245 | } |
246 | 246 | job->set_running(true); |
247 | 247 | Scheduler::JobSetting::CallbackFunc callback = job->setting().callback(); |
248 | DCHECK(callback != NULL); | |
248 | DCHECK(callback != nullptr); | |
249 | 249 | const bool success = callback(job->setting().data()); |
250 | 250 | job->set_running(false); |
251 | 251 | if (success) { |
275 | 275 | DISALLOW_COPY_AND_ASSIGN(SchedulerImpl); |
276 | 276 | }; |
277 | 277 | |
278 | Scheduler::SchedulerInterface *g_scheduler_handler = NULL; | |
278 | Scheduler::SchedulerInterface *g_scheduler_handler = nullptr; | |
279 | 279 | |
280 | 280 | Scheduler::SchedulerInterface *GetSchedulerHandler() { |
281 | if (g_scheduler_handler != NULL) { | |
281 | if (g_scheduler_handler != nullptr) { | |
282 | 282 | return g_scheduler_handler; |
283 | 283 | } else { |
284 | 284 | return Singleton<SchedulerImpl>::get(); |
37 | 37 | // usage: |
38 | 38 | // // start scheduled job |
39 | 39 | // Scheduler::AddJob(Scheduler::JobSetting( |
40 | // "TimerName", 60*1000, 60*60*1000, 30*1000, 60*1000, &Callback, NULL)); | |
41 | // (NULL is the args for Callback) | |
40 | // "TimerName", 60*1000, 60*60*1000, 30*1000, 60*1000, &Callback, nullptr)); | |
41 | // (nullptr is the args for Callback) | |
42 | 42 | // // stop job |
43 | 43 | // Scheduler::RemoveJob("TimerName"); |
44 | 44 |
38 | 38 | // ... (Do something) |
39 | 39 | // scheduler_stub.PutClockForward(60*1000); |
40 | 40 | // ... (Do something) |
41 | // Scheduler::SetSchedulerHandler(NULL); | |
41 | // Scheduler::SetSchedulerHandler(nullptr); | |
42 | 42 | // } |
43 | 43 | |
44 | 44 | #include <map> |
58 | 58 | SchedulerStub scheduler_stub; |
59 | 59 | EXPECT_FALSE(scheduler_stub.HasJob("Test")); |
60 | 60 | scheduler_stub.AddJob( |
61 | Scheduler::JobSetting("Test", 1000, 100000, 5000, 0, &TestFunc, NULL)); | |
61 | Scheduler::JobSetting("Test", 1000, 100000, 5000, 0, &TestFunc, nullptr)); | |
62 | 62 | EXPECT_TRUE(scheduler_stub.HasJob("Test")); |
63 | 63 | EXPECT_EQ(0, g_counter); |
64 | 64 | scheduler_stub.PutClockForward(1000); |
89 | 89 | TEST_F(SchedulerStubTest, BackOff) { |
90 | 90 | SchedulerStub scheduler_stub; |
91 | 91 | scheduler_stub.AddJob( |
92 | Scheduler::JobSetting("Test", 1000, 6000, 3000, 0, &TestFunc, NULL)); | |
92 | Scheduler::JobSetting("Test", 1000, 6000, 3000, 0, &TestFunc, nullptr)); | |
93 | 93 | g_result = false; |
94 | 94 | EXPECT_EQ(0, g_counter); |
95 | 95 | scheduler_stub.PutClockForward(1000); |
156 | 156 | |
157 | 157 | TEST_F(SchedulerStubTest, AddRemoveJobs) { |
158 | 158 | SchedulerStub scheduler_stub; |
159 | scheduler_stub.AddJob( | |
160 | Scheduler::JobSetting("Test1", 1000, 100000, 1000, 0, &TestFunc, NULL)); | |
159 | scheduler_stub.AddJob(Scheduler::JobSetting("Test1", 1000, 100000, 1000, 0, | |
160 | &TestFunc, nullptr)); | |
161 | 161 | EXPECT_EQ(0, g_counter); |
162 | 162 | scheduler_stub.PutClockForward(1000); // delay |
163 | 163 | EXPECT_EQ(1, g_counter); |
164 | 164 | |
165 | scheduler_stub.AddJob( | |
166 | Scheduler::JobSetting("Test2", 1000, 100000, 1000, 0, &TestFunc, NULL)); | |
165 | scheduler_stub.AddJob(Scheduler::JobSetting("Test2", 1000, 100000, 1000, 0, | |
166 | &TestFunc, nullptr)); | |
167 | 167 | |
168 | 168 | scheduler_stub.PutClockForward(1000); // delay + interval |
169 | 169 | EXPECT_EQ(3, g_counter); |
36 | 36 | class scoped_cftyperef { |
37 | 37 | public: |
38 | 38 | typedef T element_type; |
39 | explicit scoped_cftyperef(T p = NULL, bool do_retain = false) : ptr_(p) { | |
40 | if (do_retain && p != NULL) { | |
39 | explicit scoped_cftyperef(T p = nullptr, bool do_retain = false) : ptr_(p) { | |
40 | if (do_retain && p != nullptr) { | |
41 | 41 | CFRetain(ptr_); |
42 | 42 | } |
43 | 43 | } |
44 | 44 | |
45 | 45 | ~scoped_cftyperef() { |
46 | if (ptr_ != NULL) { | |
46 | if (ptr_ != nullptr) { | |
47 | 47 | CFRelease(ptr_); |
48 | 48 | } |
49 | 49 | } |
50 | 50 | |
51 | void reset(T p = NULL) { | |
52 | if (ptr_ != NULL) { | |
51 | void reset(T p = nullptr) { | |
52 | if (ptr_ != nullptr) { | |
53 | 53 | CFRelease(ptr_); |
54 | 54 | } |
55 | 55 | ptr_ = p; |
58 | 58 | T get() const { return ptr_; } |
59 | 59 | |
60 | 60 | bool Verify(CFTypeID type_id) { |
61 | return ptr_ != NULL && | |
61 | return ptr_ != nullptr && | |
62 | 62 | CFGetTypeID(reinterpret_cast<CFTypeRef>(ptr_)) == type_id; |
63 | 63 | } |
64 | 64 |
33 | 33 | |
34 | 34 | namespace mozc { |
35 | 35 | |
36 | ScopedHandle::ScopedHandle() : handle_(NULL) {} | |
36 | ScopedHandle::ScopedHandle() : handle_(nullptr) {} | |
37 | 37 | |
38 | ScopedHandle::ScopedHandle(Win32Handle handle) : handle_(NULL) { | |
38 | ScopedHandle::ScopedHandle(Win32Handle handle) : handle_(nullptr) { | |
39 | 39 | reset(handle); |
40 | 40 | } |
41 | 41 | |
68 | 68 | // transfers ownership away from this object |
69 | 69 | ScopedHandle::Win32Handle ScopedHandle::take() { |
70 | 70 | HANDLE handle = handle_; |
71 | handle_ = NULL; | |
71 | handle_ = nullptr; | |
72 | 72 | return handle; |
73 | 73 | } |
74 | 74 | |
75 | 75 | void ScopedHandle::Close() { |
76 | if (handle_ != NULL) { | |
76 | if (handle_ != nullptr) { | |
77 | 77 | ::CloseHandle(handle_); |
78 | handle_ = NULL; | |
78 | handle_ = nullptr; | |
79 | 79 | } |
80 | 80 | } |
81 | 81 |
43 | 43 | // assume HANDLE type is a synonym of void *. |
44 | 44 | typedef void *Win32Handle; |
45 | 45 | |
46 | // Initializes with NULL. | |
46 | // Initializes with nullptr. | |
47 | 47 | ScopedHandle(); |
48 | 48 | |
49 | 49 | // Initializes with taking ownership of |handle|. |
50 | 50 | // Covert: If |handle| is INVALID_HANDLE_VALUE, this wrapper treat |
51 | // it as NULL. | |
51 | // it as nullptr. | |
52 | 52 | explicit ScopedHandle(Win32Handle handle); |
53 | 53 | |
54 | 54 | // Call ::CloseHandle API against the current object (if any). |
55 | 55 | void ExitWithError() { |
56 | 56 | // This logic is copied from logging.h |
57 | 57 | #ifdef OS_WIN |
58 | ::RaiseException(::GetLastError(), EXCEPTION_NONCONTINUABLE, NULL, NULL); | |
58 | ::RaiseException(::GetLastError(), EXCEPTION_NONCONTINUABLE, 0, nullptr); | |
59 | 59 | #else |
60 | 60 | exit(-1); |
61 | 61 | #endif |
72 | 72 | |
73 | 73 | static void Delete() { |
74 | 74 | delete instance_; |
75 | instance_ = NULL; | |
75 | instance_ = nullptr; | |
76 | 76 | ResetOnce(&once_); |
77 | 77 | } |
78 | 78 | |
84 | 84 | once_t Singleton<T>::once_ = MOZC_ONCE_INIT; |
85 | 85 | |
86 | 86 | template <typename T> |
87 | T *Singleton<T>::instance_ = NULL; | |
87 | T *Singleton<T>::instance_ = nullptr; | |
88 | 88 | } // namespace mozc |
89 | 89 | |
90 | 90 | #endif // MOZC_BASE_SINGLETON_H_ |
58 | 58 | |
59 | 59 | ThreadInstance *get() { return instance_; } |
60 | 60 | |
61 | ThreadTest() : instance_(NULL) {} | |
61 | ThreadTest() : instance_(nullptr) {} | |
62 | 62 | |
63 | 63 | private: |
64 | 64 | ThreadInstance *instance_; |
54 | 54 | v.push_back(new InstanceCounter<std::string>()); |
55 | 55 | v.push_back(new InstanceCounter<std::string>()); |
56 | 56 | EXPECT_EQ(3, InstanceCounter<std::string>::instance_count); |
57 | v.push_back(NULL); | |
57 | v.push_back(nullptr); | |
58 | 58 | EXPECT_EQ(3, InstanceCounter<std::string>::instance_count); |
59 | 59 | mozc::STLDeleteElements(&v); |
60 | 60 | EXPECT_EQ(0, InstanceCounter<std::string>::instance_count); |
61 | 61 | EXPECT_EQ(0, v.size()); |
62 | 62 | |
63 | // Deleting NULL pointers to containers is ok. | |
64 | std::vector<InstanceCounter<std::string> *> *p = NULL; | |
63 | // Deleting nullptr pointers to containers is ok. | |
64 | std::vector<InstanceCounter<std::string> *> *p = nullptr; | |
65 | 65 | mozc::STLDeleteElements(p); |
66 | 66 | } |
67 | 67 |
40 | 40 | #include <pwd.h> |
41 | 41 | #include <sys/mman.h> |
42 | 42 | #include <unistd.h> |
43 | #endif // OS_WIN | |
44 | ||
43 | 45 | #ifdef __APPLE__ |
44 | 46 | #include <sys/stat.h> |
45 | 47 | #include <sys/sysctl.h> |
46 | #endif // __APPLE__ | |
47 | #endif // OS_WIN | |
48 | ||
49 | #ifdef __APPLE__ | |
50 | 48 | #include <cerrno> |
51 | 49 | #endif // __APPLE__ |
50 | ||
52 | 51 | #include <cstdlib> |
53 | 52 | #include <cstring> |
53 | ||
54 | 54 | #ifdef OS_WIN |
55 | 55 | #include <memory> |
56 | 56 | #endif // OS_WIN |
57 | ||
57 | 58 | #include <sstream> |
58 | 59 | #include <string> |
60 | ||
61 | #ifdef OS_ANDROID | |
62 | #include "base/android_util.h" | |
63 | #endif // OS_ANDROID | |
59 | 64 | |
60 | 65 | #include "base/const.h" |
61 | 66 | #include "base/file_util.h" |
62 | 67 | #include "base/logging.h" |
68 | ||
63 | 69 | #ifdef __APPLE__ |
64 | 70 | #include "base/mac_util.h" |
65 | 71 | #endif // __APPLE__ |
72 | ||
66 | 73 | #include "base/singleton.h" |
67 | 74 | #include "base/util.h" |
75 | ||
68 | 76 | #ifdef OS_WIN |
69 | 77 | #include "base/win_util.h" |
70 | #endif // OS_WIN | |
71 | ||
72 | #ifdef OS_ANDROID | |
73 | #include "base/android_util.h" | |
74 | #endif // OS_ANDROID | |
75 | ||
76 | #ifdef OS_WIN | |
77 | using std::unique_ptr; | |
78 | 78 | #endif // OS_WIN |
79 | 79 | |
80 | 80 | namespace mozc { |
209 | 209 | // NaCL platform is correct. |
210 | 210 | dir_ = "/mutable"; |
211 | 211 | return; |
212 | #else // OS_NACL || OS_CHROMEOS | |
212 | #endif // OS_NACL || OS_CHROMEOS | |
213 | ||
214 | #if defined(OS_WASM) | |
215 | // Do nothing for WebAssembly. | |
216 | return; | |
217 | #endif // OS_WASM | |
218 | ||
219 | #if defined(OS_ANDROID) | |
220 | // For android, we do nothing here because user profile directory, | |
221 | // of which the path depends on active user, | |
222 | // is injected from Java layer. | |
223 | return; | |
224 | #endif // OS_ANDROID | |
225 | ||
213 | 226 | std::string dir; |
214 | 227 | |
215 | 228 | #ifdef OS_IOS |
220 | 233 | // because the support directory doesn't exist by default. Also, it is backed |
221 | 234 | // up by iTunes and iCloud. |
222 | 235 | dir = FileUtil::JoinPath({MacUtil::GetCachesDirectory(), kProductPrefix}); |
223 | ||
224 | #elif defined(OS_WIN) | |
236 | #endif // OS_IOS | |
237 | ||
238 | #if defined(OS_WIN) | |
225 | 239 | DCHECK(SUCCEEDED(Singleton<LocalAppDataDirectoryCache>::get()->result())); |
226 | 240 | dir = Singleton<LocalAppDataDirectoryCache>::get()->path(); |
227 | #ifdef GOOGLE_JAPANESE_INPUT_BUILD | |
241 | # ifdef GOOGLE_JAPANESE_INPUT_BUILD | |
228 | 242 | dir = FileUtil::JoinPath(dir, kCompanyNameInEnglish); |
229 | 243 | FileUtil::CreateDirectory(dir); |
230 | #endif // GOOGLE_JAPANESE_INPUT_BUILD | |
244 | # endif // GOOGLE_JAPANESE_INPUT_BUILD | |
231 | 245 | dir = FileUtil::JoinPath(dir, kProductNameInEnglish); |
232 | ||
233 | #elif defined(__APPLE__) | |
246 | #endif // OS_WIN | |
247 | ||
248 | #if defined(__APPLE__) | |
234 | 249 | dir = MacUtil::GetApplicationSupportDirectory(); |
235 | #ifdef GOOGLE_JAPANESE_INPUT_BUILD | |
250 | # ifdef GOOGLE_JAPANESE_INPUT_BUILD | |
236 | 251 | dir = FileUtil::JoinPath(dir, "Google"); |
237 | 252 | // The permission of ~/Library/Application Support/Google seems to be 0755. |
238 | 253 | // TODO(komatsu): nice to make a wrapper function. |
239 | 254 | ::mkdir(dir.c_str(), 0755); |
240 | 255 | dir = FileUtil::JoinPath(dir, "JapaneseInput"); |
241 | #else // GOOGLE_JAPANESE_INPUT_BUILD | |
256 | # else // GOOGLE_JAPANESE_INPUT_BUILD | |
242 | 257 | dir = FileUtil::JoinPath(dir, "Mozc"); |
243 | #endif // GOOGLE_JAPANESE_INPUT_BUILD | |
244 | ||
245 | #elif defined(OS_WASM) | |
246 | // Do nothing for WebAssembly. | |
247 | return; | |
248 | #elif defined(OS_ANDROID) | |
249 | // For android, we do nothing here because user profile directory, | |
250 | // of which the path depends on active user, | |
251 | // is injected from Java layer. | |
252 | ||
253 | #else // !OS_IOS && !OS_WIN && !__APPLE__ && !OS_ANDROID | |
258 | # endif // GOOGLE_JAPANESE_INPUT_BUILD | |
259 | #endif // __APPLE__ | |
260 | ||
261 | #if defined(OS_LINUX) | |
254 | 262 | char buf[1024]; |
255 | 263 | struct passwd pw, *ppw; |
256 | 264 | const uid_t uid = geteuid(); |
259 | 267 | CHECK_LT(0, strlen(pw.pw_dir)) |
260 | 268 | << "Home directory for uid " << uid << " is not set."; |
261 | 269 | dir = FileUtil::JoinPath(pw.pw_dir, ".mozc"); |
262 | #endif // !OS_IOS && !OS_WIN && !__APPLE__ && !OS_ANDROID | |
270 | #endif // OS_LINUX | |
263 | 271 | |
264 | 272 | FileUtil::CreateDirectory(dir); |
265 | 273 | if (!FileUtil::DirectoryExists(dir)) { |
268 | 276 | |
269 | 277 | // set User profile directory |
270 | 278 | dir_ = dir; |
271 | #endif // !OS_NACL && !OS_CHROMEOS | |
272 | 279 | } |
273 | 280 | |
274 | 281 | } // namespace |
366 | 373 | std::string SystemUtil::GetServerDirectory() { |
367 | 374 | #ifdef OS_WIN |
368 | 375 | DCHECK(SUCCEEDED(Singleton<ProgramFilesX86Cache>::get()->result())); |
369 | #if defined(GOOGLE_JAPANESE_INPUT_BUILD) | |
376 | # if defined(GOOGLE_JAPANESE_INPUT_BUILD) | |
370 | 377 | return FileUtil::JoinPath( |
371 | 378 | FileUtil::JoinPath(Singleton<ProgramFilesX86Cache>::get()->path(), |
372 | 379 | kCompanyNameInEnglish), |
373 | 380 | kProductNameInEnglish); |
374 | #else // GOOGLE_JAPANESE_INPUT_BUILD | |
381 | # else // GOOGLE_JAPANESE_INPUT_BUILD | |
375 | 382 | return FileUtil::JoinPath(Singleton<ProgramFilesX86Cache>::get()->path(), |
376 | 383 | kProductNameInEnglish); |
377 | #endif // GOOGLE_JAPANESE_INPUT_BUILD | |
378 | ||
379 | #elif defined(__APPLE__) | |
384 | # endif // GOOGLE_JAPANESE_INPUT_BUILD | |
385 | #endif // OS_WIN | |
386 | ||
387 | #if defined(__APPLE__) | |
380 | 388 | return MacUtil::GetServerDirectory(); |
381 | ||
382 | #elif defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_NACL) || \ | |
389 | #endif // __APPLE__ | |
390 | ||
391 | #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_NACL) || \ | |
383 | 392 | defined(OS_WASM) |
384 | #if defined(MOZC_SERVER_DIRECTORY) | |
393 | # if defined(MOZC_SERVER_DIRECTORY) | |
385 | 394 | return MOZC_SERVER_DIRECTORY; |
386 | #else | |
395 | # else | |
387 | 396 | return "/usr/lib/mozc"; |
388 | #endif // MOZC_SERVER_DIRECTORY | |
389 | ||
390 | #endif // OS_WIN, __APPLE__, OS_LINUX, ... | |
397 | # endif // MOZC_SERVER_DIRECTORY | |
398 | ||
399 | #else // OS_LINUX || OS_ANDROID || OS_NACL || OS_WASM | |
400 | # error "unknown platform" | |
401 | ||
402 | #endif // OS_LINUX || OS_ANDROID || OS_NACL || OS_WASM | |
391 | 403 | } |
392 | 404 | |
393 | 405 | std::string SystemUtil::GetServerPath() { |
437 | 449 | #ifdef OS_NACL |
438 | 450 | LOG(ERROR) << "SystemUtil::GetUserNameAsString() is not implemented in NaCl."; |
439 | 451 | return "username"; |
440 | ||
441 | #elif defined(OS_WIN) // OS_NACL | |
452 | #endif // OS_NACL | |
453 | ||
454 | #if defined(OS_WIN) | |
442 | 455 | wchar_t wusername[UNLEN + 1]; |
443 | 456 | DWORD name_size = UNLEN + 1; |
444 | 457 | // Call the same name Windows API. (include Advapi32.lib). |
450 | 463 | string username; |
451 | 464 | Util::WideToUTF8(&wusername[0], &username); |
452 | 465 | return username; |
453 | ||
454 | #elif defined(OS_ANDROID) // OS_WIN | |
466 | #endif // OS_WIN | |
467 | ||
468 | #if defined(OS_ANDROID) | |
455 | 469 | // Android doesn't seem to support getpwuid_r. |
456 | 470 | struct passwd *ppw = getpwuid(geteuid()); |
457 | CHECK(ppw != NULL); | |
471 | CHECK(ppw != nullptr); | |
458 | 472 | return ppw->pw_name; |
459 | ||
460 | #else // OS_ANDROID | |
473 | #endif // OS_ANDROID | |
474 | ||
461 | 475 | // __APPLE__, OS_LINUX or OS_NACL |
462 | 476 | struct passwd pw, *ppw; |
463 | 477 | char buf[1024]; |
464 | 478 | CHECK_EQ(0, getpwuid_r(geteuid(), &pw, buf, sizeof(buf), &ppw)); |
465 | 479 | return pw.pw_name; |
466 | #endif // !OS_NACL && !OS_WIN && !OS_ANDROID | |
467 | 480 | } |
468 | 481 | |
469 | 482 | #ifdef OS_WIN |
488 | 501 | |
489 | 502 | DWORD length = 0; |
490 | 503 | ::GetTokenInformation(htoken, TokenUser, nullptr, 0, &length); |
491 | unique_ptr<char[]> buf(new char[length]); | |
504 | std::unique_ptr<char[]> buf(new char[length]); | |
492 | 505 | PTOKEN_USER p_user = reinterpret_cast<PTOKEN_USER>(buf.get()); |
493 | 506 | |
494 | 507 | if (length == 0 || |
545 | 558 | return ""; |
546 | 559 | } |
547 | 560 | |
548 | unique_ptr<char[]> buf(new char[size]); | |
561 | std::unique_ptr<char[]> buf(new char[size]); | |
549 | 562 | DWORD return_size = 0; |
550 | 563 | if (!::GetUserObjectInformationA(handle, UOI_NAME, buf.get(), size, |
551 | 564 | &return_size)) { |
559 | 572 | } |
560 | 573 | |
561 | 574 | char *result = buf.get(); |
562 | result[return_size - 1] = '\0'; // just make sure NULL terminated | |
575 | result[return_size - 1] = '\0'; // just make sure nullptr terminated | |
563 | 576 | |
564 | 577 | return result; |
565 | 578 | } |
621 | 634 | #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_NACL) || \ |
622 | 635 | defined(OS_WASM) |
623 | 636 | const char *display = getenv("DISPLAY"); |
624 | if (display == NULL) { | |
637 | if (display == nullptr) { | |
625 | 638 | return ""; |
626 | 639 | } |
627 | 640 | return display; |
628 | ||
629 | #elif defined(__APPLE__) | |
641 | #endif // OS_LINUX || OS_ANDROID || OS_NACL || OS_WASM | |
642 | ||
643 | #if defined(__APPLE__) | |
630 | 644 | return ""; |
631 | ||
632 | #elif defined(OS_WIN) | |
645 | #endif // __APPLE__ | |
646 | ||
647 | #if defined(OS_WIN) | |
633 | 648 | const string &session_id = GetSessionIdString(); |
634 | 649 | if (session_id.empty()) { |
635 | 650 | DLOG(ERROR) << "Failed to retrieve session id"; |
649 | 664 | } |
650 | 665 | |
651 | 666 | return (session_id + "." + window_station_name + "." + desktop_name); |
652 | #endif // OS_LINUX, __APPLE__, OS_WIN, ... | |
667 | #endif // OS_WIN | |
653 | 668 | } |
654 | 669 | |
655 | 670 | #ifdef OS_WIN |
839 | 854 | return 0; |
840 | 855 | } |
841 | 856 | return memory_status.ullTotalPhys; |
842 | #elif defined(__APPLE__) | |
857 | #endif // OS_WIN | |
858 | ||
859 | #if defined(__APPLE__) | |
843 | 860 | int mib[] = {CTL_HW, HW_MEMSIZE}; |
844 | 861 | uint64 total_memory = 0; |
845 | 862 | size_t size = sizeof(total_memory); |
846 | const int error = sysctl(mib, arraysize(mib), &total_memory, &size, NULL, 0); | |
863 | const int error = | |
864 | sysctl(mib, arraysize(mib), &total_memory, &size, nullptr, 0); | |
847 | 865 | if (error == -1) { |
848 | 866 | const int error = errno; |
849 | 867 | LOG(ERROR) << "sysctl with hw.memsize failed. " |
851 | 869 | return 0; |
852 | 870 | } |
853 | 871 | return total_memory; |
854 | #elif defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_NACL) || \ | |
872 | #endif // __APPLE__ | |
873 | ||
874 | #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_NACL) || \ | |
855 | 875 | defined(OS_WASM) |
856 | #if defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) | |
857 | const long page_size = sysconf(_SC_PAGESIZE); | |
858 | const long number_of_phyisical_pages = sysconf(_SC_PHYS_PAGES); | |
876 | # if defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) | |
877 | const int32 page_size = sysconf(_SC_PAGESIZE); | |
878 | const int32 number_of_phyisical_pages = sysconf(_SC_PHYS_PAGES); | |
859 | 879 | if (number_of_phyisical_pages < 0) { |
860 | 880 | // likely to be overflowed. |
861 | 881 | LOG(FATAL) << number_of_phyisical_pages << ", " << page_size; |
862 | 882 | return 0; |
863 | 883 | } |
864 | 884 | return static_cast<uint64>(number_of_phyisical_pages) * page_size; |
865 | #else // defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) | |
885 | # else // defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) | |
866 | 886 | return 0; |
867 | #endif // defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) | |
868 | #else // !(defined(OS_WIN) || defined(__APPLE__) || defined(OS_LINUX)) | |
869 | #error "unknown platform" | |
870 | #endif // OS_WIN, __APPLE__, OS_LINUX | |
887 | # endif // defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) | |
888 | ||
889 | #else // OS_LINUX || OS_ANDROID || OS_NACL || OS_WASM | |
890 | # error "unknown platform" | |
891 | ||
892 | #endif // OS_LINUX || OS_ANDROID || OS_NACL || OS_WASM | |
871 | 893 | } |
872 | 894 | |
873 | 895 | } // namespace mozc |
141 | 141 | static void SetIsWindowsX64ModeForTest(IsWindowsX64Mode mode); |
142 | 142 | |
143 | 143 | #ifdef OS_WIN |
144 | // return system directory. If failed, return NULL. | |
144 | // return system directory. If failed, return nullptr. | |
145 | 145 | // You need not to delete the returned pointer. |
146 | 146 | // This function is thread safe. |
147 | 147 | static const wchar_t *GetSystemDir(); |
43 | 43 | |
44 | 44 | #ifdef OS_WIN |
45 | 45 | UnnamedEvent::UnnamedEvent() |
46 | : handle_(::CreateEvent(NULL, FALSE, FALSE, NULL)) { | |
46 | : handle_(::CreateEvent(nullptr, FALSE, FALSE, nullptr)) { | |
47 | 47 | // Use Auto reset mode of Win32 Event. (2nd arg of CreateEvent). |
48 | 48 | // pthread_cond_signal: auto reset mode. |
49 | 49 | // pthread_cond_broadcast: manual reset mode. |
50 | if (NULL == handle_.get()) { | |
50 | if (nullptr == handle_.get()) { | |
51 | 51 | LOG(ERROR) << "CreateEvent failed: " << ::GetLastError(); |
52 | 52 | } |
53 | 53 | } |
54 | 54 | |
55 | 55 | UnnamedEvent::~UnnamedEvent() {} |
56 | 56 | |
57 | bool UnnamedEvent::IsAvailable() const { return (NULL != handle_.get()); } | |
57 | bool UnnamedEvent::IsAvailable() const { return (nullptr != handle_.get()); } | |
58 | 58 | |
59 | 59 | bool UnnamedEvent::Notify() { |
60 | 60 | if (!IsAvailable()) { |
99 | 99 | } // namespace |
100 | 100 | |
101 | 101 | UnnamedEvent::UnnamedEvent() : notified_(false) { |
102 | pthread_mutex_init(&mutex_, NULL); | |
103 | pthread_cond_init(&cond_, NULL); | |
102 | pthread_mutex_init(&mutex_, nullptr); | |
103 | pthread_cond_init(&cond_, nullptr); | |
104 | 104 | } |
105 | 105 | |
106 | 106 | // It is necessary to ensure that no threads wait for this event before the |
155 | 155 | } else { |
156 | 156 | // Wait with time out. |
157 | 157 | struct timeval tv; |
158 | if (gettimeofday(&tv, NULL) != 0) { | |
158 | if (gettimeofday(&tv, nullptr) != 0) { | |
159 | 159 | LOG(ERROR) << "Failed to take the current time: " << errno; |
160 | 160 | return false; |
161 | 161 | } |
44 | 44 | L"Software\\Google\\Update\\ClientState\\" |
45 | 45 | L"{DDCCD2A9-025E-4142-BCEB-F467B88CF830}"; |
46 | 46 | HKEY key; |
47 | LONG result = RegCreateKeyExW(HKEY_CURRENT_USER, kOmahaUsageKey, 0, NULL, 0, | |
48 | KEY_WRITE, NULL, &key, NULL); | |
47 | LONG result = RegCreateKeyExW(HKEY_CURRENT_USER, kOmahaUsageKey, 0, nullptr, | |
48 | 0, KEY_WRITE, nullptr, &key, nullptr); | |
49 | 49 | if (ERROR_SUCCESS != result) { |
50 | 50 | return false; |
51 | 51 | } |
35 | 35 | // clang-format on |
36 | 36 | #include <stdio.h> // MSVC requires this for _vsnprintf |
37 | 37 | #include <time.h> |
38 | #else // OS_WIN | |
38 | #endif // OS_WIN | |
39 | 39 | |
40 | 40 | #ifdef __APPLE__ |
41 | 41 | #include <mach/mach.h> |
42 | 42 | #include <mach/mach_time.h> |
43 | ||
44 | #elif defined(OS_NACL) // __APPLE__ | |
43 | #endif // __APPLE__ | |
44 | ||
45 | #if defined(OS_NACL) | |
45 | 46 | #include <irt.h> |
46 | #endif // __APPLE__ or OS_NACL | |
47 | #endif // OS_NACL | |
48 | ||
49 | #ifndef OS_WIN | |
47 | 50 | #include <sys/mman.h> |
48 | 51 | #include <sys/time.h> |
49 | 52 | #include <unistd.h> |
50 | #endif // OS_WIN | |
53 | #endif // !OS_WIN | |
51 | 54 | |
52 | 55 | #include <algorithm> |
53 | 56 | #include <cctype> |
240 | 243 | str[input.size()] = '\0'; |
241 | 244 | |
242 | 245 | char *eos = str + input.size(); |
243 | char *start = NULL; | |
244 | char *end = NULL; | |
246 | char *start = nullptr; | |
247 | char *end = nullptr; | |
245 | 248 | output->clear(); |
246 | 249 | |
247 | 250 | while (str < eos) { |
495 | 498 | bool Util::SplitFirstChar32(absl::string_view s, char32 *first_char32, |
496 | 499 | absl::string_view *rest) { |
497 | 500 | char32 dummy_char32 = 0; |
498 | if (first_char32 == NULL) { | |
501 | if (first_char32 == nullptr) { | |
499 | 502 | first_char32 = &dummy_char32; |
500 | 503 | } |
501 | 504 | absl::string_view dummy_rest; |
502 | if (rest == NULL) { | |
505 | if (rest == nullptr) { | |
503 | 506 | rest = &dummy_rest; |
504 | 507 | } |
505 | 508 | |
587 | 590 | bool Util::SplitLastChar32(absl::string_view s, absl::string_view *rest, |
588 | 591 | char32 *last_char32) { |
589 | 592 | absl::string_view dummy_rest; |
590 | if (rest == NULL) { | |
593 | if (rest == nullptr) { | |
591 | 594 | rest = &dummy_rest; |
592 | 595 | } |
593 | 596 | char32 dummy_char32 = 0; |
594 | if (last_char32 == NULL) { | |
597 | if (last_char32 == nullptr) { | |
595 | 598 | last_char32 = &dummy_char32; |
596 | 599 | } |
597 | 600 | |
700 | 703 | #ifdef OS_WIN |
701 | 704 | size_t Util::WideCharsLen(absl::string_view src) { |
702 | 705 | const int num_chars = |
703 | ::MultiByteToWideChar(CP_UTF8, 0, src.begin(), src.size(), NULL, 0); | |
706 | ::MultiByteToWideChar(CP_UTF8, 0, src.begin(), src.size(), nullptr, 0); | |
704 | 707 | if (num_chars <= 0) { |
705 | 708 | return 0; |
706 | 709 | } |
725 | 728 | |
726 | 729 | int Util::WideToUTF8(const wchar_t *input, string *output) { |
727 | 730 | const int output_length = |
728 | WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL); | |
731 | WideCharToMultiByte(CP_UTF8, 0, input, -1, nullptr, 0, nullptr, nullptr); | |
729 | 732 | if (output_length == 0) { |
730 | 733 | return 0; |
731 | 734 | } |
733 | 736 | std::unique_ptr<char[]> input_encoded(new char[output_length + 1]); |
734 | 737 | const int result = |
735 | 738 | WideCharToMultiByte(CP_UTF8, 0, input, -1, input_encoded.get(), |
736 | output_length + 1, NULL, NULL); | |
739 | output_length + 1, nullptr, nullptr); | |
737 | 740 | if (result > 0) { |
738 | 741 | output->assign(input_encoded.get()); |
739 | 742 | } |
825 | 828 | memset(buf, '\0', buf_size); |
826 | 829 | #ifdef OS_WIN |
827 | 830 | HCRYPTPROV hprov; |
828 | if (!::CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL, | |
831 | if (!::CryptAcquireContext(&hprov, nullptr, nullptr, PROV_RSA_FULL, | |
829 | 832 | CRYPT_VERIFYCONTEXT)) { |
830 | 833 | return false; |
831 | 834 | } |
836 | 839 | } |
837 | 840 | ::CryptReleaseContext(hprov, 0); |
838 | 841 | return true; |
839 | #elif defined(OS_NACL) | |
842 | #endif // OS_WIN | |
843 | ||
844 | #if defined(OS_NACL) | |
840 | 845 | struct nacl_irt_random interface; |
841 | 846 | |
842 | 847 | if (nacl_interface_query(NACL_IRT_RANDOM_v0_1, &interface, |
856 | 861 | return false; |
857 | 862 | } |
858 | 863 | return true; |
859 | #else // !OS_WIN && !OS_NACL | |
864 | #endif | |
865 | ||
866 | #if defined(OS_CHROMEOS) | |
867 | // TODO(googleo): b/171939770 Accessing "/dev/urandom" is not allowed in | |
868 | // "ime" sandbox. Returns false to use the self-implemented random number | |
869 | // instead. When the API is unblocked, remove the hack. | |
870 | return false; | |
871 | #endif // OS_CHROMEOS | |
872 | ||
860 | 873 | // Use non blocking interface on Linux. |
861 | 874 | // Mac also have /dev/urandom (although it's identical with /dev/random) |
862 | 875 | std::ifstream ifs("/dev/urandom", std::ios::binary); |
865 | 878 | } |
866 | 879 | ifs.read(buf, buf_size); |
867 | 880 | return true; |
868 | #endif // OS_WIN or OS_NACL | |
869 | 881 | } |
870 | 882 | } // namespace |
871 | 883 | |
1269 | 1281 | void Util::AppendCGIParams( |
1270 | 1282 | const std::vector<std::pair<std::string, std::string> > ¶ms, |
1271 | 1283 | std::string *base) { |
1272 | if (params.size() == 0 || base == NULL) { | |
1284 | if (params.size() == 0 || base == nullptr) { | |
1273 | 1285 | return; |
1274 | 1286 | } |
1275 | 1287 | |
1550 | 1562 | if (s.size() != 8) { |
1551 | 1563 | return false; |
1552 | 1564 | } |
1565 | // The following operations assume `char` is unsigned (i.e. -funsigned-char). | |
1553 | 1566 | *x = static_cast<uint64>(s[0]) << 56 | static_cast<uint64>(s[1]) << 48 | |
1554 | 1567 | static_cast<uint64>(s[2]) << 40 | static_cast<uint64>(s[3]) << 32 | |
1555 | 1568 | static_cast<uint64>(s[4]) << 24 | static_cast<uint64>(s[5]) << 16 | |
1560 | 1573 | bool Util::IsLittleEndian() { |
1561 | 1574 | #ifdef OS_WIN |
1562 | 1575 | return true; |
1563 | #else // OS_WIN | |
1576 | #endif // OS_WIN | |
1577 | ||
1564 | 1578 | union { |
1565 | 1579 | unsigned char c[4]; |
1566 | 1580 | unsigned int i; |
1570 | 1584 | static_assert(sizeof(u) == sizeof(u.i), "Checking alignment."); |
1571 | 1585 | u.i = 0x12345678U; |
1572 | 1586 | return u.c[0] == 0x78U; |
1573 | #endif // OS_WIN | |
1574 | 1587 | } |
1575 | 1588 | |
1576 | 1589 | } // namespace mozc |
213 | 213 | static size_t UCS4ToUTF8(char32 c, char *output); |
214 | 214 | |
215 | 215 | // Returns true if |s| is split into |first_char32| + |rest|. |
216 | // You can pass NULL to |first_char32| and/or |rest| to ignore the matched | |
216 | // You can pass nullptr to |first_char32| and/or |rest| to ignore the matched | |
217 | 217 | // value. |
218 | 218 | // Returns false if an invalid UTF-8 sequence is prefixed. That is, |rest| may |
219 | 219 | // contain any invalid sequence even when this method returns true. |
221 | 221 | absl::string_view *rest); |
222 | 222 | |
223 | 223 | // Returns true if |s| is split into |rest| + |last_char32|. |
224 | // You can pass NULL to |rest| and/or |last_char32| to ignore the matched | |
224 | // You can pass nullptr to |rest| and/or |last_char32| to ignore the matched | |
225 | 225 | // value. |
226 | 226 | // Returns false if an invalid UTF-8 sequence is suffixed. That is, |rest| may |
227 | 227 | // contain any invalid sequence even when this method returns true. |
58 | 58 | } |
59 | 59 | |
60 | 60 | TEST(WinSandboxTest, GetSidsToDisable) { |
61 | HANDLE process_token_ret = NULL; | |
61 | HANDLE process_token_ret = nullptr; | |
62 | 62 | ::OpenProcessToken(::GetCurrentProcess(), TOKEN_ALL_ACCESS, |
63 | 63 | &process_token_ret); |
64 | 64 | ScopedHandle process_token(process_token_ret); |
91 | 91 | } |
92 | 92 | |
93 | 93 | TEST(WinSandboxTest, GetPrivilegesToDisable) { |
94 | HANDLE process_token_ret = NULL; | |
94 | HANDLE process_token_ret = nullptr; | |
95 | 95 | ::OpenProcessToken(::GetCurrentProcess(), TOKEN_ALL_ACCESS, |
96 | 96 | &process_token_ret); |
97 | 97 | ScopedHandle process_token(process_token_ret); |
117 | 117 | } |
118 | 118 | |
119 | 119 | TEST(WinSandboxTest, GetSidsToRestrict) { |
120 | HANDLE process_token_ret = NULL; | |
120 | HANDLE process_token_ret = nullptr; | |
121 | 121 | ::OpenProcessToken(::GetCurrentProcess(), TOKEN_ALL_ACCESS, |
122 | 122 | &process_token_ret); |
123 | 123 | ScopedHandle process_token(process_token_ret); |
45 | 45 | // system directory. |
46 | 46 | // If the function succeeds, the return value is a handle to the module. |
47 | 47 | // You should call FreeLibrary with the handle. |
48 | // If the function fails, the return value is NULL. | |
48 | // If the function fails, the return value is nullptr. | |
49 | 49 | static HMODULE LoadSystemLibrary(const std::wstring &base_filename); |
50 | 50 | |
51 | 51 | // Load a DLL which has the specified base-name and is located in the |
52 | 52 | // Mozc server directory. |
53 | 53 | // If the function succeeds, the return value is a handle to the module. |
54 | 54 | // You should call FreeLibrary with the handle. |
55 | // If the function fails, the return value is NULL. | |
55 | // If the function fails, the return value is nullptr. | |
56 | 56 | static HMODULE LoadMozcLibrary(const std::wstring &base_filename); |
57 | 57 | |
58 | 58 | // If a DLL which has the specified base-name and located in the system |
60 | 60 | // If the function succeeds, the return value is a handle to the module |
61 | 61 | // without incrementing its reference count so that you should not call |
62 | 62 | // FreeLibrary with the handle. |
63 | // If the function fails, the return value is NULL. | |
63 | // If the function fails, the return value is nullptr. | |
64 | 64 | static HMODULE GetSystemModuleHandle(const std::wstring &base_filename); |
65 | 65 | |
66 | 66 | // A variant ot GetSystemModuleHandle except that this method increments |
171 | 171 | ScopedCOMInitializer(); |
172 | 172 | ScopedCOMInitializer::~ScopedCOMInitializer(); |
173 | 173 | |
174 | // Returns the error code from CoInitialize(NULL) | |
174 | // Returns the error code from CoInitialize(nullptr) | |
175 | 175 | // (called in constructor) |
176 | 176 | inline HRESULT error_code() const { return hr_; } |
177 | 177 |
202 | 202 | EXPECT_TRUE(LooksLikeNtPath(nt_system_dir)); |
203 | 203 | |
204 | 204 | EXPECT_FALSE(WinUtil::GetNtPath(system_dir, nullptr)) |
205 | << "Must fail against a NULL argument."; | |
205 | << "Must fail against a nullptr argument."; | |
206 | 206 | |
207 | 207 | std::wstring nt_notepad; |
208 | 208 | EXPECT_TRUE(WinUtil::GetNtPath(notepad, &nt_notepad)) |
60 | 60 | // program name into standard argc/argv parameters. |
61 | 61 | class WinCommandLine { |
62 | 62 | public: |
63 | WinCommandLine() : argc_(0), argv_(NULL) { | |
63 | WinCommandLine() : argc_(0), argv_(nullptr) { | |
64 | 64 | LPWSTR *argvw = ::CommandLineToArgvW(::GetCommandLineW(), &argc_); |
65 | if (argvw == NULL) { | |
65 | if (argvw == nullptr) { | |
66 | 66 | return; |
67 | 67 | } |
68 | 68 | |
83 | 83 | delete[] argv_[i]; |
84 | 84 | } |
85 | 85 | delete[] argv_; |
86 | argv_ = NULL; | |
86 | argv_ = nullptr; | |
87 | 87 | } |
88 | 88 | |
89 | 89 | int argc() const { return argc_; } |
117 | 117 | DWORD vt = 0; |
118 | 118 | HKEY hKey = 0; |
119 | 119 | if (ERROR_SUCCESS == ::RegOpenKeyExW(HKEY_CURRENT_USER, mozc::kMozcRegKey, |
120 | NULL, KEY_READ, &hKey)) { | |
120 | nullptr, KEY_READ, &hKey)) { | |
121 | 121 | if (ERROR_SUCCESS == ::RegQueryValueExW( |
122 | hKey, L"debug_sleep_time", NULL, &vt, | |
122 | hKey, L"debug_sleep_time", nullptr, &vt, | |
123 | 123 | reinterpret_cast<BYTE *>(&sleep_time), &size) && |
124 | 124 | vt == REG_DWORD && sleep_time > 0) { |
125 | 125 | ::Sleep(sleep_time * 1000); |
292 | 292 | static_cast<MozcSessionHandlerThread *>(data); |
293 | 293 | usage_stats::UsageStats::SetInteger("BigDictionaryState", |
294 | 294 | self->GetBigDictionaryState()); |
295 | return usage_stats::UsageStatsUploader::Send(NULL); | |
295 | return usage_stats::UsageStatsUploader::Send(nullptr); | |
296 | 296 | } |
297 | 297 | #endif // GOOGLE_JAPANESE_INPUT_BUILD |
298 | 298 | |
390 | 390 | // We use dummy argc and argv to call mozc::InitMozc(). |
391 | 391 | int argc = 1; |
392 | 392 | char argv0[] = "NaclModule"; |
393 | char *argv_body[] = {argv0, NULL}; | |
393 | char *argv_body[] = {argv0, nullptr}; | |
394 | 394 | char **argv = argv_body; |
395 | 395 | mozc::InitMozc(argv[0], &argc, &argv); |
396 | 396 |
32 | 32 | "cc_binary_mozc", |
33 | 33 | "cc_library_mozc", |
34 | 34 | "cc_test_mozc", |
35 | "py_binary_mozc", | |
35 | 36 | "select_mozc", |
36 | 37 | ) |
37 | 38 | |
198 | 199 | ], |
199 | 200 | ) |
200 | 201 | |
202 | py_binary_mozc( | |
203 | name = "gen_client_quality_test_data", | |
204 | srcs = ["gen_client_quality_test_data.py"], | |
205 | ) | |
206 | ||
207 | genrule( | |
208 | name = "client_quality_test_data", | |
209 | srcs = [ | |
210 | # TSV files formatted as "label <tab> expected <tab> query" | |
211 | ], | |
212 | outs = ["client_quality_test_data.inc"], | |
213 | cmd = "$(location :gen_client_quality_test_data) $(SRCS) > $@", | |
214 | exec_tools = [":gen_client_quality_test_data"], | |
215 | ) | |
216 | ||
217 | cc_binary_mozc( | |
218 | name = "client_quality_test_main", | |
219 | srcs = [ | |
220 | "client_quality_test_main.cc", | |
221 | ":client_quality_test_data", | |
222 | ], | |
223 | deps = [ | |
224 | ":client", | |
225 | "//base", | |
226 | "//base:file_stream", | |
227 | "//base:flags", | |
228 | "//base:init_mozc", | |
229 | "//base:logging", | |
230 | "//base:multifile", | |
231 | "//base:port", | |
232 | "//base:util", | |
233 | "//evaluation:scorer", | |
234 | "//protocol:commands_proto", | |
235 | "@com_google_absl//absl/strings", | |
236 | ], | |
237 | ) |
392 | 392 | } |
393 | 393 | |
394 | 394 | void Client::EnableCascadingWindow(const bool enable) { |
395 | if (preferences_.get() == NULL) { | |
395 | if (preferences_ == nullptr) { | |
396 | 396 | preferences_.reset(new config::Config); |
397 | 397 | } |
398 | 398 | preferences_->set_use_cascading_window(enable); |
535 | 535 | |
536 | 536 | // PingServer ignores all server status |
537 | 537 | bool Client::PingServer() const { |
538 | if (client_factory_ == NULL) { | |
538 | if (client_factory_ == nullptr) { | |
539 | 539 | return false; |
540 | 540 | } |
541 | 541 | |
549 | 549 | std::unique_ptr<IPCClientInterface> client(client_factory_->NewClient( |
550 | 550 | kServerAddress, server_launcher_->server_program())); |
551 | 551 | |
552 | if (client.get() == NULL) { | |
552 | if (client == nullptr) { | |
553 | 553 | LOG(ERROR) << "Cannot make client object"; |
554 | 554 | return false; |
555 | 555 | } |
605 | 605 | return false; |
606 | 606 | } |
607 | 607 | |
608 | if (client_factory_ == NULL) { | |
608 | if (client_factory_ == nullptr) { | |
609 | 609 | return false; |
610 | 610 | } |
611 | 611 | |
628 | 628 | server_product_version_ = Version::GetMozcVersion(); |
629 | 629 | server_process_id_ = 0; |
630 | 630 | |
631 | if (client.get() == NULL) { | |
631 | if (client == nullptr) { | |
632 | 632 | LOG(ERROR) << "Cannot make client object"; |
633 | 633 | server_status_ = SERVER_FATAL; |
634 | 634 | return false; |
689 | 689 | } |
690 | 690 | |
691 | 691 | bool Client::StartServer() { |
692 | if (server_launcher_.get() != NULL) { | |
692 | if (server_launcher_ != nullptr) { | |
693 | 693 | return server_launcher_->StartServer(this); |
694 | 694 | } |
695 | 695 | return true; |
696 | 696 | } |
697 | 697 | |
698 | 698 | void Client::OnFatal(ServerLauncherInterface::ServerErrorType type) { |
699 | if (server_launcher_.get() != NULL) { | |
699 | if (server_launcher_ != nullptr) { | |
700 | 700 | server_launcher_->OnFatal(type); |
701 | 701 | } |
702 | 702 | } |
703 | 703 | |
704 | 704 | void Client::InitInput(commands::Input *input) const { |
705 | 705 | input->set_id(id_); |
706 | if (preferences_.get() != NULL) { | |
706 | if (preferences_ != nullptr) { | |
707 | 707 | input->mutable_config()->CopyFrom(*preferences_); |
708 | 708 | } |
709 | 709 | } |
791 | 791 | |
792 | 792 | bool Client::TranslateProtoBufToMozcToolArg(const commands::Output &output, |
793 | 793 | std::string *mode) { |
794 | if (!output.has_launch_tool_mode() || mode == NULL) { | |
794 | if (!output.has_launch_tool_mode() || mode == nullptr) { | |
795 | 795 | return false; |
796 | 796 | } |
797 | 797 | |
906 | 906 | virtual ClientInterface *NewClient() { return new Client; } |
907 | 907 | }; |
908 | 908 | |
909 | ClientFactoryInterface *g_client_factory = NULL; | |
909 | ClientFactoryInterface *g_client_factory = nullptr; | |
910 | 910 | } // namespace |
911 | 911 | |
912 | 912 | ClientInterface *ClientFactory::NewClient() { |
913 | if (g_client_factory == NULL) { | |
913 | if (g_client_factory == nullptr) { | |
914 | 914 | return Singleton<DefaultClientFactory>::get()->NewClient(); |
915 | 915 | } else { |
916 | 916 | return g_client_factory->NewClient(); |
211 | 211 | |
212 | 212 | std::map<std::string, std::vector<double> > scores; // Results to be averaged |
213 | 213 | |
214 | for (mozc::TestCase* test_case = mozc::test_cases; test_case->source != NULL; | |
215 | ++test_case) { | |
214 | for (mozc::TestCase* test_case = mozc::test_cases; | |
215 | test_case->source != nullptr; ++test_case) { | |
216 | 216 | const std::string& source = test_case->source; |
217 | 217 | const std::string& hiragana_sentence = test_case->hiragana_sentence; |
218 | 218 | const std::string& expected_result = test_case->expected_result; |
149 | 149 | client.SendKey(keys[i], &output); |
150 | 150 | VLOG(2) << "Output of SendKey: " << output.DebugString(); |
151 | 151 | |
152 | if (renderer_client.get() != NULL) { | |
152 | if (renderer_client != nullptr) { | |
153 | 153 | renderer_command.set_type(commands::RendererCommand::UPDATE); |
154 | 154 | renderer_command.set_visible(output.has_candidates()); |
155 | 155 | renderer_command.mutable_output()->CopyFrom(output); |
178 | 178 | } |
179 | 179 | |
180 | 180 | std::unique_ptr<mozc::InputFileStream> input_file; |
181 | std::istream *input = NULL; | |
181 | std::istream *input = nullptr; | |
182 | 182 | |
183 | 183 | if (!FLAGS_input.empty()) { |
184 | 184 | // Batch mode loading the input file. |
130 | 130 | client.SendKey(keys[i], &output); |
131 | 131 | VLOG(2) << "Output of SendKey: " << output.DebugString(); |
132 | 132 | |
133 | if (renderer_client.get() != NULL) { | |
133 | if (renderer_client != nullptr) { | |
134 | 134 | renderer_command.set_type(mozc::commands::RendererCommand::UPDATE); |
135 | 135 | renderer_command.set_visible(output.has_candidates()); |
136 | 136 | renderer_command.mutable_output()->CopyFrom(output); |
27 | 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 | |
30 | """Generate a C++ include file for testing data from TSV files.""" | |
31 | ||
32 | from __future__ import print_function | |
33 | ||
30 | 34 | import codecs |
31 | 35 | import logging |
32 | 36 | import sys |
33 | 37 | |
34 | 38 | |
35 | 39 | def escape(string): |
36 | return ''.join('\\x%02x' % ord(char) for char in string.encode('utf-8')) | |
40 | return ''.join('\\x%02x' % char for char in string.encode('utf-8')) | |
37 | 41 | |
38 | 42 | |
39 | 43 | def convert_tsv(filename): |
44 | """Loads a TSV file and returns an entry of TestCase.""" | |
40 | 45 | tsv = codecs.open(filename, 'rb', 'utf-8') |
41 | 46 | for line in tsv: |
42 | 47 | line = line.rstrip() |
56 | 61 | expected = fields[4] |
57 | 62 | query = fields[5] |
58 | 63 | |
59 | print ' // {"%s", "%s", "%s"},' % (label, expected, query) | |
60 | print (' {"%s", "%s", "%s"},' % | |
61 | (escape(label), escape(expected), escape(query))) | |
64 | print(' // {"%s", "%s", "%s"},' % (label, expected, query)) | |
65 | print(' {"%s", "%s", "%s"},' % | |
66 | (escape(label), escape(expected), escape(query))) | |
62 | 67 | tsv.close() |
63 | 68 | |
64 | 69 | |
65 | 70 | def main(): |
66 | sys.stdin = codecs.getreader('utf-8')(sys.stdin) | |
67 | sys.stdout = codecs.getwriter('utf-8')(sys.stdout) | |
68 | sys.stderr = codecs.getwriter('utf-8')(sys.stderr) | |
69 | logging.basicConfig(level = logging.INFO) | |
71 | sys.stdin = codecs.getreader('utf-8')(sys.stdin.buffer) | |
72 | sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer) | |
73 | sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer) | |
74 | logging.basicConfig(level=logging.INFO) | |
70 | 75 | |
71 | print '// Automatically generated by mozc' | |
72 | print '#ifndef MOZC_SESSION_QUALITY_MAIN_DATA_H_' | |
73 | print '#define MOZC_SESSION_QUALITY_MAIN_DATA_H_' | |
74 | print '' | |
75 | print 'namespace mozc {' | |
76 | print 'struct TestCase {' | |
77 | print ' const char* source;' | |
78 | print ' const char* expected_result;' | |
79 | print ' const char* hiragana_sentence;' | |
80 | print '};' | |
81 | print '' | |
82 | print 'static TestCase test_cases[] = {' | |
76 | print('// Automatically generated by mozc') | |
77 | print('#ifndef MOZC_SESSION_QUALITY_MAIN_DATA_H_') | |
78 | print('#define MOZC_SESSION_QUALITY_MAIN_DATA_H_') | |
79 | print('') | |
80 | print('namespace mozc {') | |
81 | print('struct TestCase {') | |
82 | print(' const char* source;') | |
83 | print(' const char* expected_result;') | |
84 | print(' const char* hiragana_sentence;') | |
85 | print('};') | |
86 | print('') | |
87 | print('static TestCase test_cases[] = {') | |
83 | 88 | |
84 | 89 | for filename in sys.argv[1:]: |
85 | 90 | convert_tsv(filename) |
86 | 91 | |
87 | print ' {NULL, NULL, NULL}' | |
88 | print '};' | |
89 | print '} // namespace mozc' | |
90 | print '#endif // MOZC_SESSION_QUALITY_MAIN_DATA_H_' | |
92 | print(' {nullptr, nullptr, nullptr}') | |
93 | print('};') | |
94 | print('} // namespace mozc') | |
95 | print('#endif // MOZC_SESSION_QUALITY_MAIN_DATA_H_') | |
91 | 96 | |
92 | 97 | |
93 | 98 | if __name__ == '__main__': |
1043 | 1043 | |
1044 | 1044 | // static |
1045 | 1045 | bool Composer::TransformCharactersForNumbers(std::string *query) { |
1046 | if (query == NULL) { | |
1047 | LOG(ERROR) << "query is NULL"; | |
1046 | if (query == nullptr) { | |
1047 | LOG(ERROR) << "query is nullptr"; | |
1048 | 1048 | return false; |
1049 | 1049 | } |
1050 | 1050 |
1108 | 1108 | composer_->GetStringForSubmission(&src_composition); |
1109 | 1109 | EXPECT_EQ("", src_composition); |
1110 | 1110 | |
1111 | Composer dest(NULL, request_.get(), config_.get()); | |
1111 | Composer dest(nullptr, request_.get(), config_.get()); | |
1112 | 1112 | dest.CopyFrom(*composer_); |
1113 | 1113 | |
1114 | 1114 | ExpectSameComposer(*composer_, dest); |
1123 | 1123 | composer_->GetStringForSubmission(&src_composition); |
1124 | 1124 | EXPECT_EQ("あn", src_composition); |
1125 | 1125 | |
1126 | Composer dest(NULL, request_.get(), config_.get()); | |
1126 | Composer dest(nullptr, request_.get(), config_.get()); | |
1127 | 1127 | dest.CopyFrom(*composer_); |
1128 | 1128 | |
1129 | 1129 | ExpectSameComposer(*composer_, dest); |
1136 | 1136 | composer_->GetQueryForConversion(&src_composition); |
1137 | 1137 | EXPECT_EQ("あん", src_composition); |
1138 | 1138 | |
1139 | Composer dest(NULL, request_.get(), config_.get()); | |
1139 | Composer dest(nullptr, request_.get(), config_.get()); | |
1140 | 1140 | dest.CopyFrom(*composer_); |
1141 | 1141 | |
1142 | 1142 | ExpectSameComposer(*composer_, dest); |
1155 | 1155 | composer_->GetStringForSubmission(&src_composition); |
1156 | 1156 | EXPECT_EQ("AaAAあ", src_composition); |
1157 | 1157 | |
1158 | Composer dest(NULL, request_.get(), config_.get()); | |
1158 | Composer dest(nullptr, request_.get(), config_.get()); | |
1159 | 1159 | dest.CopyFrom(*composer_); |
1160 | 1160 | |
1161 | 1161 | ExpectSameComposer(*composer_, dest); |
1173 | 1173 | composer_->GetStringForSubmission(&src_composition); |
1174 | 1174 | EXPECT_EQ("M", src_composition); |
1175 | 1175 | |
1176 | Composer dest(NULL, request_.get(), config_.get()); | |
1176 | Composer dest(nullptr, request_.get(), config_.get()); | |
1177 | 1177 | dest.CopyFrom(*composer_); |
1178 | 1178 | |
1179 | 1179 | ExpectSameComposer(*composer_, dest); |
155 | 155 | size_t key_length = 0; |
156 | 156 | bool fixed = false; |
157 | 157 | const Entry *entry = table_->LookUpPrefix(pending_, &key_length, &fixed); |
158 | if (entry != NULL && entry->input() == entry->result()) { | |
158 | if (entry != nullptr && entry->input() == entry->result()) { | |
159 | 159 | converted.append(entry->result()); |
160 | 160 | } |
161 | 161 | } |
303 | 303 | const Entry *entry = table_->LookUpPrefix(key, &key_length, &fixed); |
304 | 304 | local_length_cache_ = std::string::npos; |
305 | 305 | |
306 | if (entry == NULL) { | |
306 | if (entry == nullptr) { | |
307 | 307 | if (key_length == 0) { |
308 | 308 | // No prefix character is not contained in the table, fallback |
309 | 309 | // operation is performed. |
338 | 338 | return kNoLoop; |
339 | 339 | } |
340 | 340 | |
341 | // The prefix of key reached a conversion result, thus entry is not NULL. | |
341 | // The prefix of key reached a conversion result, thus entry is not nullptr. | |
342 | 342 | |
343 | 343 | if (key.size() == key_length) { |
344 | 344 | const bool is_following_entry = |
420 | 420 | // If this entry is the first entry, the table attributes are |
421 | 421 | // applied to this chunk. |
422 | 422 | const Entry *entry = table_->LookUp(pending_); |
423 | if (entry != NULL) { | |
423 | if (entry != nullptr) { | |
424 | 424 | attributes_ = entry->attributes(); |
425 | 425 | } |
426 | 426 | return; |
430 | 430 | size_t key_length = 0; |
431 | 431 | bool fixed = false; |
432 | 432 | const Entry *entry = table_->LookUpPrefix(input, &key_length, &fixed); |
433 | if (entry == NULL) { | |
433 | if (entry == nullptr) { | |
434 | 434 | // Do not modify this char_chunk, all key and converted_char |
435 | 435 | // values will be used by the next char_chunk. |
436 | 436 | return; |
190 | 190 | } |
191 | 191 | |
192 | 192 | TEST(CharChunkTest, GetLength) { |
193 | CharChunk chunk1(Transliterators::CONVERSION_STRING, NULL); | |
193 | CharChunk chunk1(Transliterators::CONVERSION_STRING, nullptr); | |
194 | 194 | chunk1.set_conversion("ね"); |
195 | 195 | chunk1.set_pending(""); |
196 | 196 | chunk1.set_raw("ne"); |
197 | 197 | EXPECT_EQ(1, chunk1.GetLength(Transliterators::CONVERSION_STRING)); |
198 | 198 | EXPECT_EQ(2, chunk1.GetLength(Transliterators::RAW_STRING)); |
199 | 199 | |
200 | CharChunk chunk2(Transliterators::CONVERSION_STRING, NULL); | |
200 | CharChunk chunk2(Transliterators::CONVERSION_STRING, nullptr); | |
201 | 201 | chunk2.set_conversion("っと"); |
202 | 202 | chunk2.set_pending(""); |
203 | 203 | chunk2.set_raw("tto"); |
204 | 204 | EXPECT_EQ(2, chunk2.GetLength(Transliterators::CONVERSION_STRING)); |
205 | 205 | EXPECT_EQ(3, chunk2.GetLength(Transliterators::RAW_STRING)); |
206 | 206 | |
207 | CharChunk chunk3(Transliterators::CONVERSION_STRING, NULL); | |
207 | CharChunk chunk3(Transliterators::CONVERSION_STRING, nullptr); | |
208 | 208 | chunk3.set_conversion("が"); |
209 | 209 | chunk3.set_pending(""); |
210 | 210 | chunk3.set_raw("ga"); |
362 | 362 | EXPECT_EQ("mo", output); |
363 | 363 | |
364 | 364 | // Split "mo" to "m" and "o". |
365 | CharChunk *left_chunk_ptr = NULL; | |
365 | CharChunk *left_chunk_ptr = nullptr; | |
366 | 366 | chunk.SplitChunk(Transliterators::LOCAL, 1, &left_chunk_ptr); |
367 | 367 | std::unique_ptr<CharChunk> left_chunk(left_chunk_ptr); |
368 | 368 | |
994 | 994 | chunk.AddInput(&input); |
995 | 995 | } |
996 | 996 | |
997 | CharChunk *left_new_chunk_ptr = NULL; | |
997 | CharChunk *left_new_chunk_ptr = nullptr; | |
998 | 998 | chunk.SplitChunk(Transliterators::HIRAGANA, size_t(1), &left_new_chunk_ptr); |
999 | 999 | std::unique_ptr<CharChunk> left_new_chunk(left_new_chunk_ptr); |
1000 | 1000 | { |
1006 | 1006 | |
1007 | 1007 | TEST(CharChunkTest, Combine) { |
1008 | 1008 | { |
1009 | CharChunk lhs(Transliterators::CONVERSION_STRING, NULL); | |
1010 | CharChunk rhs(Transliterators::CONVERSION_STRING, NULL); | |
1009 | CharChunk lhs(Transliterators::CONVERSION_STRING, nullptr); | |
1010 | CharChunk rhs(Transliterators::CONVERSION_STRING, nullptr); | |
1011 | 1011 | lhs.set_ambiguous("LA"); |
1012 | 1012 | lhs.set_conversion("LC"); |
1013 | 1013 | lhs.set_pending("LP"); |
1026 | 1026 | } |
1027 | 1027 | |
1028 | 1028 | { // lhs' ambigous is empty. |
1029 | CharChunk lhs(Transliterators::CONVERSION_STRING, NULL); | |
1030 | CharChunk rhs(Transliterators::CONVERSION_STRING, NULL); | |
1029 | CharChunk lhs(Transliterators::CONVERSION_STRING, nullptr); | |
1030 | CharChunk rhs(Transliterators::CONVERSION_STRING, nullptr); | |
1031 | 1031 | |
1032 | 1032 | lhs.set_ambiguous(""); |
1033 | 1033 | lhs.set_conversion("LC"); |
1047 | 1047 | } |
1048 | 1048 | |
1049 | 1049 | { // rhs' ambigous is empty. |
1050 | CharChunk lhs(Transliterators::CONVERSION_STRING, NULL); | |
1051 | CharChunk rhs(Transliterators::CONVERSION_STRING, NULL); | |
1050 | CharChunk lhs(Transliterators::CONVERSION_STRING, nullptr); | |
1051 | CharChunk rhs(Transliterators::CONVERSION_STRING, nullptr); | |
1052 | 1052 | |
1053 | 1053 | lhs.set_ambiguous("LA"); |
1054 | 1054 | lhs.set_conversion("LC"); |
1203 | 1203 | |
1204 | 1204 | TEST(CharChunkTest, SplitChunkWithSpecialKeys) { |
1205 | 1205 | { |
1206 | CharChunk chunk(Transliterators::CONVERSION_STRING, NULL); | |
1206 | CharChunk chunk(Transliterators::CONVERSION_STRING, nullptr); | |
1207 | 1207 | chunk.set_raw("a"); |
1208 | 1208 | chunk.set_conversion(Table::ParseSpecialKey("ab{1}cd")); |
1209 | 1209 | |
1210 | CharChunk *left_chunk_ptr = NULL; | |
1210 | CharChunk *left_chunk_ptr = nullptr; | |
1211 | 1211 | EXPECT_FALSE(chunk.SplitChunk(Transliterators::CONVERSION_STRING, 0, |
1212 | 1212 | &left_chunk_ptr)); |
1213 | 1213 | std::unique_ptr<CharChunk> left_chunk(left_chunk_ptr); |
1218 | 1218 | } |
1219 | 1219 | |
1220 | 1220 | { |
1221 | CharChunk chunk(Transliterators::CONVERSION_STRING, NULL); | |
1221 | CharChunk chunk(Transliterators::CONVERSION_STRING, nullptr); | |
1222 | 1222 | chunk.set_raw("a"); |
1223 | 1223 | chunk.set_conversion(Table::ParseSpecialKey("ab{1}cd")); |
1224 | 1224 | |
1225 | CharChunk *left_chunk_ptr = NULL; | |
1225 | CharChunk *left_chunk_ptr = nullptr; | |
1226 | 1226 | EXPECT_TRUE(chunk.SplitChunk(Transliterators::CONVERSION_STRING, 1, |
1227 | 1227 | &left_chunk_ptr)); |
1228 | 1228 | std::unique_ptr<CharChunk> left_chunk(left_chunk_ptr); |
1231 | 1231 | } |
1232 | 1232 | |
1233 | 1233 | { |
1234 | CharChunk chunk(Transliterators::CONVERSION_STRING, NULL); | |
1234 | CharChunk chunk(Transliterators::CONVERSION_STRING, nullptr); | |
1235 | 1235 | chunk.set_raw("a"); |
1236 | 1236 | chunk.set_conversion(Table::ParseSpecialKey("ab{1}cd")); |
1237 | 1237 | |
1238 | CharChunk *left_chunk_ptr = NULL; | |
1238 | CharChunk *left_chunk_ptr = nullptr; | |
1239 | 1239 | EXPECT_TRUE(chunk.SplitChunk(Transliterators::CONVERSION_STRING, 2, |
1240 | 1240 | &left_chunk_ptr)); |
1241 | 1241 | std::unique_ptr<CharChunk> left_chunk(left_chunk_ptr); |
1244 | 1244 | } |
1245 | 1245 | |
1246 | 1246 | { |
1247 | CharChunk chunk(Transliterators::CONVERSION_STRING, NULL); | |
1247 | CharChunk chunk(Transliterators::CONVERSION_STRING, nullptr); | |
1248 | 1248 | chunk.set_raw("a"); |
1249 | 1249 | chunk.set_conversion(Table::ParseSpecialKey("ab{1}cd")); |
1250 | 1250 | |
1251 | CharChunk *left_chunk_ptr = NULL; | |
1251 | CharChunk *left_chunk_ptr = nullptr; | |
1252 | 1252 | EXPECT_TRUE(chunk.SplitChunk(Transliterators::CONVERSION_STRING, 3, |
1253 | 1253 | &left_chunk_ptr)); |
1254 | 1254 | std::unique_ptr<CharChunk> left_chunk(left_chunk_ptr); |
1722 | 1722 | src.attributes_ = NEW_CHUNK; |
1723 | 1723 | |
1724 | 1724 | std::unique_ptr<CharChunk> dest( |
1725 | new CharChunk(Transliterators::CONVERSION_STRING, NULL)); | |
1725 | new CharChunk(Transliterators::CONVERSION_STRING, nullptr)); | |
1726 | 1726 | EXPECT_FALSE(src.transliterator_ == dest->transliterator_); |
1727 | 1727 | EXPECT_FALSE(src.table_ == dest->table_); |
1728 | 1728 | EXPECT_NE(src.raw_, dest->raw_); |
120 | 120 | continue; |
121 | 121 | } |
122 | 122 | |
123 | CharChunk *left_deleted_chunk_ptr = NULL; | |
123 | CharChunk *left_deleted_chunk_ptr = nullptr; | |
124 | 124 | (*chunk_it)->SplitChunk(Transliterators::LOCAL, 1, &left_deleted_chunk_ptr); |
125 | 125 | std::unique_ptr<CharChunk> left_deleted_chunk(left_deleted_chunk_ptr); |
126 | 126 | } |
369 | 369 | // The position is the beginning of composition. |
370 | 370 | if (pos <= 0) { |
371 | 371 | *it = chunks_.begin(); |
372 | return NULL; | |
372 | return nullptr; | |
373 | 373 | } |
374 | 374 | |
375 | 375 | size_t inner_position; |
381 | 381 | return chunk; |
382 | 382 | } |
383 | 383 | |
384 | CharChunk *left_chunk = NULL; | |
384 | CharChunk *left_chunk = nullptr; | |
385 | 385 | chunk->SplitChunk(Transliterators::LOCAL, inner_position, &left_chunk); |
386 | 386 | chunks_.insert(*it, left_chunk); |
387 | 387 | return left_chunk; |
34 | 34 | namespace composer { |
35 | 35 | |
36 | 36 | CompositionInput::CompositionInput() |
37 | : has_conversion_(false), is_new_input_(false), transliterator_(NULL) {} | |
37 | : has_conversion_(false), is_new_input_(false), transliterator_(nullptr) {} | |
38 | 38 | |
39 | 39 | CompositionInput::~CompositionInput() {} |
40 | 40 | |
43 | 43 | conversion_.clear(); |
44 | 44 | has_conversion_ = false; |
45 | 45 | is_new_input_ = false; |
46 | transliterator_ = NULL; | |
46 | transliterator_ = nullptr; | |
47 | 47 | } |
48 | 48 | |
49 | 49 | bool CompositionInput::Empty() const { |
45 | 45 | EXPECT_FALSE(input.has_conversion()); |
46 | 46 | EXPECT_TRUE(input.conversion().empty()); |
47 | 47 | EXPECT_FALSE(input.is_new_input()); |
48 | EXPECT_TRUE(NULL == input.transliterator()); | |
48 | EXPECT_TRUE(nullptr == input.transliterator()); | |
49 | 49 | } |
50 | 50 | |
51 | 51 | { // Value setting |
71 | 71 | EXPECT_FALSE(input.has_conversion()); |
72 | 72 | EXPECT_TRUE(input.conversion().empty()); |
73 | 73 | EXPECT_FALSE(input.is_new_input()); |
74 | EXPECT_TRUE(NULL == input.transliterator()); | |
74 | EXPECT_TRUE(nullptr == input.transliterator()); | |
75 | 75 | |
76 | 76 | EXPECT_FALSE(input2.Empty()); |
77 | 77 | EXPECT_EQ("raw", input2.raw()); |
282 | 282 | }; |
283 | 283 | for (int i = 0; i < arraysize(test_cases); ++i) { |
284 | 284 | const TestCase& test = test_cases[i]; |
285 | CharChunk right_orig_chunk(Transliterators::CONVERSION_STRING, NULL); | |
285 | CharChunk right_orig_chunk(Transliterators::CONVERSION_STRING, nullptr); | |
286 | 286 | right_orig_chunk.set_conversion(test.conversion); |
287 | 287 | right_orig_chunk.set_pending(test.pending); |
288 | 288 | right_orig_chunk.set_raw(test.raw); |
289 | CharChunk* left_new_chunk_ptr = NULL; | |
289 | CharChunk* left_new_chunk_ptr = nullptr; | |
290 | 290 | right_orig_chunk.SplitChunk(Transliterators::RAW_STRING, test.position, |
291 | 291 | &left_new_chunk_ptr); |
292 | 292 | std::unique_ptr<CharChunk> left_new_chunk(left_new_chunk_ptr); |
293 | 293 | |
294 | if (left_new_chunk.get() != NULL) { | |
294 | if (left_new_chunk != nullptr) { | |
295 | 295 | EXPECT_EQ(test.expected_left_conversion, left_new_chunk->conversion()); |
296 | 296 | EXPECT_EQ(test.expected_left_pending, left_new_chunk->pending()); |
297 | 297 | EXPECT_EQ(test.expected_left_raw, left_new_chunk->raw()); |
327 | 327 | }; |
328 | 328 | for (int i = 0; i < arraysize(test_cases); ++i) { |
329 | 329 | const TestCase& test = test_cases[i]; |
330 | CharChunk right_orig_chunk(Transliterators::CONVERSION_STRING, NULL); | |
330 | CharChunk right_orig_chunk(Transliterators::CONVERSION_STRING, nullptr); | |
331 | 331 | right_orig_chunk.set_conversion(test.conversion); |
332 | 332 | right_orig_chunk.set_pending(test.pending); |
333 | 333 | right_orig_chunk.set_raw(test.raw); |
334 | CharChunk* left_new_chunk_ptr = NULL; | |
334 | CharChunk* left_new_chunk_ptr = nullptr; | |
335 | 335 | right_orig_chunk.SplitChunk(Transliterators::CONVERSION_STRING, |
336 | 336 | test.position, &left_new_chunk_ptr); |
337 | 337 | std::unique_ptr<CharChunk> left_new_chunk(left_new_chunk_ptr); |
338 | 338 | |
339 | if (left_new_chunk.get() != NULL) { | |
339 | if (left_new_chunk != nullptr) { | |
340 | 340 | EXPECT_EQ(test.expected_left_conversion, left_new_chunk->conversion()); |
341 | 341 | EXPECT_EQ(test.expected_left_pending, left_new_chunk->pending()); |
342 | 342 | EXPECT_EQ(test.expected_left_raw, left_new_chunk->raw()); |
49 | 49 | size_t key_length; |
50 | 50 | bool fixed; |
51 | 51 | const Entry* entry = table_.LookUpPrefix(key, &key_length, &fixed); |
52 | if (entry == NULL) { | |
52 | if (entry == nullptr) { | |
53 | 53 | output->append(key, 0, 1); |
54 | 54 | key.erase(0, 1); |
55 | 55 | } else { |
168 | 168 | void TypingCorrector::GetQueriesForPrediction( |
169 | 169 | std::vector<TypeCorrectedQuery> *queries) const { |
170 | 170 | queries->clear(); |
171 | if (!IsAvailable() || table_ == NULL || raw_key_.empty()) { | |
171 | if (!IsAvailable() || table_ == nullptr || raw_key_.empty()) { | |
172 | 172 | return; |
173 | 173 | } |
174 | 174 | // These objects are for cache. Used and reset repeatedly. |
41 | 41 | 0, 1, 2, 3, 4, 5, 6, |
42 | 42 | }; |
43 | 43 | TypingModel model(characters, strlen(characters), costs, arraysize(costs), |
44 | NULL); | |
44 | nullptr); | |
45 | 45 | model.character_to_radix_table_['a'] = 1; |
46 | 46 | model.character_to_radix_table_['b'] = 2; |
47 | 47 | model.character_to_radix_table_['c'] = 3; |
55 | 55 | 0, 1, 2, 3, 4, 5, 6, |
56 | 56 | }; |
57 | 57 | TypingModel model(characters, strlen(characters), costs, arraysize(costs), |
58 | NULL); | |
58 | nullptr); | |
59 | 59 | ASSERT_EQ(0, model.GetIndex("")); |
60 | 60 | ASSERT_EQ(1, model.GetIndex("a")); |
61 | 61 | ASSERT_EQ(4, model.GetIndex("d")); |
206 | 206 | for (size_t i = 0; i < keys.size(); ++i) { |
207 | 207 | if (Util::CharsLen(keys[i]) == 1) { |
208 | 208 | char32 key_code = 0; |
209 | if (Util::SplitFirstChar32(keys[i], &key_code, NULL)) { | |
209 | if (Util::SplitFirstChar32(keys[i], &key_code, nullptr)) { | |
210 | 210 | key_event->set_key_code(key_code); |
211 | 211 | } |
212 | 212 | } else { |
197 | 197 | table_file_name = kNotouchHalfwidthasciiTableFile; |
198 | 198 | break; |
199 | 199 | default: |
200 | table_file_name = NULL; | |
200 | table_file_name = nullptr; | |
201 | 201 | } |
202 | 202 | if (table_file_name && LoadFromFile(table_file_name)) { |
203 | 203 | return true; |
228 | 228 | // Initialize punctuations. |
229 | 229 | const config::Config::PunctuationMethod punctuation_method = |
230 | 230 | config.punctuation_method(); |
231 | const mozc::composer::Entry *entry = NULL; | |
231 | const mozc::composer::Entry *entry = nullptr; | |
232 | 232 | |
233 | 233 | // Comma / Kuten |
234 | 234 | entry = LookUp(","); |
235 | if (entry == NULL || | |
235 | if (entry == nullptr || | |
236 | 236 | (entry->result() == kKuten && entry->pending().empty())) { |
237 | 237 | if (punctuation_method == config::Config::COMMA_PERIOD || |
238 | 238 | punctuation_method == config::Config::COMMA_TOUTEN) { |
244 | 244 | |
245 | 245 | // Period / Touten |
246 | 246 | entry = LookUp("."); |
247 | if (entry == NULL || | |
247 | if (entry == nullptr || | |
248 | 248 | (entry->result() == kTouten && entry->pending().empty())) { |
249 | 249 | if (punctuation_method == config::Config::COMMA_PERIOD || |
250 | 250 | punctuation_method == config::Config::KUTEN_PERIOD) { |
259 | 259 | |
260 | 260 | // Slash / Middle dot |
261 | 261 | entry = LookUp("/"); |
262 | if (entry == NULL || | |
262 | if (entry == nullptr || | |
263 | 263 | (entry->result() == kMiddleDot && entry->pending().empty())) { |
264 | 264 | if (symbol_method == config::Config::SQUARE_BRACKET_SLASH || |
265 | 265 | symbol_method == config::Config::CORNER_BRACKET_SLASH) { |
271 | 271 | |
272 | 272 | // Square open bracket / Corner open bracket |
273 | 273 | entry = LookUp("["); |
274 | if (entry == NULL || | |
274 | if (entry == nullptr || | |
275 | 275 | (entry->result() == kCornerOpen && entry->pending().empty())) { |
276 | 276 | if (symbol_method == config::Config::CORNER_BRACKET_MIDDLE_DOT || |
277 | 277 | symbol_method == config::Config::CORNER_BRACKET_SLASH) { |
283 | 283 | |
284 | 284 | // Square close bracket / Corner close bracket |
285 | 285 | entry = LookUp("]"); |
286 | if (entry == NULL || | |
286 | if (entry == nullptr || | |
287 | 287 | (entry->result() == kCornerClose && entry->pending().empty())) { |
288 | 288 | if (symbol_method == config::Config::CORNER_BRACKET_MIDDLE_DOT || |
289 | 289 | symbol_method == config::Config::CORNER_BRACKET_SLASH) { |
318 | 318 | size_t key_length = 0; |
319 | 319 | bool fixed = false; |
320 | 320 | const Entry *entry = LookUpPrefix(key, &key_length, &fixed); |
321 | if (entry == NULL) { | |
321 | if (entry == nullptr) { | |
322 | 322 | return false; |
323 | 323 | } |
324 | 324 | DCHECK_LE(key_length, key.size()); |
349 | 349 | if (escaped_input.size() >= kMaxSize || output.size() >= kMaxSize || |
350 | 350 | escaped_pending.size() >= kMaxSize) { |
351 | 351 | LOG(ERROR) << "Invalid input/output/pending"; |
352 | return NULL; | |
352 | return nullptr; | |
353 | 353 | } |
354 | 354 | |
355 | 355 | const std::string input = ParseSpecialKey(escaped_input); |
357 | 357 | if (IsLoopingEntry(input, pending)) { |
358 | 358 | LOG(WARNING) << "Entry " << input << " " << output << " " << pending |
359 | 359 | << " is removed, since the rule is looping"; |
360 | return NULL; | |
361 | } | |
362 | ||
363 | const Entry *old_entry = NULL; | |
360 | return nullptr; | |
361 | } | |
362 | ||
363 | const Entry *old_entry = nullptr; | |
364 | 364 | if (entries_->LookUp(input, &old_entry)) { |
365 | 365 | DeleteEntry(old_entry); |
366 | 366 | } |
407 | 407 | |
408 | 408 | bool Table::LoadFromFile(const char *filepath) { |
409 | 409 | std::unique_ptr<std::istream> ifs(ConfigFileStream::LegacyOpen(filepath)); |
410 | if (ifs.get() == NULL) { | |
410 | if (ifs == nullptr) { | |
411 | 411 | return false; |
412 | 412 | } |
413 | 413 | return LoadFromStream(ifs.get()); |
471 | 471 | } |
472 | 472 | |
473 | 473 | const Entry *Table::LookUp(const std::string &input) const { |
474 | const Entry *entry = NULL; | |
474 | const Entry *entry = nullptr; | |
475 | 475 | if (case_sensitive_) { |
476 | 476 | entries_->LookUp(input, &entry); |
477 | 477 | } else { |
484 | 484 | |
485 | 485 | const Entry *Table::LookUpPrefix(const std::string &input, size_t *key_length, |
486 | 486 | bool *fixed) const { |
487 | const Entry *entry = NULL; | |
487 | const Entry *entry = nullptr; | |
488 | 488 | if (case_sensitive_) { |
489 | 489 | entries_->LookUpPrefix(input, &entry, key_length, fixed); |
490 | 490 | } else { |
63 | 63 | |
64 | 64 | std::string GetResult(const Table &table, const std::string &key) { |
65 | 65 | const Entry *entry = table.LookUp(key); |
66 | if (entry == NULL) { | |
67 | return "<NULL>"; | |
66 | if (entry == nullptr) { | |
67 | return "<nullptr>"; | |
68 | 68 | } |
69 | 69 | return entry->result(); |
70 | 70 | } |
71 | 71 | |
72 | 72 | std::string GetInput(const Table &table, const std::string &key) { |
73 | 73 | const Entry *entry = table.LookUp(key); |
74 | if (entry == NULL) { | |
75 | return "<NULL>"; | |
74 | if (entry == nullptr) { | |
75 | return "<nullptr>"; | |
76 | 76 | } |
77 | 77 | return entry->input(); |
78 | 78 | } |
115 | 115 | const Entry *entry; |
116 | 116 | entry = table.LookUp(test.input); |
117 | 117 | |
118 | EXPECT_EQ(test.expected_result, (entry != NULL)); | |
119 | if (entry == NULL) { | |
118 | EXPECT_EQ(test.expected_result, (entry != nullptr)); | |
119 | if (entry == nullptr) { | |
120 | 120 | continue; |
121 | 121 | } |
122 | 122 | EXPECT_EQ(test.expected_output, entry->result()); |
159 | 159 | ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config, |
160 | 160 | mock_data_manager_)); |
161 | 161 | const Entry *entry = table.LookUp(test_cases[i].input); |
162 | EXPECT_TRUE(entry != NULL) << "Failed index = " << i; | |
162 | EXPECT_TRUE(entry != nullptr) << "Failed index = " << i; | |
163 | 163 | if (entry) { |
164 | 164 | EXPECT_EQ(test_cases[i].expected, entry->result()); |
165 | 165 | } |
195 | 195 | ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config, |
196 | 196 | mock_data_manager_)); |
197 | 197 | const Entry *entry = table.LookUp(test_cases[i].input); |
198 | EXPECT_TRUE(entry != NULL) << "Failed index = " << i; | |
198 | EXPECT_TRUE(entry != nullptr) << "Failed index = " << i; | |
199 | 199 | if (entry) { |
200 | 200 | EXPECT_EQ(test_cases[i].expected, entry->result()); |
201 | 201 | } |
212 | 212 | mock_data_manager_)); |
213 | 213 | |
214 | 214 | const Entry *entry = table.LookUp("a"); |
215 | ASSERT_TRUE(entry != NULL); | |
215 | ASSERT_TRUE(entry != nullptr); | |
216 | 216 | EXPECT_EQ("あ", entry->result()); |
217 | 217 | EXPECT_TRUE(entry->pending().empty()); |
218 | 218 | } |
223 | 223 | ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config_, |
224 | 224 | mock_data_manager_)); |
225 | 225 | const Entry *entry = table.LookUp("か゛"); |
226 | ASSERT_TRUE(entry != NULL); | |
226 | ASSERT_TRUE(entry != nullptr); | |
227 | 227 | EXPECT_EQ("が", entry->result()); |
228 | 228 | EXPECT_TRUE(entry->pending().empty()); |
229 | 229 | } |
237 | 237 | EXPECT_TRUE(table.IsLoopingEntry("b", "a")); |
238 | 238 | table.AddRule("b", "aa", "a"); // looping |
239 | 239 | |
240 | EXPECT_TRUE(table.LookUp("a") != NULL); | |
241 | EXPECT_TRUE(table.LookUp("b") == NULL); | |
240 | EXPECT_TRUE(table.LookUp("a") != nullptr); | |
241 | EXPECT_TRUE(table.LookUp("b") == nullptr); | |
242 | 242 | } |
243 | 243 | |
244 | 244 | { |
249 | 249 | EXPECT_TRUE(table.IsLoopingEntry("b", "a")); |
250 | 250 | table.AddRule("b", "aa", "a"); // looping |
251 | 251 | |
252 | EXPECT_TRUE(table.LookUp("a") != NULL); | |
253 | EXPECT_TRUE(table.LookUp("b") == NULL); | |
252 | EXPECT_TRUE(table.LookUp("a") != nullptr); | |
253 | EXPECT_TRUE(table.LookUp("b") == nullptr); | |
254 | 254 | } |
255 | 255 | |
256 | 256 | { |
267 | 267 | EXPECT_TRUE(table.IsLoopingEntry("d", "a")); |
268 | 268 | table.AddRule("d", "aa", "a"); // looping |
269 | 269 | |
270 | EXPECT_TRUE(table.LookUp("a") != NULL); | |
271 | EXPECT_TRUE(table.LookUp("b") != NULL); | |
272 | EXPECT_TRUE(table.LookUp("c") != NULL); | |
273 | EXPECT_TRUE(table.LookUp("d") == NULL); | |
270 | EXPECT_TRUE(table.LookUp("a") != nullptr); | |
271 | EXPECT_TRUE(table.LookUp("b") != nullptr); | |
272 | EXPECT_TRUE(table.LookUp("c") != nullptr); | |
273 | EXPECT_TRUE(table.LookUp("d") == nullptr); | |
274 | 274 | } |
275 | 275 | |
276 | 276 | { |
281 | 281 | EXPECT_FALSE(table.IsLoopingEntry("www", "ww")); |
282 | 282 | table.AddRule("www", "W", "ww"); // not looping |
283 | 283 | |
284 | EXPECT_TRUE(table.LookUp("wa") != NULL); | |
285 | EXPECT_TRUE(table.LookUp("ww") != NULL); | |
286 | EXPECT_TRUE(table.LookUp("www") != NULL); | |
284 | EXPECT_TRUE(table.LookUp("wa") != nullptr); | |
285 | EXPECT_TRUE(table.LookUp("ww") != nullptr); | |
286 | EXPECT_TRUE(table.LookUp("www") != nullptr); | |
287 | 287 | } |
288 | 288 | |
289 | 289 | { |
294 | 294 | EXPECT_FALSE(table.IsLoopingEntry("ww", "w")); |
295 | 295 | table.AddRule("ww", "X", "w"); |
296 | 296 | |
297 | EXPECT_TRUE(table.LookUp("wa") != NULL); | |
298 | EXPECT_TRUE(table.LookUp("ww") != NULL); | |
299 | EXPECT_TRUE(table.LookUp("www") != NULL); | |
297 | EXPECT_TRUE(table.LookUp("wa") != nullptr); | |
298 | EXPECT_TRUE(table.LookUp("ww") != nullptr); | |
299 | EXPECT_TRUE(table.LookUp("www") != nullptr); | |
300 | 300 | } |
301 | 301 | |
302 | 302 | { |
304 | 304 | EXPECT_TRUE(table.IsLoopingEntry("a", "a")); |
305 | 305 | table.AddRule("a", "aa", "a"); // looping |
306 | 306 | |
307 | EXPECT_TRUE(table.LookUp("a") == NULL); | |
307 | EXPECT_TRUE(table.LookUp("a") == nullptr); | |
308 | 308 | } |
309 | 309 | |
310 | 310 | // Too long input |
316 | 316 | too_long += 'a'; |
317 | 317 | } |
318 | 318 | table.AddRule(too_long, "test", "test"); |
319 | EXPECT_TRUE(table.LookUp(too_long) == NULL); | |
319 | EXPECT_TRUE(table.LookUp(too_long) == nullptr); | |
320 | 320 | |
321 | 321 | table.AddRule("a", too_long, "test"); |
322 | EXPECT_TRUE(table.LookUp("a") == NULL); | |
322 | EXPECT_TRUE(table.LookUp("a") == nullptr); | |
323 | 323 | |
324 | 324 | table.AddRule("a", "test", too_long); |
325 | EXPECT_TRUE(table.LookUp("a") == NULL); | |
325 | EXPECT_TRUE(table.LookUp("a") == nullptr); | |
326 | 326 | } |
327 | 327 | |
328 | 328 | // reasonably long |
334 | 334 | reasonably_long += 'a'; |
335 | 335 | } |
336 | 336 | table.AddRule(reasonably_long, "test", "test"); |
337 | EXPECT_TRUE(table.LookUp(reasonably_long) != NULL); | |
337 | EXPECT_TRUE(table.LookUp(reasonably_long) != nullptr); | |
338 | 338 | |
339 | 339 | table.AddRule("a", reasonably_long, "test"); |
340 | EXPECT_TRUE(table.LookUp("a") != NULL); | |
340 | EXPECT_TRUE(table.LookUp("a") != nullptr); | |
341 | 341 | |
342 | 342 | table.AddRule("a", "test", reasonably_long); |
343 | EXPECT_TRUE(table.LookUp("a") != NULL); | |
343 | EXPECT_TRUE(table.LookUp("a") != nullptr); | |
344 | 344 | } |
345 | 345 | } |
346 | 346 | |
360 | 360 | commands::Request request; |
361 | 361 | table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_); |
362 | 362 | |
363 | const Entry *entry = NULL; | |
363 | const Entry *entry = nullptr; | |
364 | 364 | entry = table.LookUp("mozc"); |
365 | ASSERT_TRUE(entry != NULL); | |
365 | ASSERT_TRUE(entry != nullptr); | |
366 | 366 | EXPECT_EQ("MOZC", entry->result()); |
367 | 367 | |
368 | 368 | entry = table.LookUp(","); |
369 | ASSERT_TRUE(entry != NULL); | |
369 | ASSERT_TRUE(entry != nullptr); | |
370 | 370 | EXPECT_EQ("COMMA", entry->result()); |
371 | 371 | |
372 | 372 | entry = table.LookUp("."); |
373 | ASSERT_TRUE(entry != NULL); | |
373 | ASSERT_TRUE(entry != nullptr); | |
374 | 374 | EXPECT_EQ("PERIOD", entry->result()); |
375 | 375 | |
376 | 376 | entry = table.LookUp("/"); |
377 | ASSERT_TRUE(entry != NULL); | |
377 | ASSERT_TRUE(entry != nullptr); | |
378 | 378 | EXPECT_EQ("SLASH", entry->result()); |
379 | 379 | |
380 | 380 | entry = table.LookUp("["); |
381 | ASSERT_TRUE(entry != NULL); | |
381 | ASSERT_TRUE(entry != nullptr); | |
382 | 382 | EXPECT_EQ("OPEN", entry->result()); |
383 | 383 | |
384 | 384 | entry = table.LookUp("]"); |
385 | ASSERT_TRUE(entry != NULL); | |
385 | ASSERT_TRUE(entry != nullptr); | |
386 | 386 | EXPECT_EQ("CLOSE", entry->result()); |
387 | 387 | } |
388 | 388 | |
417 | 417 | EXPECT_TRUE(table.HasSubRules("Z")); |
418 | 418 | |
419 | 419 | { // Test for LookUpPrefix |
420 | const Entry *entry = NULL; | |
420 | const Entry *entry = nullptr; | |
421 | 421 | size_t key_length = 0; |
422 | 422 | bool fixed = false; |
423 | 423 | entry = table.LookUpPrefix("bA", &key_length, &fixed); |
424 | EXPECT_TRUE(entry != NULL); | |
424 | EXPECT_TRUE(entry != nullptr); | |
425 | 425 | EXPECT_EQ("[ba]", entry->result()); |
426 | 426 | EXPECT_EQ(2, key_length); |
427 | 427 | EXPECT_TRUE(fixed); |
435 | 435 | EXPECT_EQ("[ba]", GetResult(table, "ba")); |
436 | 436 | EXPECT_EQ("[BA]", GetResult(table, "BA")); |
437 | 437 | EXPECT_EQ("[Ba]", GetResult(table, "Ba")); |
438 | EXPECT_EQ("<NULL>", GetResult(table, "bA")); | |
438 | EXPECT_EQ("<nullptr>", GetResult(table, "bA")); | |
439 | 439 | |
440 | 440 | EXPECT_EQ("a", GetInput(table, "a")); |
441 | 441 | EXPECT_EQ("A", GetInput(table, "A")); |
442 | 442 | EXPECT_EQ("ba", GetInput(table, "ba")); |
443 | 443 | EXPECT_EQ("BA", GetInput(table, "BA")); |
444 | 444 | EXPECT_EQ("Ba", GetInput(table, "Ba")); |
445 | EXPECT_EQ("<NULL>", GetInput(table, "bA")); | |
445 | EXPECT_EQ("<nullptr>", GetInput(table, "bA")); | |
446 | 446 | |
447 | 447 | // Test for HasSubRules |
448 | 448 | EXPECT_FALSE(table.HasSubRules("Z")); |
449 | 449 | |
450 | 450 | { // Test for LookUpPrefix |
451 | const Entry *entry = NULL; | |
451 | const Entry *entry = nullptr; | |
452 | 452 | size_t key_length = 0; |
453 | 453 | bool fixed = false; |
454 | 454 | entry = table.LookUpPrefix("bA", &key_length, &fixed); |
455 | EXPECT_TRUE(entry == NULL); | |
455 | EXPECT_TRUE(entry == nullptr); | |
456 | 456 | EXPECT_EQ(1, key_length); |
457 | 457 | EXPECT_TRUE(fixed); |
458 | 458 | } |
521 | 521 | EXPECT_EQ("[ba]", GetResult(table, "ba")); |
522 | 522 | EXPECT_EQ("[BA]", GetResult(table, "BA")); |
523 | 523 | EXPECT_EQ("[Ba]", GetResult(table, "Ba")); |
524 | EXPECT_EQ("<NULL>", GetResult(table, "bA")); | |
524 | EXPECT_EQ("<nullptr>", GetResult(table, "bA")); | |
525 | 525 | |
526 | 526 | EXPECT_EQ("a", GetInput(table, "a")); |
527 | 527 | EXPECT_EQ("A", GetInput(table, "A")); |
528 | 528 | EXPECT_EQ("ba", GetInput(table, "ba")); |
529 | 529 | EXPECT_EQ("BA", GetInput(table, "BA")); |
530 | 530 | EXPECT_EQ("Ba", GetInput(table, "Ba")); |
531 | EXPECT_EQ("<NULL>", GetInput(table, "bA")); | |
531 | EXPECT_EQ("<nullptr>", GetInput(table, "bA")); | |
532 | 532 | |
533 | 533 | // Test for HasSubRules |
534 | 534 | EXPECT_FALSE(table.HasSubRules("Z")); |
535 | 535 | |
536 | 536 | { // Test for LookUpPrefix |
537 | const Entry *entry = NULL; | |
537 | const Entry *entry = nullptr; | |
538 | 538 | size_t key_length = 0; |
539 | 539 | bool fixed = false; |
540 | 540 | entry = table.LookUpPrefix("bA", &key_length, &fixed); |
541 | EXPECT_TRUE(entry == NULL); | |
541 | EXPECT_TRUE(entry == nullptr); | |
542 | 542 | EXPECT_EQ(1, key_length); |
543 | 543 | EXPECT_TRUE(fixed); |
544 | 544 | } |
561 | 561 | EXPECT_EQ("[ba]", GetResult(table, "ba")); |
562 | 562 | EXPECT_EQ("[BA]", GetResult(table, "BA")); |
563 | 563 | EXPECT_EQ("[Ba]", GetResult(table, "Ba")); |
564 | EXPECT_EQ("<NULL>", GetResult(table, "bA")); | |
564 | EXPECT_EQ("<nullptr>", GetResult(table, "bA")); | |
565 | 565 | |
566 | 566 | EXPECT_EQ("a", GetInput(table, "a")); |
567 | 567 | EXPECT_EQ("A", GetInput(table, "A")); |
568 | 568 | EXPECT_EQ("ba", GetInput(table, "ba")); |
569 | 569 | EXPECT_EQ("BA", GetInput(table, "BA")); |
570 | 570 | EXPECT_EQ("Ba", GetInput(table, "Ba")); |
571 | EXPECT_EQ("<NULL>", GetInput(table, "bA")); | |
571 | EXPECT_EQ("<nullptr>", GetInput(table, "bA")); | |
572 | 572 | |
573 | 573 | // Test for HasSubRules |
574 | 574 | EXPECT_FALSE(table.HasSubRules("Z")); |
575 | 575 | |
576 | 576 | { // Test for LookUpPrefix |
577 | const Entry *entry = NULL; | |
577 | const Entry *entry = nullptr; | |
578 | 578 | size_t key_length = 0; |
579 | 579 | bool fixed = false; |
580 | 580 | entry = table.LookUpPrefix("bA", &key_length, &fixed); |
581 | EXPECT_TRUE(entry == NULL); | |
581 | EXPECT_TRUE(entry == nullptr); | |
582 | 582 | EXPECT_EQ(1, key_length); |
583 | 583 | EXPECT_TRUE(fixed); |
584 | 584 | } |
601 | 601 | EXPECT_EQ("[ba]", GetResult(table, "ba")); |
602 | 602 | EXPECT_EQ("[BA]", GetResult(table, "BA")); |
603 | 603 | EXPECT_EQ("[Ba]", GetResult(table, "Ba")); |
604 | EXPECT_EQ("<NULL>", GetResult(table, "bA")); | |
604 | EXPECT_EQ("<nullptr>", GetResult(table, "bA")); | |
605 | 605 | |
606 | 606 | EXPECT_EQ("a", GetInput(table, "a")); |
607 | 607 | EXPECT_EQ("A", GetInput(table, "A")); |
608 | 608 | EXPECT_EQ("ba", GetInput(table, "ba")); |
609 | 609 | EXPECT_EQ("BA", GetInput(table, "BA")); |
610 | 610 | EXPECT_EQ("Ba", GetInput(table, "Ba")); |
611 | EXPECT_EQ("<NULL>", GetInput(table, "bA")); | |
611 | EXPECT_EQ("<nullptr>", GetInput(table, "bA")); | |
612 | 612 | |
613 | 613 | // Test for HasSubRules |
614 | 614 | EXPECT_FALSE(table.HasSubRules("Z")); |
615 | 615 | |
616 | 616 | { // Test for LookUpPrefix |
617 | const Entry *entry = NULL; | |
617 | const Entry *entry = nullptr; | |
618 | 618 | size_t key_length = 0; |
619 | 619 | bool fixed = false; |
620 | 620 | entry = table.LookUpPrefix("bA", &key_length, &fixed); |
621 | EXPECT_TRUE(entry == NULL); | |
621 | EXPECT_TRUE(entry == nullptr); | |
622 | 622 | EXPECT_EQ(1, key_length); |
623 | 623 | EXPECT_TRUE(fixed); |
624 | 624 | } |
688 | 688 | mozc::composer::Table table; |
689 | 689 | table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_); |
690 | 690 | { |
691 | const mozc::composer::Entry *entry = NULL; | |
691 | const mozc::composer::Entry *entry = nullptr; | |
692 | 692 | size_t key_length = 0; |
693 | 693 | bool fixed = false; |
694 | 694 | entry = table.LookUpPrefix("2", &key_length, &fixed); |
699 | 699 | EXPECT_TRUE(fixed); |
700 | 700 | } |
701 | 701 | { |
702 | const mozc::composer::Entry *entry = NULL; | |
702 | const mozc::composer::Entry *entry = nullptr; | |
703 | 703 | size_t key_length = 0; |
704 | 704 | bool fixed = false; |
705 | 705 | entry = table.LookUpPrefix("し*", &key_length, &fixed); |
718 | 718 | mozc::commands::Request::TWELVE_KEYS_TO_HALFWIDTHASCII); |
719 | 719 | Table table; |
720 | 720 | table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_); |
721 | const mozc::composer::Entry *entry = NULL; | |
721 | const mozc::composer::Entry *entry = nullptr; | |
722 | 722 | size_t key_length = 0; |
723 | 723 | bool fixed = false; |
724 | 724 | entry = table.LookUpPrefix("2", &key_length, &fixed); |
736 | 736 | Table table; |
737 | 737 | table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_); |
738 | 738 | { |
739 | const mozc::composer::Entry *entry = NULL; | |
739 | const mozc::composer::Entry *entry = nullptr; | |
740 | 740 | size_t key_length = 0; |
741 | 741 | bool fixed = false; |
742 | 742 | entry = table.LookUpPrefix("しゃ*", &key_length, &fixed); |
784 | 784 | |
785 | 785 | const Entry *entry; |
786 | 786 | entry = table.LookUp("ww"); |
787 | EXPECT_TRUE(NULL != entry); | |
787 | EXPECT_TRUE(nullptr != entry); | |
788 | 788 | |
789 | 789 | size_t key_length; |
790 | 790 | bool fixed; |
791 | 791 | entry = table.LookUpPrefix("ww", &key_length, &fixed); |
792 | EXPECT_TRUE(NULL != entry); | |
792 | EXPECT_TRUE(nullptr != entry); | |
793 | 793 | EXPECT_EQ(2, key_length); |
794 | 794 | EXPECT_FALSE(fixed); |
795 | 795 | } |
800 | 800 | table.AddRule("www", "w", "ww"); |
801 | 801 | EXPECT_TRUE(table.HasSubRules("ww")); |
802 | 802 | |
803 | const Entry *entry = NULL; | |
803 | const Entry *entry = nullptr; | |
804 | 804 | entry = table.LookUp("ww"); |
805 | EXPECT_TRUE(NULL != entry); | |
805 | EXPECT_TRUE(nullptr != entry); | |
806 | 806 | |
807 | 807 | size_t key_length = 0; |
808 | 808 | bool fixed = false; |
809 | 809 | entry = table.LookUpPrefix("ww", &key_length, &fixed); |
810 | EXPECT_TRUE(NULL != entry); | |
810 | EXPECT_TRUE(nullptr != entry); | |
811 | 811 | EXPECT_EQ(2, key_length); |
812 | 812 | EXPECT_FALSE(fixed); |
813 | 813 | } |
825 | 825 | const Entry *entry = table.LookUpPrefix(kInput, &key_length, &fixed); |
826 | 826 | EXPECT_EQ(1, key_length); |
827 | 827 | EXPECT_TRUE(fixed); |
828 | ASSERT_TRUE(NULL != entry); | |
828 | ASSERT_TRUE(nullptr != entry); | |
829 | 829 | EXPECT_EQ(kInput, entry->input()); |
830 | 830 | EXPECT_EQ("", entry->result()); |
831 | 831 | EXPECT_EQ("a", entry->pending()); |
841 | 841 | entry = table.LookUpPrefix(kInput2, &key_length, &fixed); |
842 | 842 | EXPECT_EQ(2, key_length); |
843 | 843 | EXPECT_TRUE(fixed); |
844 | ASSERT_TRUE(NULL != entry); | |
844 | ASSERT_TRUE(nullptr != entry); | |
845 | 845 | EXPECT_EQ(kInput2, entry->input()); |
846 | 846 | EXPECT_EQ("", entry->result()); |
847 | 847 | EXPECT_EQ("b", entry->pending()); |
862 | 862 | Table table; |
863 | 863 | table.LoadFromString(kRule); |
864 | 864 | |
865 | const Entry *entry = NULL; | |
865 | const Entry *entry = nullptr; | |
866 | 866 | // Test for "a\t[A]\n" -- 2 entry rule |
867 | 867 | EXPECT_FALSE(table.HasNewChunkEntry("a")); |
868 | 868 | entry = table.LookUp("a"); |
869 | ASSERT_TRUE(NULL != entry); | |
869 | ASSERT_TRUE(nullptr != entry); | |
870 | 870 | EXPECT_EQ("[A]", entry->result()); |
871 | 871 | EXPECT_EQ("", entry->pending()); |
872 | 872 | |
873 | 873 | // Test for "kk\t[X]\tk\n" -- 3 entry rule |
874 | 874 | EXPECT_FALSE(table.HasNewChunkEntry("kk")); |
875 | 875 | entry = table.LookUp("kk"); |
876 | ASSERT_TRUE(NULL != entry); | |
876 | ASSERT_TRUE(nullptr != entry); | |
877 | 877 | EXPECT_EQ("[X]", entry->result()); |
878 | 878 | EXPECT_EQ("k", entry->pending()); |
879 | 879 | |
880 | 880 | // Test for "ww\t[W]\tw\tNewChunk\n" -- 3 entry rule + attribute rule |
881 | 881 | EXPECT_TRUE(table.HasNewChunkEntry("ww")); |
882 | 882 | entry = table.LookUp("ww"); |
883 | ASSERT_TRUE(NULL != entry); | |
883 | ASSERT_TRUE(nullptr != entry); | |
884 | 884 | EXPECT_EQ("[W]", entry->result()); |
885 | 885 | EXPECT_EQ("w", entry->pending()); |
886 | 886 | EXPECT_EQ(NEW_CHUNK, entry->attributes()); |
889 | 889 | // attribute rules |
890 | 890 | EXPECT_TRUE(table.HasNewChunkEntry("xx")); |
891 | 891 | entry = table.LookUp("xx"); |
892 | ASSERT_TRUE(NULL != entry); | |
892 | ASSERT_TRUE(nullptr != entry); | |
893 | 893 | EXPECT_EQ("[X]", entry->result()); |
894 | 894 | EXPECT_EQ("x", entry->pending()); |
895 | 895 | EXPECT_EQ((NEW_CHUNK | NO_TRANSLITERATION), entry->attributes()); |
898 | 898 | // -- all attributes |
899 | 899 | EXPECT_TRUE(table.HasNewChunkEntry("yy")); |
900 | 900 | entry = table.LookUp("yy"); |
901 | ASSERT_TRUE(NULL != entry); | |
901 | ASSERT_TRUE(nullptr != entry); | |
902 | 902 | EXPECT_EQ("[Y]", entry->result()); |
903 | 903 | EXPECT_EQ("y", entry->pending()); |
904 | 904 | EXPECT_EQ((NEW_CHUNK | NO_TRANSLITERATION | DIRECT_INPUT | END_CHUNK), |
906 | 906 | |
907 | 907 | // Test for "#\t[#]\n" -- This line starts with '#' but should be a rule. |
908 | 908 | entry = table.LookUp("#"); |
909 | ASSERT_TRUE(NULL != entry); | |
909 | ASSERT_TRUE(nullptr != entry); | |
910 | 910 | EXPECT_EQ("[#]", entry->result()); |
911 | 911 | EXPECT_EQ("", entry->pending()); |
912 | 912 | } |
919 | 919 | table.AddRule("x{{}", "X{", ""); |
920 | 920 | table.AddRule("xy", "XY", ""); |
921 | 921 | |
922 | const Entry *entry = NULL; | |
922 | const Entry *entry = nullptr; | |
923 | 923 | entry = table.LookUp("x{#1}y"); |
924 | EXPECT_TRUE(NULL == entry); | |
924 | EXPECT_TRUE(nullptr == entry); | |
925 | 925 | |
926 | 926 | std::string key; |
927 | 927 | key = Table::ParseSpecialKey("x{#1}y"); |
928 | 928 | entry = table.LookUp(key); |
929 | ASSERT_TRUE(NULL != entry); | |
929 | ASSERT_TRUE(nullptr != entry); | |
930 | 930 | EXPECT_EQ(key, entry->input()); |
931 | 931 | EXPECT_EQ("X1Y", entry->result()); |
932 | 932 | |
933 | 933 | key = Table::ParseSpecialKey("x{#2}y"); |
934 | 934 | entry = table.LookUp(key); |
935 | ASSERT_TRUE(NULL != entry); | |
935 | ASSERT_TRUE(nullptr != entry); | |
936 | 936 | EXPECT_EQ(key, entry->input()); |
937 | 937 | EXPECT_EQ("X2Y", entry->result()); |
938 | 938 | |
939 | 939 | key = "x{"; |
940 | 940 | entry = table.LookUp(key); |
941 | ASSERT_TRUE(NULL != entry); | |
941 | ASSERT_TRUE(nullptr != entry); | |
942 | 942 | EXPECT_EQ(key, entry->input()); |
943 | 943 | EXPECT_EQ("X{", entry->result()); |
944 | 944 | } |
1036 | 1036 | config.set_symbol_method(symbol_method[symbol]); |
1037 | 1037 | const Table *table = |
1038 | 1038 | table_manager.GetTable(request, config, mock_data_manager_); |
1039 | EXPECT_TRUE(table != NULL); | |
1039 | EXPECT_TRUE(table != nullptr); | |
1040 | 1040 | EXPECT_TRUE(table_manager.GetTable(request, config, |
1041 | 1041 | mock_data_manager_) == table); |
1042 | 1042 | EXPECT_TRUE(table_set.find(table) == table_set.end()); |
1059 | 1059 | config.set_custom_roman_table(kRule); |
1060 | 1060 | const Table *table = |
1061 | 1061 | table_manager.GetTable(request, config, mock_data_manager_); |
1062 | EXPECT_TRUE(table != NULL); | |
1062 | EXPECT_TRUE(table != nullptr); | |
1063 | 1063 | EXPECT_TRUE(table_manager.GetTable(request, config, mock_data_manager_) == |
1064 | 1064 | table); |
1065 | EXPECT_TRUE(NULL != table->LookUp("a")); | |
1066 | EXPECT_TRUE(NULL == table->LookUp("kk")); | |
1065 | EXPECT_TRUE(nullptr != table->LookUp("a")); | |
1066 | EXPECT_TRUE(nullptr == table->LookUp("kk")); | |
1067 | 1067 | |
1068 | 1068 | const std::string kRule2 = |
1069 | 1069 | "a\t[A]\n" // 2 entry rule |
1071 | 1071 | config.set_custom_roman_table(kRule2); |
1072 | 1072 | const Table *table2 = |
1073 | 1073 | table_manager.GetTable(request, config, mock_data_manager_); |
1074 | EXPECT_TRUE(table2 != NULL); | |
1074 | EXPECT_TRUE(table2 != nullptr); | |
1075 | 1075 | EXPECT_TRUE(table_manager.GetTable(request, config, mock_data_manager_) == |
1076 | 1076 | table2); |
1077 | EXPECT_TRUE(NULL != table2->LookUp("a")); | |
1078 | EXPECT_TRUE(NULL != table2->LookUp("kk")); | |
1077 | EXPECT_TRUE(nullptr != table2->LookUp("a")); | |
1078 | EXPECT_TRUE(nullptr != table2->LookUp("kk")); | |
1079 | 1079 | } |
1080 | 1080 | } |
1081 | 1081 |
211 | 211 | std::string tmp; |
212 | 212 | Util::HalfWidthToFullWidth(str, &tmp); |
213 | 213 | char32 ucs4 = 0; |
214 | if (Util::SplitFirstChar32(tmp, &ucs4, NULL) && ucs4 <= 0xffff) { | |
214 | if (Util::SplitFirstChar32(tmp, &ucs4, nullptr) && ucs4 <= 0xffff) { | |
215 | 215 | ucs2 = static_cast<uint16>(ucs4); |
216 | 216 | } else { |
217 | 217 | ucs2 = 0x0000; // no conversion as fall back |
240 | 240 | } |
241 | 241 | |
242 | 242 | CharacterFormManagerImpl::CharacterFormManagerImpl() |
243 | : storage_(NULL), require_consistent_conversion_(false) {} | |
243 | : storage_(nullptr), require_consistent_conversion_(false) {} | |
244 | 244 | |
245 | 245 | CharacterFormManagerImpl::~CharacterFormManagerImpl() {} |
246 | 246 | |
265 | 265 | } |
266 | 266 | |
267 | 267 | void CharacterFormManagerImpl::ClearHistory() { |
268 | if (storage_ != NULL) { | |
268 | if (storage_ != nullptr) { | |
269 | 269 | storage_->Clear(); |
270 | 270 | } |
271 | 271 | } |
306 | 306 | |
307 | 307 | Config::CharacterForm CharacterFormManagerImpl::GetCharacterFormFromStorage( |
308 | 308 | uint16 ucs2) const { |
309 | if (storage_ == NULL) { | |
309 | if (storage_ == nullptr) { | |
310 | 310 | return Config::FULL_WIDTH; // Return default setting |
311 | 311 | } |
312 | 312 | const std::string key(reinterpret_cast<const char *>(&ucs2), sizeof(ucs2)); |
313 | 313 | const char *value = storage_->Lookup(key); |
314 | if (value == NULL) { | |
314 | if (value == nullptr) { | |
315 | 315 | return Config::FULL_WIDTH; // Return default setting |
316 | 316 | } |
317 | 317 | const uint32 ivalue = *reinterpret_cast<const uint32 *>(value); |
324 | 324 | return; |
325 | 325 | } |
326 | 326 | |
327 | if (storage_ == NULL) { | |
327 | if (storage_ == nullptr) { | |
328 | 328 | return; |
329 | 329 | } |
330 | 330 | |
331 | 331 | const std::string key(reinterpret_cast<const char *>(&ucs2), sizeof(ucs2)); |
332 | 332 | const char *value = storage_->Lookup(key); |
333 | if (value != NULL && static_cast<Config::CharacterForm>(*value) == form) { | |
333 | if (value != nullptr && static_cast<Config::CharacterForm>(*value) == form) { | |
334 | 334 | return; |
335 | 335 | } |
336 | 336 | |
356 | 356 | |
357 | 357 | void CharacterFormManagerImpl::ConvertString(const std::string &str, |
358 | 358 | std::string *output) const { |
359 | ConvertStringWithAlternative(str, output, NULL); | |
359 | ConvertStringWithAlternative(str, output, nullptr); | |
360 | 360 | } |
361 | 361 | |
362 | 362 | bool CharacterFormManagerImpl::TryConvertStringWithPreference( |
474 | 474 | *output = str; |
475 | 475 | } |
476 | 476 | |
477 | if (alternative_output != NULL) { | |
477 | if (alternative_output != nullptr) { | |
478 | 478 | alternative_output->clear(); |
479 | 479 | ConvertStringAlternative(*output, alternative_output); |
480 | 480 | } |
481 | 481 | |
482 | 482 | // return true if alternative_output and output are different |
483 | return (alternative_output != NULL && *alternative_output != *output); | |
483 | return (alternative_output != nullptr && *alternative_output != *output); | |
484 | 484 | } |
485 | 485 | |
486 | 486 | void CharacterFormManagerImpl::Clear() { |
563 | 563 | const uint32 key_type = 0; |
564 | 564 | storage_.reset(LRUStorage::Create(filename.c_str(), sizeof(key_type), |
565 | 565 | kLRUSize, kSeedValue)); |
566 | LOG_IF(ERROR, storage_.get() == NULL) << "cannot open " << filename; | |
566 | LOG_IF(ERROR, storage_.get() == nullptr) << "cannot open " << filename; | |
567 | 567 | preedit_.reset(new PreeditCharacterFormManagerImpl); |
568 | 568 | conversion_.reset(new ConversionCharacterFormManagerImpl); |
569 | 569 | preedit_->set_storage(storage_.get()); |
68 | 68 | // Converts string according to the config rules. |
69 | 69 | // if alternate output, which should be shown next to |
70 | 70 | // the output, is defined, it is stored into alternative_output. |
71 | // If alternative_output is not required, you can pass NULL. | |
71 | // If alternative_output is not required, you can pass nullptr. | |
72 | 72 | // e.g., output = "@" alternative_output = "@" |
73 | 73 | // return true if both output and alternative_output are defined. |
74 | 74 | bool ConvertPreeditStringWithAlternative( |
213 | 213 | Config input_proto; |
214 | 214 | bool ret_code = true; |
215 | 215 | |
216 | if (is.get() == NULL) { | |
216 | if (is == nullptr) { | |
217 | 217 | LOG(ERROR) << filename_ << " is not found"; |
218 | 218 | ret_code = false; |
219 | 219 | } else if (!input_proto.ParseFromIstream(is.get())) { |
273 | 273 | DISALLOW_COPY_AND_ASSIGN(NullStatsConfigUtilImpl); |
274 | 274 | }; |
275 | 275 | |
276 | StatsConfigUtilInterface *g_stats_config_util_handler = NULL; | |
276 | StatsConfigUtilInterface *g_stats_config_util_handler = nullptr; | |
277 | 277 | |
278 | 278 | // GetStatsConfigUtil and SetHandler are not thread safe. |
279 | 279 | |
294 | 294 | #endif |
295 | 295 | |
296 | 296 | StatsConfigUtilInterface &GetStatsConfigUtil() { |
297 | if (g_stats_config_util_handler == NULL) { | |
297 | if (g_stats_config_util_handler == nullptr) { | |
298 | 298 | return *(Singleton<DefaultConfigUtilImpl>::get()); |
299 | 299 | } else { |
300 | 300 | return *g_stats_config_util_handler; |
70 | 70 | const int kRunLevelHigh = 2; |
71 | 71 | |
72 | 72 | bool TryGetKnownKey(HKEY key, LPCWSTR sub_key, HKEY *result_key) { |
73 | HKEY dummy = NULL; | |
74 | HKEY &result = (result_key != NULL ? *result_key : dummy); | |
73 | HKEY dummy = nullptr; | |
74 | HKEY &result = (result_key != nullptr ? *result_key : dummy); | |
75 | 75 | if (HKEY_CURRENT_USER == key) { |
76 | 76 | if (std::wstring(kOmahaUsageKey) == sub_key) { |
77 | 77 | result = kHKCU_ClientState; |
162 | 162 | if (!HasUsagestatsValue(key)) { |
163 | 163 | return false; |
164 | 164 | } |
165 | if (value != NULL) { | |
165 | if (value != nullptr) { | |
166 | 166 | *value = |
167 | 167 | mozc::Singleton<Property>::get()->get_entry_from_usagestats_map(key); |
168 | 168 | } |
202 | 202 | HKEY key, LPCWSTR sub_key, DWORD reserved, LPWSTR class_name, |
203 | 203 | DWORD options, REGSAM sam, LPSECURITY_ATTRIBUTES security_attributes, |
204 | 204 | PHKEY result, LPDWORD disposition) { |
205 | HKEY dummy = NULL; | |
206 | HKEY &result_key = result != NULL ? *result : dummy; | |
205 | HKEY dummy = nullptr; | |
206 | HKEY &result_key = result != nullptr ? *result : dummy; | |
207 | 207 | if (!TryGetKnownKey(key, sub_key, &result_key)) { |
208 | 208 | return ERROR_ACCESS_DENIED; |
209 | 209 | } |
244 | 244 | return ERROR_FILE_NOT_FOUND; |
245 | 245 | } |
246 | 246 | GetUsagestatsValue(key, reinterpret_cast<DWORD *>(data)); |
247 | if (type != NULL) { | |
247 | if (type != nullptr) { | |
248 | 248 | *type = REG_DWORD; |
249 | 249 | } |
250 | 250 | return ERROR_SUCCESS; |
69 | 69 | node_freelist_.reset(new FreeList<Node>(1024)); |
70 | 70 | pos_matcher_.Set(mock_data_manager_.GetPOSMatcherData()); |
71 | 71 | { |
72 | const char *data = NULL; | |
72 | const char *data = nullptr; | |
73 | 73 | size_t size = 0; |
74 | 74 | mock_data_manager_.GetSuggestionFilterData(&data, &size); |
75 | 75 | suggestion_filter_.reset(new SuggestionFilter(data, size)); |
409 | 409 | bool ConverterImpl::StartPredictionForRequest(const ConversionRequest &request, |
410 | 410 | Segments *segments) const { |
411 | 411 | if (!request.has_composer()) { |
412 | LOG(ERROR) << "Composer is NULL"; | |
412 | LOG(ERROR) << "Composer is nullptr"; | |
413 | 413 | return false; |
414 | 414 | } |
415 | 415 |
174 | 174 | |
175 | 175 | inline int GetCost(const Node *lnode, const Node *rnode) const { |
176 | 176 | const int kInvalidPenaltyCost = 100000; |
177 | if (rnode->constrained_prev != NULL && lnode != rnode->constrained_prev) { | |
177 | if (rnode->constrained_prev != nullptr && | |
178 | lnode != rnode->constrained_prev) { | |
178 | 179 | return kInvalidPenaltyCost; |
179 | 180 | } |
180 | 181 | return connector_->GetTransitionCost(lnode->rid, rnode->lid) + rnode->wcost; |
477 | 477 | size_t *length) const { |
478 | 478 | if (!IsAvailable()) { |
479 | 479 | *length = 0; |
480 | return NULL; | |
480 | return nullptr; | |
481 | 481 | } |
482 | 482 | |
483 | 483 | if (mode_ == KANA) { |
484 | 484 | *length = 0; |
485 | return NULL; | |
485 | return nullptr; | |
486 | 486 | } |
487 | 487 | |
488 | 488 | const size_t corrected_key_pos = GetCorrectedPosition(original_key_pos); |
489 | 489 | if (!IsValidPosition(corrected_key_pos)) { |
490 | 490 | *length = 0; |
491 | return NULL; | |
491 | return nullptr; | |
492 | 492 | } |
493 | 493 | |
494 | 494 | const char *corrected_substr = corrected_key_.data() + corrected_key_pos; |
503 | 503 | } |
504 | 504 | |
505 | 505 | *length = 0; |
506 | return NULL; | |
506 | return nullptr; | |
507 | 507 | } |
508 | 508 | |
509 | 509 | size_t KeyCorrector::GetOriginalOffset(const size_t original_key_pos, |
83 | 83 | |
84 | 84 | // return new prefix of string correspoindng to |
85 | 85 | // the prefix of the original key at "original_key_pos" |
86 | // if new prefix and original prefix are the same, return NULL. | |
87 | // Note that return value won't be NULL terminated. | |
86 | // if new prefix and original prefix are the same, return nullptr. | |
87 | // Note that return value won't be nullptr terminated. | |
88 | 88 | // "length" stores the length of return value. |
89 | 89 | // We don't allow empty matching (see GetPrefix(15) below) |
90 | 90 | // |
101 | 101 | // GetPrefix(3) = "かいじゅうのはっぱ" |
102 | 102 | // GetPrefix(9) = "じゅうのはっぱ" |
103 | 103 | // GetPrefix(12) = "ゅうのはっぱ" |
104 | // GetPrefix(15) = NULL (not "うのはっぱ") | |
104 | // GetPrefix(15) = nullptr (not "うのはっぱ") | |
105 | 105 | // "う" itself doesn't correspond to the original key, |
106 | 106 | // so, we don't make a new prefix |
107 | // GetPrefix(18) = NULL (same as original) | |
107 | // GetPrefix(18) = nullptr (same as original) | |
108 | 108 | // |
109 | 109 | // Example2: |
110 | 110 | // original: "みんあのほん" |
111 | 111 | // GetPrefix(0) = "みんなのほん" |
112 | 112 | // GetPrefix(3) = "んなのほん" |
113 | 113 | // GetPrefix(9) = "なのほん" |
114 | // GetPrefix(12) = NULL | |
114 | // GetPrefix(12) = nullptr | |
115 | 115 | const char *GetCorrectedPrefix(const size_t original_key_pos, |
116 | 116 | size_t *length) const; |
117 | 117 |
362 | 362 | size_t length = 0; |
363 | 363 | |
364 | 364 | // same as the original key |
365 | EXPECT_TRUE(NULL == corrector.GetCorrectedPrefix(0, &length)); | |
366 | EXPECT_TRUE(NULL == corrector.GetCorrectedPrefix(1, &length)); | |
367 | EXPECT_TRUE(NULL == corrector.GetCorrectedPrefix(2, &length)); | |
368 | EXPECT_TRUE(NULL == corrector.GetCorrectedPrefix(3, &length)); | |
365 | EXPECT_TRUE(nullptr == corrector.GetCorrectedPrefix(0, &length)); | |
366 | EXPECT_TRUE(nullptr == corrector.GetCorrectedPrefix(1, &length)); | |
367 | EXPECT_TRUE(nullptr == corrector.GetCorrectedPrefix(2, &length)); | |
368 | EXPECT_TRUE(nullptr == corrector.GetCorrectedPrefix(3, &length)); | |
369 | 369 | } |
370 | 370 | |
371 | 371 | { |
374 | 374 | EXPECT_TRUE(corrector.IsAvailable()); |
375 | 375 | EXPECT_EQ("みんなであそぼう", corrector.corrected_key()); |
376 | 376 | |
377 | const char *output = NULL; | |
377 | const char *output = nullptr; | |
378 | 378 | size_t length = 0; |
379 | 379 | |
380 | 380 | output = corrector.GetCorrectedPrefix(0, &length); |
388 | 388 | |
389 | 389 | output = corrector.GetCorrectedPrefix(9, &length); |
390 | 390 | // same |
391 | EXPECT_TRUE(NULL == output); | |
391 | EXPECT_TRUE(nullptr == output); | |
392 | 392 | } |
393 | 393 | |
394 | 394 | { |
397 | 397 | EXPECT_TRUE(corrector.IsAvailable()); |
398 | 398 | EXPECT_EQ("こんにちは", corrector.corrected_key()); |
399 | 399 | |
400 | const char *output = NULL; | |
400 | const char *output = nullptr; | |
401 | 401 | size_t length = 0; |
402 | 402 | |
403 | 403 | output = corrector.GetCorrectedPrefix(0, &length); |
404 | 404 | EXPECT_EQ("こんにちは", std::string(output, length)); |
405 | 405 | |
406 | 406 | output = corrector.GetCorrectedPrefix(3, &length); |
407 | EXPECT_TRUE(NULL == output); | |
407 | EXPECT_TRUE(nullptr == output); | |
408 | 408 | |
409 | 409 | output = corrector.GetCorrectedPrefix(6, &length); |
410 | EXPECT_TRUE(NULL == output); | |
410 | EXPECT_TRUE(nullptr == output); | |
411 | 411 | |
412 | 412 | output = corrector.GetCorrectedPrefix(9, &length); |
413 | EXPECT_TRUE(NULL == output); | |
413 | EXPECT_TRUE(nullptr == output); | |
414 | 414 | } |
415 | 415 | |
416 | 416 | { |
419 | 419 | EXPECT_TRUE(corrector.IsAvailable()); |
420 | 420 | EXPECT_EQ("こんにちはせんよう", corrector.corrected_key()); |
421 | 421 | |
422 | const char *output = NULL; | |
422 | const char *output = nullptr; | |
423 | 423 | size_t length = 0; |
424 | 424 | |
425 | 425 | output = corrector.GetCorrectedPrefix(0, &length); |
426 | 426 | EXPECT_EQ("こんにちはせんよう", std::string(output, length)); |
427 | 427 | |
428 | 428 | output = corrector.GetCorrectedPrefix(3, &length); |
429 | EXPECT_TRUE(NULL == output); | |
429 | EXPECT_TRUE(nullptr == output); | |
430 | 430 | |
431 | 431 | output = corrector.GetCorrectedPrefix(6, &length); |
432 | EXPECT_TRUE(NULL == output); | |
432 | EXPECT_TRUE(nullptr == output); | |
433 | 433 | |
434 | 434 | output = corrector.GetCorrectedPrefix(9, &length); |
435 | 435 | EXPECT_EQ("にちはせんよう", std::string(output, length)); |
526 | 526 | size_t length = 0; |
527 | 527 | |
528 | 528 | // same as the original key |
529 | EXPECT_TRUE(NULL == corrector.GetCorrectedPrefix(0, &length)); | |
530 | EXPECT_TRUE(NULL == corrector.GetCorrectedPrefix(1, &length)); | |
531 | EXPECT_TRUE(NULL == corrector.GetCorrectedPrefix(2, &length)); | |
532 | EXPECT_TRUE(NULL == corrector.GetCorrectedPrefix(3, &length)); | |
529 | EXPECT_TRUE(nullptr == corrector.GetCorrectedPrefix(0, &length)); | |
530 | EXPECT_TRUE(nullptr == corrector.GetCorrectedPrefix(1, &length)); | |
531 | EXPECT_TRUE(nullptr == corrector.GetCorrectedPrefix(2, &length)); | |
532 | EXPECT_TRUE(nullptr == corrector.GetCorrectedPrefix(3, &length)); | |
533 | 533 | } |
534 | 534 | } |
535 | 535 |
56 | 56 | bos_node->cost = 0; |
57 | 57 | bos_node->begin_pos = length; |
58 | 58 | bos_node->end_pos = length; |
59 | bos_node->enext = NULL; | |
59 | bos_node->enext = nullptr; | |
60 | 60 | return bos_node; |
61 | 61 | } |
62 | 62 | |
72 | 72 | eos_node->cost = 0; |
73 | 73 | eos_node->begin_pos = length; |
74 | 74 | eos_node->end_pos = length; |
75 | eos_node->bnext = NULL; | |
75 | eos_node->bnext = nullptr; | |
76 | 76 | return eos_node; |
77 | 77 | } |
78 | 78 | |
79 | 79 | bool PathContainsString(const Node *node, size_t begin_pos, size_t end_pos, |
80 | 80 | const std::string &str) { |
81 | 81 | CHECK(node); |
82 | for (; node->prev != NULL; node = node->prev) { | |
82 | for (; node->prev != nullptr; node = node->prev) { | |
83 | 83 | if (node->begin_pos == begin_pos && node->end_pos == end_pos && |
84 | 84 | node->value == str) { |
85 | 85 | return true; |
109 | 109 | for (const Node *node = end_node; node; node = node->prev) { |
110 | 110 | node_vector.push_back(node); |
111 | 111 | } |
112 | const Node *prev_node = NULL; | |
112 | const Node *prev_node = nullptr; | |
113 | 113 | |
114 | 114 | for (int i = static_cast<int>(node_vector.size()) - 1; i >= 0; --i) { |
115 | 115 | const Node *node = node_vector[i]; |
163 | 163 | cache_info_.resize(size + 4); |
164 | 164 | |
165 | 165 | std::fill(begin_nodes_.begin(), begin_nodes_.end(), |
166 | static_cast<Node *>(NULL)); | |
167 | std::fill(end_nodes_.begin(), end_nodes_.end(), static_cast<Node *>(NULL)); | |
166 | static_cast<Node *>(nullptr)); | |
167 | std::fill(end_nodes_.begin(), end_nodes_.end(), static_cast<Node *>(nullptr)); | |
168 | 168 | std::fill(cache_info_.begin(), cache_info_.end(), 0); |
169 | 169 | |
170 | 170 | end_nodes_[0] = InitBOSNode(this, static_cast<uint16>(0)); |
177 | 177 | Node *Lattice::eos_nodes() const { return begin_nodes_[key_.size()]; } |
178 | 178 | |
179 | 179 | void Lattice::Insert(size_t pos, Node *node) { |
180 | for (Node *rnode = node; rnode != NULL; rnode = rnode->bnext) { | |
180 | for (Node *rnode = node; rnode != nullptr; rnode = rnode->bnext) { | |
181 | 181 | const size_t end_pos = std::min(rnode->key.size() + pos, key_.size()); |
182 | 182 | rnode->begin_pos = static_cast<uint16>(pos); |
183 | 183 | rnode->end_pos = static_cast<uint16>(end_pos); |
184 | rnode->prev = NULL; | |
185 | rnode->next = NULL; | |
184 | rnode->prev = nullptr; | |
185 | rnode->next = nullptr; | |
186 | 186 | rnode->cost = 0; |
187 | 187 | rnode->enext = end_nodes_[end_pos]; |
188 | 188 | end_nodes_[end_pos] = rnode; |
189 | 189 | } |
190 | 190 | |
191 | if (begin_nodes_[pos] == NULL) { | |
191 | if (begin_nodes_[pos] == nullptr) { | |
192 | 192 | begin_nodes_[pos] = node; |
193 | 193 | } else { |
194 | for (Node *rnode = node; rnode != NULL; rnode = rnode->bnext) { | |
195 | if (rnode->bnext == NULL) { | |
194 | for (Node *rnode = node; rnode != nullptr; rnode = rnode->bnext) { | |
195 | if (rnode->bnext == nullptr) { | |
196 | 196 | rnode->bnext = begin_nodes_[pos]; |
197 | 197 | begin_nodes_[pos] = node; |
198 | 198 | break; |
266 | 266 | end_nodes_.resize(new_size + 4); |
267 | 267 | |
268 | 268 | std::fill(begin_nodes_.begin() + old_size, begin_nodes_.end(), |
269 | static_cast<Node *>(NULL)); | |
269 | static_cast<Node *>(nullptr)); | |
270 | 270 | std::fill(end_nodes_.begin() + old_size + 1, end_nodes_.end(), |
271 | static_cast<Node *>(NULL)); | |
271 | static_cast<Node *>(nullptr)); | |
272 | 272 | |
273 | 273 | end_nodes_[0] = InitBOSNode(this, static_cast<uint16>(0)); |
274 | 274 | begin_nodes_[new_size] = InitEOSNode(this, static_cast<uint16>(new_size)); |
290 | 290 | // erase nodes whose end position exceeds new_len |
291 | 291 | for (size_t i = 0; i < new_len; ++i) { |
292 | 292 | Node *begin = begin_nodes_[i]; |
293 | if (begin == NULL) { | |
293 | if (begin == nullptr) { | |
294 | 294 | continue; |
295 | 295 | } |
296 | 296 | |
297 | for (Node *prev = begin, *curr = begin->bnext; curr != NULL;) { | |
297 | for (Node *prev = begin, *curr = begin->bnext; curr != nullptr;) { | |
298 | 298 | CHECK(prev); |
299 | 299 | if (curr->end_pos > new_len) { |
300 | 300 | prev->bnext = curr->bnext; |
311 | 311 | |
312 | 312 | // update begin_nodes and end_nodes |
313 | 313 | for (size_t i = new_len; i <= old_len; ++i) { |
314 | begin_nodes_[i] = NULL; | |
314 | begin_nodes_[i] = nullptr; | |
315 | 315 | } |
316 | 316 | for (size_t i = new_len + 1; i <= old_len; ++i) { |
317 | end_nodes_[i] = NULL; | |
317 | end_nodes_[i] = nullptr; | |
318 | 318 | } |
319 | 319 | begin_nodes_[new_len] = InitEOSNode(this, static_cast<uint16>(new_len)); |
320 | 320 | |
340 | 340 | |
341 | 341 | void Lattice::ResetNodeCost() { |
342 | 342 | for (size_t i = 0; i <= key_.size(); ++i) { |
343 | if (begin_nodes_[i] != NULL) { | |
344 | Node *prev = NULL; | |
345 | for (Node *node = begin_nodes_[i]; node != NULL; node = node->bnext) { | |
343 | if (begin_nodes_[i] != nullptr) { | |
344 | Node *prev = nullptr; | |
345 | for (Node *node = begin_nodes_[i]; node != nullptr; node = node->bnext) { | |
346 | 346 | // do not process BOS / EOS nodes |
347 | 347 | if (node->node_type == Node::BOS_NODE || |
348 | 348 | node->node_type == Node::EOS_NODE) { |
354 | 354 | node->wcost = node->raw_wcost; |
355 | 355 | } else { |
356 | 356 | if (node == begin_nodes_[i]) { |
357 | if (node->bnext == NULL) { | |
358 | begin_nodes_[i] = NULL; | |
357 | if (node->bnext == nullptr) { | |
358 | begin_nodes_[i] = nullptr; | |
359 | 359 | } else { |
360 | 360 | begin_nodes_[i] = node->bnext; |
361 | 361 | } |
370 | 370 | } |
371 | 371 | } |
372 | 372 | |
373 | if (end_nodes_[i] != NULL) { | |
374 | Node *prev = NULL; | |
375 | for (Node *node = end_nodes_[i]; node != NULL; node = node->enext) { | |
373 | if (end_nodes_[i] != nullptr) { | |
374 | Node *prev = nullptr; | |
375 | for (Node *node = end_nodes_[i]; node != nullptr; node = node->enext) { | |
376 | 376 | if (node->node_type == Node::BOS_NODE || |
377 | 377 | node->node_type == Node::EOS_NODE) { |
378 | 378 | continue; |
381 | 381 | node->wcost = node->raw_wcost; |
382 | 382 | } else { |
383 | 383 | if (node == end_nodes_[i]) { |
384 | if (node->enext == NULL) { | |
385 | end_nodes_[i] = NULL; | |
384 | if (node->enext == nullptr) { | |
385 | end_nodes_[i] = nullptr; | |
386 | 386 | } else { |
387 | 387 | end_nodes_[i] = node->enext; |
388 | 388 | } |
418 | 418 | return os.str(); |
419 | 419 | } |
420 | 420 | |
421 | for (; node != NULL; node = node->prev) { | |
421 | for (; node != nullptr; node = node->prev) { | |
422 | 422 | best_path_nodes.push_back(node); |
423 | 423 | } |
424 | 424 |
61 | 61 | TEST(LatticeTest, NewNodeTest) { |
62 | 62 | Lattice lattice; |
63 | 63 | Node *node = lattice.NewNode(); |
64 | EXPECT_TRUE(node != NULL); | |
64 | EXPECT_TRUE(node != nullptr); | |
65 | 65 | EXPECT_EQ(0, node->lid); |
66 | 66 | EXPECT_EQ(0, node->rid); |
67 | 67 | } |
95 | 95 | |
96 | 96 | int size = 0; |
97 | 97 | Node *node3 = lattice.end_nodes(3); |
98 | for (; node3 != NULL; node3 = node3->enext) { | |
98 | for (; node3 != nullptr; node3 = node3->enext) { | |
99 | 99 | ++size; |
100 | 100 | } |
101 | 101 | EXPECT_EQ(2, size); |
152 | 152 | // check for begin_nodes |
153 | 153 | if (i < key_size) { |
154 | 154 | std::set<int> lengths; |
155 | for (Node *node = lattice.begin_nodes(i); node != NULL; | |
155 | for (Node *node = lattice.begin_nodes(i); node != nullptr; | |
156 | 156 | node = node->bnext) { |
157 | 157 | lengths.insert(node->key.size()); |
158 | 158 | } |
161 | 161 | // check for end_nodes |
162 | 162 | if (i > 0) { |
163 | 163 | std::set<int> lengths; |
164 | for (Node *node = lattice.end_nodes(i); node != NULL; | |
164 | for (Node *node = lattice.end_nodes(i); node != nullptr; | |
165 | 165 | node = node->enext) { |
166 | 166 | lengths.insert(node->key.size()); |
167 | 167 | } |
202 | 202 | // check for begin_nodes |
203 | 203 | if (i < key_size) { |
204 | 204 | std::set<int> lengths; |
205 | for (Node *node = lattice.begin_nodes(i); node != NULL; | |
205 | for (Node *node = lattice.begin_nodes(i); node != nullptr; | |
206 | 206 | node = node->bnext) { |
207 | 207 | lengths.insert(node->key.size()); |
208 | 208 | } |
211 | 211 | // check for end_nodes |
212 | 212 | if (i > 0) { |
213 | 213 | std::set<int> lengths; |
214 | for (Node *node = lattice.end_nodes(i); node != NULL; | |
214 | for (Node *node = lattice.end_nodes(i); node != nullptr; | |
215 | 215 | node = node->enext) { |
216 | 216 | lengths.insert(node->key.size()); |
217 | 217 | } |
113 | 113 | connector_(connector), |
114 | 114 | pos_matcher_(pos_matcher), |
115 | 115 | lattice_(lattice), |
116 | begin_node_(NULL), | |
117 | end_node_(NULL), | |
116 | begin_node_(nullptr), | |
117 | end_node_(nullptr), | |
118 | 118 | freelist_(kFreeListSize), |
119 | 119 | filter_(new CandidateFilter(suppression_dic, pos_matcher, |
120 | 120 | suggestion_filter, |
121 | 121 | apply_suggestion_filter_for_exact_match)), |
122 | 122 | viterbi_result_checked_(false), |
123 | 123 | check_mode_(STRICT), |
124 | boundary_checker_(NULL) { | |
124 | boundary_checker_(nullptr) { | |
125 | 125 | DCHECK(suppression_dictionary_); |
126 | 126 | DCHECK(segmenter); |
127 | 127 | DCHECK(connector); |
128 | if (lattice_ == NULL || !lattice_->has_lattice()) { | |
128 | if (lattice_ == nullptr || !lattice_->has_lattice()) { | |
129 | 129 | LOG(ERROR) << "lattice is not available"; |
130 | 130 | return; |
131 | 131 | } |
146 | 146 | begin_node_ = begin_node; |
147 | 147 | end_node_ = end_node; |
148 | 148 | |
149 | for (Node *node = lattice_->begin_nodes(end_node_->begin_pos); node != NULL; | |
150 | node = node->bnext) { | |
149 | for (Node *node = lattice_->begin_nodes(end_node_->begin_pos); | |
150 | node != nullptr; node = node->bnext) { | |
151 | 151 | if (node == end_node_ || (node->lid != end_node_->lid && |
152 | 152 | node->cost - end_node_->cost <= kCostDiff && |
153 | 153 | node->prev != end_node_->prev)) { |
154 | 154 | // Push "EOS" nodes. |
155 | agenda_.Push(CreateNewElement(node, NULL, node->cost, 0, 0, 0)); | |
155 | agenda_.Push(CreateNewElement(node, nullptr, node->cost, 0, 0, 0)); | |
156 | 156 | } |
157 | 157 | } |
158 | 158 | |
187 | 187 | bool is_functional = false; |
188 | 188 | for (size_t i = 0; i < nodes.size(); ++i) { |
189 | 189 | const Node *node = nodes[i]; |
190 | DCHECK(node != NULL); | |
190 | DCHECK(node != nullptr); | |
191 | 191 | if (!is_functional && !pos_matcher_->IsFunctional(node->lid)) { |
192 | 192 | candidate->content_value += node->value; |
193 | 193 | candidate->content_key += node->key; |
197 | 197 | candidate->key += node->key; |
198 | 198 | candidate->value += node->value; |
199 | 199 | |
200 | if (node->constrained_prev != NULL || | |
201 | (node->next != NULL && node->next->constrained_prev == node)) { | |
200 | if (node->constrained_prev != nullptr || | |
201 | (node->next != nullptr && node->next->constrained_prev == node)) { | |
202 | 202 | // If result has constrained_node, set CONTEXT_SENSITIVE. |
203 | 203 | // If a node has constrained node, the node is generated by |
204 | 204 | // a) compound node and resegmented via personal name resegmentation |
279 | 279 | DCHECK(end_node_); |
280 | 280 | |
281 | 281 | DCHECK(candidate); |
282 | if (lattice_ == NULL || !lattice_->has_lattice()) { | |
282 | if (lattice_ == nullptr || !lattice_->has_lattice()) { | |
283 | 283 | LOG(ERROR) << "Must create lattice in advance"; |
284 | 284 | return false; |
285 | 285 | } |
345 | 345 | // reached to the goal. |
346 | 346 | if (rnode->end_pos == begin_node_->end_pos) { |
347 | 347 | nodes_.clear(); |
348 | for (const QueueElement *elm = top->next; elm->next != NULL; | |
348 | for (const QueueElement *elm = top->next; elm->next != nullptr; | |
349 | 349 | elm = elm->next) { |
350 | 350 | nodes_.push_back(elm->node); |
351 | 351 | } |
367 | 367 | // do nothing |
368 | 368 | } |
369 | 369 | } else { |
370 | const QueueElement *best_left_elm = NULL; | |
370 | const QueueElement *best_left_elm = nullptr; | |
371 | 371 | const bool is_right_edge = rnode->begin_pos == end_node_->begin_pos; |
372 | 372 | const bool is_left_edge = rnode->begin_pos == begin_node_->end_pos; |
373 | 373 | DCHECK(!(is_right_edge && is_left_edge)); |
376 | 376 | // begin/end node regardless of its value. |
377 | 377 | const bool is_edge = (is_right_edge || is_left_edge); |
378 | 378 | |
379 | for (Node *lnode = lattice_->end_nodes(rnode->begin_pos); lnode != NULL; | |
380 | lnode = lnode->enext) { | |
379 | for (Node *lnode = lattice_->end_nodes(rnode->begin_pos); | |
380 | lnode != nullptr; lnode = lnode->enext) { | |
381 | 381 | // is_invalid_position is true if the lnode's location is invalid |
382 | 382 | // 1. |<-- begin_node_-->| |
383 | 383 | // |<--lnode-->| <== overlapped. |
414 | 414 | continue; |
415 | 415 | } |
416 | 416 | |
417 | DCHECK(this->boundary_checker_ != NULL); | |
417 | DCHECK(this->boundary_checker_ != nullptr); | |
418 | 418 | BoundaryCheckResult boundary_result = |
419 | 419 | (this->*boundary_checker_)(lnode, rnode, is_edge); |
420 | 420 | if (boundary_result == INVALID) { |
467 | 467 | // Even if expand all left nodes, all the |value| part should |
468 | 468 | // be identical. Here, we simply use the best left edge node. |
469 | 469 | // This hack reduces the number of redundant calls of pop(). |
470 | if (best_left_elm == NULL || best_left_elm->fx > fx) { | |
470 | if (best_left_elm == nullptr || best_left_elm->fx > fx) { | |
471 | 471 | best_left_elm = |
472 | 472 | CreateNewElement(lnode, top, fx, gx, structure_gx, w_gx); |
473 | 473 | } |
477 | 477 | } |
478 | 478 | } |
479 | 479 | |
480 | if (best_left_elm != NULL) { | |
480 | if (best_left_elm != nullptr) { | |
481 | 481 | agenda_.Push(best_left_elm); |
482 | 482 | } |
483 | 483 | } |
591 | 591 | int NBestGenerator::GetTransitionCost(const Node *lnode, |
592 | 592 | const Node *rnode) const { |
593 | 593 | const int kInvalidPenaltyCost = 100000; |
594 | if (rnode->constrained_prev != NULL && lnode != rnode->constrained_prev) { | |
594 | if (rnode->constrained_prev != nullptr && lnode != rnode->constrained_prev) { | |
595 | 595 | return kInvalidPenaltyCost; |
596 | 596 | } |
597 | 597 | return connector_->GetTransitionCost(lnode->rid, rnode->lid); |
167 | 167 | const Segments &segments, const Node &begin_node, |
168 | 168 | const std::vector<uint16> &group, |
169 | 169 | bool is_single_segment) { |
170 | const Node *end_node = NULL; | |
171 | for (Node *node = begin_node.next; node->next != NULL; node = node->next) { | |
170 | const Node *end_node = nullptr; | |
171 | for (Node *node = begin_node.next; node->next != nullptr; | |
172 | node = node->next) { | |
172 | 173 | end_node = node->next; |
173 | 174 | if (converter.IsSegmentEndNode(segments, node, group, |
174 | 175 | is_single_segment)) { |
48 | 48 | class BaseNodeListBuilder : public dictionary::DictionaryInterface::Callback { |
49 | 49 | public: |
50 | 50 | BaseNodeListBuilder(mozc::NodeAllocator *allocator, int limit) |
51 | : allocator_(allocator), limit_(limit), penalty_(0), result_(NULL) { | |
52 | DCHECK(allocator_) << "Allocator must not be NULL"; | |
51 | : allocator_(allocator), limit_(limit), penalty_(0), result_(nullptr) { | |
52 | DCHECK(allocator_) << "Allocator must not be nullptr"; | |
53 | 53 | } |
54 | 54 | |
55 | 55 | BaseNodeListBuilder(const BaseNodeListBuilder &) = delete; |
43 | 43 | namespace internal { |
44 | 44 | |
45 | 45 | PosIdPrinter::PosIdPrinter(std::istream *id_def) { |
46 | if (id_def == NULL) { | |
46 | if (id_def == nullptr) { | |
47 | 47 | return; |
48 | 48 | } |
49 | 49 |
67 | 67 | } |
68 | 68 | |
69 | 69 | TEST_F(PosIdPrinterTest, NullInput) { |
70 | PosIdPrinter pos_id_printer(NULL); | |
70 | PosIdPrinter pos_id_printer(nullptr); | |
71 | 71 | EXPECT_EQ("", pos_id_printer.IdToString(-1)); |
72 | 72 | EXPECT_EQ("", pos_id_printer.IdToString(1934)); |
73 | 73 | } |
249 | 249 | } |
250 | 250 | |
251 | 251 | int Segment::indexOf(const Segment::Candidate *candidate) { |
252 | if (candidate == NULL) { | |
252 | if (candidate == nullptr) { | |
253 | 253 | return static_cast<int>(candidates_size()); |
254 | 254 | } |
255 | 255 |
187 | 187 | EXPECT_TRUE(segment.is_valid_index(-kMetaCandidatesSize)); |
188 | 188 | EXPECT_FALSE(segment.is_valid_index(-kMetaCandidatesSize - 1)); |
189 | 189 | |
190 | EXPECT_EQ(segment.candidates_size(), segment.indexOf(NULL)); | |
190 | EXPECT_EQ(segment.candidates_size(), segment.indexOf(nullptr)); | |
191 | 191 | |
192 | 192 | segment.pop_back_candidate(); |
193 | 193 | EXPECT_EQ(cand[3], segment.mutable_candidate(3)); |
597 | 597 | EXPECT_EQ(meta_idx, segment.indexOf(segment.mutable_candidate(meta_idx))); |
598 | 598 | } |
599 | 599 | |
600 | EXPECT_EQ(segment.candidates_size(), segment.indexOf(NULL)); | |
600 | EXPECT_EQ(segment.candidates_size(), segment.indexOf(nullptr)); | |
601 | 601 | |
602 | 602 | // mutable_meta_candidates |
603 | 603 | { |
27 | 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 | |
30 | load("//tools/build_defs:stubs.bzl", "cc_embed_data") | |
31 | ||
30 | 32 | package(default_visibility = [ |
31 | 33 | "//:__subpackages__", |
32 | 34 | ]) |
44 | 46 | "user_segment_history_pos_group.def", |
45 | 47 | ]) |
46 | 48 | |
49 | cc_embed_data( | |
50 | name = "dictionary_config_data", | |
51 | srcs = [ | |
52 | "boundary.def", | |
53 | "cforms.def", | |
54 | "segmenter.def", | |
55 | "sorting_map.tsv", | |
56 | "special_pos.def", | |
57 | "third_party_pos_map.def", | |
58 | "user_pos.def", | |
59 | "user_segment_history_pos_group.def", | |
60 | ], | |
61 | outs = [ | |
62 | "dictionary_config_data.cc", | |
63 | "dictionary_config_data.h", | |
64 | "dictionary_config_data_data.o", | |
65 | ], | |
66 | flatten = 1, | |
67 | strip = "dictionary_config_data", | |
68 | ) |
195 | 195 | // Load embedded suggestion filter (bloom filter) |
196 | 196 | std::unique_ptr<SuggestionFilter> suggestion_filter; |
197 | 197 | { |
198 | const char *data = NULL; | |
198 | const char *data = nullptr; | |
199 | 199 | size_t size; |
200 | 200 | data_manager_->GetSuggestionFilterData(&data, &size); |
201 | 201 | suggestion_filter.reset(new SuggestionFilter(data, size)); |
203 | 203 | "//testing:mozctest", |
204 | 204 | "//usage_stats", |
205 | 205 | "//usage_stats:usage_stats_testing_util", |
206 | "@com_google_absl//absl/memory", | |
206 | 207 | "@com_google_absl//absl/strings", |
207 | 208 | ], |
208 | 209 | ) |
524 | 525 | "//base:serialized_string_array", |
525 | 526 | "//base:util", |
526 | 527 | "//data_manager:data_manager_interface", |
528 | "@com_google_absl//absl/memory", | |
527 | 529 | "@com_google_absl//absl/strings", |
528 | 530 | ], |
529 | 531 | ) |
185 | 185 | DictionaryFileCodecFactory::SetCodec(internal_codec.get()); |
186 | 186 | const DictionaryFileCodecInterface *codec = |
187 | 187 | DictionaryFileCodecFactory::GetCodec(); |
188 | EXPECT_TRUE(codec != NULL); | |
188 | EXPECT_TRUE(codec != nullptr); | |
189 | 189 | { |
190 | 190 | std::vector<DictionaryFileSection> write_sections; |
191 | 191 | const std::string value0 = "Value 0 test"; |
80 | 80 | content.assign(ptr, len); |
81 | 81 | EXPECT_EQ("9876543210", content); |
82 | 82 | ptr = df.GetSection("sec3", &len); |
83 | EXPECT_TRUE(ptr == NULL); | |
83 | EXPECT_TRUE(ptr == nullptr); | |
84 | 84 | } |
85 | 85 | |
86 | 86 | FileUtil::Unlink(dfn); |
807 | 807 | } // namespace |
808 | 808 | |
809 | 809 | namespace { |
810 | SystemDictionaryCodecInterface *g_system_dictionary_codec = NULL; | |
810 | SystemDictionaryCodecInterface *g_system_dictionary_codec = nullptr; | |
811 | 811 | typedef SystemDictionaryCodec DefaultSystemDictionaryCodec; |
812 | 812 | } // namespace |
813 | 813 | |
814 | 814 | SystemDictionaryCodecInterface *SystemDictionaryCodecFactory::GetCodec() { |
815 | if (g_system_dictionary_codec == NULL) { | |
815 | if (g_system_dictionary_codec == nullptr) { | |
816 | 816 | return Singleton<DefaultSystemDictionaryCodec>::get(); |
817 | 817 | } else { |
818 | 818 | return g_system_dictionary_codec; |
117 | 117 | class SystemDictionaryCodecTest : public ::testing::Test { |
118 | 118 | protected: |
119 | 119 | void SetUp() override { |
120 | SystemDictionaryCodecFactory::SetCodec(NULL); | |
120 | SystemDictionaryCodecFactory::SetCodec(nullptr); | |
121 | 121 | ResetAllTokens(); |
122 | 122 | } |
123 | 123 | |
124 | 124 | void TearDown() override { |
125 | SystemDictionaryCodecFactory::SetCodec(NULL); | |
125 | SystemDictionaryCodecFactory::SetCodec(nullptr); | |
126 | 126 | ResetAllTokens(); |
127 | 127 | } |
128 | 128 | |
274 | 274 | void CheckDecoded() const { |
275 | 275 | EXPECT_EQ(source_tokens_.size(), decoded_tokens_.size()); |
276 | 276 | for (size_t i = 0; i < source_tokens_.size(); ++i) { |
277 | EXPECT_TRUE(source_tokens_[i].token != NULL); | |
278 | EXPECT_TRUE(decoded_tokens_[i].token != NULL); | |
277 | EXPECT_TRUE(source_tokens_[i].token != nullptr); | |
278 | EXPECT_TRUE(decoded_tokens_[i].token != nullptr); | |
279 | 279 | |
280 | 280 | EXPECT_EQ(source_tokens_[i].token->attributes, |
281 | 281 | decoded_tokens_[i].token->attributes); |
66 | 66 | // Usage: |
67 | 67 | // SystemDictionary::Builder builder(filename); |
68 | 68 | // builder.SetOptions(SystemDictionary::NONE); |
69 | // builder.SetCodec(NULL); | |
69 | // builder.SetCodec(nullptr); | |
70 | 70 | // SystemDictionary *dictionary = builder.Build(); |
71 | 71 | // ... |
72 | 72 | // delete dictionary; |
83 | 83 | // Sets options (default: NONE) |
84 | 84 | Builder &SetOptions(Options options); |
85 | 85 | |
86 | // Sets codec (default: NULL) | |
87 | // Uses default codec if this is NULL | |
86 | // Sets codec (default: nullptr) | |
87 | // Uses default codec if this is nullptr | |
88 | 88 | // Doesn't take the ownership of |codec|. |
89 | 89 | Builder &SetCodec(const SystemDictionaryCodecInterface *codec); |
90 | 90 |
545 | 545 | BuildSystemDictionary(tokens, 100); |
546 | 546 | std::unique_ptr<SystemDictionary> system_dic = |
547 | 547 | SystemDictionary::Builder(dic_fn_).Build().value(); |
548 | ASSERT_TRUE(system_dic.get() != NULL) | |
548 | ASSERT_TRUE(system_dic.get() != nullptr) | |
549 | 549 | << "Failed to open dictionary source: " << dic_fn_; |
550 | 550 | |
551 | 551 | const std::string kKey = "かつこう"; |
72 | 72 | token = t; |
73 | 73 | } |
74 | 74 | void Clear() { |
75 | token = NULL; | |
75 | token = nullptr; | |
76 | 76 | id_in_value_trie = -1; |
77 | 77 | id_in_frequent_pos_map = -1; |
78 | 78 | pos_type = DEFAULT_POS; |
275 | 275 | DISALLOW_COPY_AND_ASSIGN(UserDictionaryReloader); |
276 | 276 | }; |
277 | 277 | |
278 | UserDictionary::UserDictionary(const UserPOSInterface *user_pos, | |
278 | UserDictionary::UserDictionary(std::unique_ptr<const UserPOSInterface> user_pos, | |
279 | 279 | POSMatcher pos_matcher, |
280 | 280 | SuppressionDictionary *suppression_dictionary) |
281 | 281 | : ALLOW_THIS_IN_INITIALIZER_LIST( |
282 | 282 | reloader_(new UserDictionaryReloader(this))), |
283 | user_pos_(user_pos), | |
283 | user_pos_(std::move(user_pos)), | |
284 | 284 | pos_matcher_(pos_matcher), |
285 | 285 | suppression_dictionary_(suppression_dictionary), |
286 | 286 | tokens_(new TokensIndex(user_pos_.get(), suppression_dictionary)), |
49 | 49 | |
50 | 50 | class UserDictionary : public DictionaryInterface { |
51 | 51 | public: |
52 | UserDictionary(const UserPOSInterface *user_pos, POSMatcher pos_matcher, | |
52 | UserDictionary(std::unique_ptr<const UserPOSInterface> user_pos, | |
53 | POSMatcher pos_matcher, | |
53 | 54 | SuppressionDictionary *suppression_dictionary); |
54 | 55 | ~UserDictionary() override; |
55 | 56 |
103 | 103 | bool ConvertEntryInternal(const POSMap *pos_map, size_t map_size, |
104 | 104 | const UserDictionaryImporter::RawEntry &from, |
105 | 105 | UserDictionary::Entry *to) { |
106 | if (to == NULL) { | |
106 | if (to == nullptr) { | |
107 | 107 | LOG(ERROR) << "Null pointer is passed."; |
108 | 108 | return false; |
109 | 109 | } |
185 | 185 | |
186 | 186 | UserDictionaryImporter::ErrorType UserDictionaryImporter::ImportFromIterator( |
187 | 187 | InputIteratorInterface *iter, UserDictionary *user_dic) { |
188 | if (iter == NULL || user_dic == NULL) { | |
189 | LOG(ERROR) << "iter or user_dic is NULL"; | |
188 | if (iter == nullptr || user_dic == nullptr) { | |
189 | LOG(ERROR) << "iter or user_dic is nullptr"; | |
190 | 190 | return IMPORT_FATAL; |
191 | 191 | } |
192 | 192 | |
317 | 317 | return false; |
318 | 318 | } |
319 | 319 | |
320 | if (entry == NULL) { | |
321 | LOG(ERROR) << "Entry is NULL"; | |
320 | if (entry == nullptr) { | |
321 | LOG(ERROR) << "Entry is nullptr"; | |
322 | 322 | return false; |
323 | 323 | } |
324 | 324 |
45 | 45 | class TestInputIterator |
46 | 46 | : public UserDictionaryImporter::InputIteratorInterface { |
47 | 47 | public: |
48 | TestInputIterator() : index_(0), is_available_(false), entries_(NULL) {} | |
48 | TestInputIterator() : index_(0), is_available_(false), entries_(nullptr) {} | |
49 | 49 | |
50 | 50 | bool IsAvailable() const { return is_available_; } |
51 | 51 |
84 | 84 | : index_(index), dictionary_(dictionary) {} |
85 | 85 | |
86 | 86 | virtual bool RunUndo(mozc::UserDictionaryStorage *storage) { |
87 | if (dictionary_.get() == NULL) { | |
87 | if (dictionary_ == nullptr) { | |
88 | 88 | return false; |
89 | 89 | } |
90 | 90 | |
138 | 138 | UserDictionary *dictionary = |
139 | 139 | UserDictionaryUtil::GetMutableUserDictionaryById( |
140 | 140 | &storage->GetProto(), dictionary_id_); |
141 | if (dictionary == NULL) { | |
141 | if (dictionary == nullptr) { | |
142 | 142 | return false; |
143 | 143 | } |
144 | 144 | |
162 | 162 | UserDictionary *dictionary = |
163 | 163 | UserDictionaryUtil::GetMutableUserDictionaryById( |
164 | 164 | &storage->GetProto(), dictionary_id_); |
165 | if (dictionary == NULL || dictionary->entries_size() == 0) { | |
165 | if (dictionary == nullptr || dictionary->entries_size() == 0) { | |
166 | 166 | return false; |
167 | 167 | } |
168 | 168 | |
188 | 188 | UserDictionary *dictionary = |
189 | 189 | UserDictionaryUtil::GetMutableUserDictionaryById( |
190 | 190 | &storage->GetProto(), dictionary_id_); |
191 | if (dictionary == NULL || index_ < 0 || | |
191 | if (dictionary == nullptr || index_ < 0 || | |
192 | 192 | dictionary->entries_size() <= index_) { |
193 | 193 | return false; |
194 | 194 | } |
233 | 233 | UserDictionary *dictionary = |
234 | 234 | UserDictionaryUtil::GetMutableUserDictionaryById( |
235 | 235 | &storage->GetProto(), dictionary_id_); |
236 | if (dictionary == NULL) { | |
236 | if (dictionary == nullptr) { | |
237 | 237 | return false; |
238 | 238 | } |
239 | 239 | |
298 | 298 | UserDictionary *dictionary = |
299 | 299 | UserDictionaryUtil::GetMutableUserDictionaryById( |
300 | 300 | &storage->GetProto(), dictionary_id_); |
301 | if (dictionary == NULL) { | |
301 | if (dictionary == nullptr) { | |
302 | 302 | return false; |
303 | 303 | } |
304 | 304 | |
501 | 501 | const UserDictionary *dictionary = |
502 | 502 | UserDictionaryUtil::GetUserDictionaryById( |
503 | 503 | storage_->GetProto(), dictionary_id); |
504 | if (dictionary != NULL) { | |
504 | if (dictionary != nullptr) { | |
505 | 505 | // Note that if dictionary is null, it means the dictionary_id is invalid |
506 | 506 | // so following RenameDictionary will fail, and error handling is done |
507 | 507 | // in the following codes. |
536 | 536 | uint64 dictionary_id, const UserDictionary::Entry &entry) { |
537 | 537 | UserDictionary *dictionary = UserDictionaryUtil::GetMutableUserDictionaryById( |
538 | 538 | &storage_->GetProto(), dictionary_id); |
539 | if (dictionary == NULL) { | |
539 | if (dictionary == nullptr) { | |
540 | 540 | return UserDictionaryCommandStatus::UNKNOWN_DICTIONARY_ID; |
541 | 541 | } |
542 | 542 | |
563 | 563 | uint64 dictionary_id, int index, const UserDictionary::Entry &entry) { |
564 | 564 | UserDictionary *dictionary = UserDictionaryUtil::GetMutableUserDictionaryById( |
565 | 565 | &storage_->GetProto(), dictionary_id); |
566 | if (dictionary == NULL) { | |
566 | if (dictionary == nullptr) { | |
567 | 567 | return UserDictionaryCommandStatus::UNKNOWN_DICTIONARY_ID; |
568 | 568 | } |
569 | 569 | |
590 | 590 | uint64 dictionary_id, const std::vector<int> &index_list) { |
591 | 591 | UserDictionary *dictionary = UserDictionaryUtil::GetMutableUserDictionaryById( |
592 | 592 | &storage_->GetProto(), dictionary_id); |
593 | if (dictionary == NULL) { | |
593 | if (dictionary == nullptr) { | |
594 | 594 | return UserDictionaryCommandStatus::UNKNOWN_DICTIONARY_ID; |
595 | 595 | } |
596 | 596 | |
611 | 611 | const int index = index_list[i]; |
612 | 612 | |
613 | 613 | deleted_entries.push_back(std::make_pair(index, data[index])); |
614 | data[index] = NULL; | |
615 | } | |
616 | ||
617 | UserDictionary::Entry **tail = std::remove( | |
618 | data, data + entries->size(), static_cast<UserDictionary::Entry *>(NULL)); | |
614 | data[index] = nullptr; | |
615 | } | |
616 | ||
617 | UserDictionary::Entry **tail = | |
618 | std::remove(data, data + entries->size(), | |
619 | static_cast<UserDictionary::Entry *>(nullptr)); | |
619 | 620 | const int remaining_size = tail - data; |
620 | 621 | while (entries->size() > remaining_size) { |
621 | 622 | entries->ReleaseLast(); |
629 | 630 | uint64 dictionary_id, const std::string &data) { |
630 | 631 | UserDictionary *dictionary = UserDictionaryUtil::GetMutableUserDictionaryById( |
631 | 632 | &storage_->GetProto(), dictionary_id); |
632 | if (dictionary == NULL) { | |
633 | if (dictionary == nullptr) { | |
633 | 634 | return UserDictionaryCommandStatus::UNKNOWN_DICTIONARY_ID; |
634 | 635 | } |
635 | 636 | |
692 | 693 | |
693 | 694 | UserDictionary *dictionary = UserDictionaryUtil::GetMutableUserDictionaryById( |
694 | 695 | &storage_->GetProto(), *new_dictionary_id); |
695 | if (dictionary == NULL) { | |
696 | if (dictionary == nullptr) { | |
696 | 697 | // The dictionary should be always found. |
697 | 698 | return UserDictionaryCommandStatus::UNKNOWN_ERROR; |
698 | 699 | } |
130 | 130 | void UserDictionarySessionHandler::NoOperation( |
131 | 131 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
132 | 132 | UserDictionarySession *session = GetSession(command, status); |
133 | if (session == NULL) { | |
133 | if (session == nullptr) { | |
134 | 134 | return; |
135 | 135 | } |
136 | 136 | |
168 | 168 | void UserDictionarySessionHandler::DeleteSession( |
169 | 169 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
170 | 170 | UserDictionarySession *session = GetSession(command, status); |
171 | if (session == NULL) { | |
171 | if (session == nullptr) { | |
172 | 172 | return; |
173 | 173 | } |
174 | 174 | |
181 | 181 | void UserDictionarySessionHandler::SetDefaultDictionaryName( |
182 | 182 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
183 | 183 | UserDictionarySession *session = GetSession(command, status); |
184 | if (session == NULL) { | |
184 | if (session == nullptr) { | |
185 | 185 | return; |
186 | 186 | } |
187 | 187 | |
197 | 197 | void UserDictionarySessionHandler::CheckUndoability( |
198 | 198 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
199 | 199 | UserDictionarySession *session = GetSession(command, status); |
200 | if (session == NULL) { | |
200 | if (session == nullptr) { | |
201 | 201 | return; |
202 | 202 | } |
203 | 203 | |
210 | 210 | void UserDictionarySessionHandler::Undo(const UserDictionaryCommand &command, |
211 | 211 | UserDictionaryCommandStatus *status) { |
212 | 212 | UserDictionarySession *session = GetSession(command, status); |
213 | if (session == NULL) { | |
213 | if (session == nullptr) { | |
214 | 214 | return; |
215 | 215 | } |
216 | 216 | |
220 | 220 | void UserDictionarySessionHandler::Load(const UserDictionaryCommand &command, |
221 | 221 | UserDictionaryCommandStatus *status) { |
222 | 222 | UserDictionarySession *session = GetSession(command, status); |
223 | if (session == NULL) { | |
223 | if (session == nullptr) { | |
224 | 224 | return; |
225 | 225 | } |
226 | 226 | |
234 | 234 | void UserDictionarySessionHandler::Save(const UserDictionaryCommand &command, |
235 | 235 | UserDictionaryCommandStatus *status) { |
236 | 236 | UserDictionarySession *session = GetSession(command, status); |
237 | if (session == NULL) { | |
237 | if (session == nullptr) { | |
238 | 238 | return; |
239 | 239 | } |
240 | 240 | |
244 | 244 | void UserDictionarySessionHandler::GetUserDictionaryNameList( |
245 | 245 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
246 | 246 | UserDictionarySession *session = GetSession(command, status); |
247 | if (session == NULL) { | |
247 | if (session == nullptr) { | |
248 | 248 | return; |
249 | 249 | } |
250 | 250 | |
268 | 268 | void UserDictionarySessionHandler::GetEntrySize( |
269 | 269 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
270 | 270 | UserDictionarySession *session = GetSession(command, status); |
271 | if (session == NULL) { | |
271 | if (session == nullptr) { | |
272 | 272 | return; |
273 | 273 | } |
274 | 274 | |
279 | 279 | |
280 | 280 | const UserDictionary *dictionary = UserDictionaryUtil::GetUserDictionaryById( |
281 | 281 | session_->storage(), command.dictionary_id()); |
282 | if (dictionary == NULL) { | |
282 | if (dictionary == nullptr) { | |
283 | 283 | status->set_status(UserDictionaryCommandStatus::UNKNOWN_DICTIONARY_ID); |
284 | 284 | return; |
285 | 285 | } |
292 | 292 | void UserDictionarySessionHandler::GetEntries( |
293 | 293 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
294 | 294 | UserDictionarySession *session = GetSession(command, status); |
295 | if (session == NULL) { | |
295 | if (session == nullptr) { | |
296 | 296 | return; |
297 | 297 | } |
298 | 298 | |
303 | 303 | |
304 | 304 | const UserDictionary *dictionary = UserDictionaryUtil::GetUserDictionaryById( |
305 | 305 | session_->storage(), command.dictionary_id()); |
306 | if (dictionary == NULL) { | |
306 | if (dictionary == nullptr) { | |
307 | 307 | status->set_status(UserDictionaryCommandStatus::UNKNOWN_DICTIONARY_ID); |
308 | 308 | return; |
309 | 309 | } |
332 | 332 | void UserDictionarySessionHandler::CheckNewDictionaryAvailability( |
333 | 333 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
334 | 334 | UserDictionarySession *session = GetSession(command, status); |
335 | if (session == NULL) { | |
335 | if (session == nullptr) { | |
336 | 336 | return; |
337 | 337 | } |
338 | 338 | |
349 | 349 | void UserDictionarySessionHandler::CreateDictionary( |
350 | 350 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
351 | 351 | UserDictionarySession *session = GetSession(command, status); |
352 | if (session == NULL) { | |
352 | if (session == nullptr) { | |
353 | 353 | return; |
354 | 354 | } |
355 | 355 | |
370 | 370 | void UserDictionarySessionHandler::DeleteDictionary( |
371 | 371 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
372 | 372 | UserDictionarySession *session = GetSession(command, status); |
373 | if (session == NULL) { | |
373 | if (session == nullptr) { | |
374 | 374 | return; |
375 | 375 | } |
376 | 376 | |
390 | 390 | void UserDictionarySessionHandler::RenameDictionary( |
391 | 391 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
392 | 392 | UserDictionarySession *session = GetSession(command, status); |
393 | if (session == NULL) { | |
393 | if (session == nullptr) { | |
394 | 394 | return; |
395 | 395 | } |
396 | 396 | |
406 | 406 | void UserDictionarySessionHandler::CheckNewEntryAvailability( |
407 | 407 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
408 | 408 | UserDictionarySession *session = GetSession(command, status); |
409 | if (session == NULL) { | |
409 | if (session == nullptr) { | |
410 | 410 | return; |
411 | 411 | } |
412 | 412 | |
417 | 417 | |
418 | 418 | const UserDictionary *dictionary = UserDictionaryUtil::GetUserDictionaryById( |
419 | 419 | session->storage(), command.dictionary_id()); |
420 | if (dictionary == NULL) { | |
420 | if (dictionary == nullptr) { | |
421 | 421 | status->set_status(UserDictionaryCommandStatus::UNKNOWN_DICTIONARY_ID); |
422 | 422 | return; |
423 | 423 | } |
434 | 434 | void UserDictionarySessionHandler::AddEntry( |
435 | 435 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
436 | 436 | UserDictionarySession *session = GetSession(command, status); |
437 | if (session == NULL) { | |
437 | if (session == nullptr) { | |
438 | 438 | return; |
439 | 439 | } |
440 | 440 | |
450 | 450 | void UserDictionarySessionHandler::EditEntry( |
451 | 451 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
452 | 452 | UserDictionarySession *session = GetSession(command, status); |
453 | if (session == NULL) { | |
453 | if (session == nullptr) { | |
454 | 454 | return; |
455 | 455 | } |
456 | 456 | |
467 | 467 | void UserDictionarySessionHandler::DeleteEntry( |
468 | 468 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
469 | 469 | UserDictionarySession *session = GetSession(command, status); |
470 | if (session == NULL) { | |
470 | if (session == nullptr) { | |
471 | 471 | return; |
472 | 472 | } |
473 | 473 | |
484 | 484 | void UserDictionarySessionHandler::ImportData( |
485 | 485 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
486 | 486 | UserDictionarySession *session = GetSession(command, status); |
487 | if (session == NULL) { | |
487 | if (session == nullptr) { | |
488 | 488 | return; |
489 | 489 | } |
490 | 490 | |
522 | 522 | void UserDictionarySessionHandler::GetStorage( |
523 | 523 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
524 | 524 | UserDictionarySession *session = GetSession(command, status); |
525 | if (session == NULL) { | |
525 | if (session == nullptr) { | |
526 | 526 | return; |
527 | 527 | } |
528 | 528 | status->mutable_storage()->CopyFrom(session->storage()); |
534 | 534 | const UserDictionaryCommand &command, UserDictionaryCommandStatus *status) { |
535 | 535 | if (!command.has_session_id()) { |
536 | 536 | status->set_status(UserDictionaryCommandStatus::INVALID_ARGUMENT); |
537 | return NULL; | |
538 | } | |
539 | ||
540 | if (session_.get() == NULL || session_id_ != command.session_id()) { | |
537 | return nullptr; | |
538 | } | |
539 | ||
540 | if (session_ == nullptr || session_id_ != command.session_id()) { | |
541 | 541 | status->set_status(UserDictionaryCommandStatus::UNKNOWN_SESSION_ID); |
542 | return NULL; | |
542 | return nullptr; | |
543 | 543 | } |
544 | 544 | |
545 | 545 | return session_.get(); |
551 | 551 | Util::GetRandomSequence(reinterpret_cast<char *>(&id), sizeof(id)); |
552 | 552 | |
553 | 553 | if (id != kInvalidSessionId && |
554 | (session_.get() == NULL || session_id_ != id)) { | |
554 | (session_ == nullptr || session_id_ != id)) { | |
555 | 555 | // New id is generated. |
556 | 556 | break; |
557 | 557 | } |
261 | 261 | } |
262 | 262 | |
263 | 263 | bool UserDictionaryStorage::DeleteDictionary(uint64 dic_id) { |
264 | if (!UserDictionaryUtil::DeleteDictionary(&proto_, dic_id, NULL, NULL)) { | |
264 | if (!UserDictionaryUtil::DeleteDictionary(&proto_, dic_id, nullptr, | |
265 | nullptr)) { | |
265 | 266 | // Failed to delete dictionary. |
266 | 267 | last_error_type_ = INVALID_DICTIONARY_ID; |
267 | 268 | return false; |
281 | 282 | } |
282 | 283 | |
283 | 284 | UserDictionary *dic = GetUserDictionary(dic_id); |
284 | if (dic == NULL) { | |
285 | if (dic == nullptr) { | |
285 | 286 | last_error_type_ = INVALID_DICTIONARY_ID; |
286 | 287 | LOG(ERROR) << "Invalid dictionary id: " << dic_id; |
287 | 288 | return false; |
131 | 131 | for (size_t i = 0; i < kDictionariesSize; ++i) { |
132 | 132 | EXPECT_EQ(storage.GetProto().mutable_dictionaries(i + dict_size), |
133 | 133 | storage.GetUserDictionary(id[i])); |
134 | EXPECT_EQ(NULL, storage.GetUserDictionary(id[i] + 1)); | |
134 | EXPECT_EQ(nullptr, storage.GetUserDictionary(id[i] + 1)); | |
135 | 135 | } |
136 | 136 | |
137 | 137 | // empty |
57 | 57 | #include "testing/base/public/mozctest.h" |
58 | 58 | #include "usage_stats/usage_stats.h" |
59 | 59 | #include "usage_stats/usage_stats_testing_util.h" |
60 | #include "absl/memory/memory.h" | |
60 | 61 | #include "absl/strings/string_view.h" |
61 | 62 | |
62 | 63 | namespace mozc { |
206 | 207 | // Creates a user dictionary with mock pos data. |
207 | 208 | UserDictionary *CreateDictionaryWithMockPos() { |
208 | 209 | return new UserDictionary( |
209 | new UserPOSMock(), | |
210 | absl::make_unique<UserPOSMock>(), | |
210 | 211 | dictionary::POSMatcher(mock_data_manager_.GetPOSMatcherData()), |
211 | 212 | suppression_dictionary_.get()); |
212 | 213 | } |
178 | 178 | const user_dictionary::UserDictionaryStorage &storage, |
179 | 179 | uint64 dictionary_id) { |
180 | 180 | int index = GetUserDictionaryIndexById(storage, dictionary_id); |
181 | return index >= 0 ? &storage.dictionaries(index) : NULL; | |
181 | return index >= 0 ? &storage.dictionaries(index) : nullptr; | |
182 | 182 | } |
183 | 183 | |
184 | 184 | user_dictionary::UserDictionary * |
185 | 185 | UserDictionaryUtil::GetMutableUserDictionaryById( |
186 | 186 | user_dictionary::UserDictionaryStorage *storage, uint64 dictionary_id) { |
187 | 187 | int index = GetUserDictionaryIndexById(*storage, dictionary_id); |
188 | return index >= 0 ? storage->mutable_dictionaries(index) : NULL; | |
188 | return index >= 0 ? storage->mutable_dictionaries(index) : nullptr; | |
189 | 189 | } |
190 | 190 | |
191 | 191 | int UserDictionaryUtil::GetUserDictionaryIndexById( |
314 | 314 | if (user_dictionary::UserDictionary::PosType_IsValid(pos_type)) { |
315 | 315 | return kPosTypeStringTable[pos_type]; |
316 | 316 | } |
317 | return NULL; | |
317 | return nullptr; | |
318 | 318 | } |
319 | 319 | |
320 | 320 | user_dictionary::UserDictionary::PosType UserDictionaryUtil::ToPosType( |
372 | 372 | return UserDictionaryCommandStatus::DICTIONARY_SIZE_LIMIT_EXCEEDED; |
373 | 373 | } |
374 | 374 | |
375 | if (new_dictionary_id == NULL) { | |
376 | LOG(ERROR) << "new_dictionary_id is NULL"; | |
375 | if (new_dictionary_id == nullptr) { | |
376 | LOG(ERROR) << "new_dictionary_id is nullptr"; | |
377 | 377 | return UserDictionaryCommandStatus::UNKNOWN_ERROR; |
378 | 378 | } |
379 | 379 | |
380 | 380 | *new_dictionary_id = CreateNewDictionaryId(*storage); |
381 | 381 | user_dictionary::UserDictionary *dictionary = storage->add_dictionaries(); |
382 | if (dictionary == NULL) { | |
382 | if (dictionary == nullptr) { | |
383 | 383 | LOG(ERROR) << "add_dictionaries failed."; |
384 | 384 | return UserDictionaryCommandStatus::UNKNOWN_ERROR; |
385 | 385 | } |
393 | 393 | user_dictionary::UserDictionaryStorage *storage, uint64 dictionary_id, |
394 | 394 | int *original_index, user_dictionary::UserDictionary **deleted_dictionary) { |
395 | 395 | const int index = GetUserDictionaryIndexById(*storage, dictionary_id); |
396 | if (original_index != NULL) { | |
396 | if (original_index != nullptr) { | |
397 | 397 | *original_index = index; |
398 | 398 | } |
399 | 399 | |
409 | 409 | dictionaries->pointer_begin() + index + 1, |
410 | 410 | dictionaries->pointer_end()); |
411 | 411 | |
412 | if (deleted_dictionary == NULL) { | |
412 | if (deleted_dictionary == nullptr) { | |
413 | 413 | dictionaries->RemoveLast(); |
414 | 414 | } else { |
415 | 415 | *deleted_dictionary = dictionaries->ReleaseLast(); |
109 | 109 | static bool IsDictionaryFull( |
110 | 110 | const user_dictionary::UserDictionary &dictionary); |
111 | 111 | |
112 | // Returns UserDictionary with the given id, or NULL if not found. | |
112 | // Returns UserDictionary with the given id, or nullptr if not found. | |
113 | 113 | static const user_dictionary::UserDictionary *GetUserDictionaryById( |
114 | 114 | const user_dictionary::UserDictionaryStorage &storage, |
115 | 115 | uint64 dictionary_id); |
125 | 125 | // Returns the file name of UserDictionary. |
126 | 126 | static std::string GetUserDictionaryFileName(); |
127 | 127 | |
128 | // Returns the string representation of PosType, or NULL if the given | |
128 | // Returns the string representation of PosType, or nullptr if the given | |
129 | 129 | // pos is invalid. |
130 | 130 | // For historicall reason, the pos was represented in Japanese characters. |
131 | 131 | static const char *GetStringPosType( |
132 | 132 | user_dictionary::UserDictionary::PosType pos_type); |
133 | 133 | |
134 | // Returns the string representation of PosType, or NULL if the given | |
134 | // Returns the string representation of PosType, or nullptr if the given | |
135 | 135 | // pos is invalid. |
136 | 136 | static user_dictionary::UserDictionary::PosType ToPosType( |
137 | 137 | const char *string_pos_type); |
146 | 146 | const std::string &dictionary_name, uint64 *new_dictionary_id); |
147 | 147 | |
148 | 148 | // Deletes dictionary specified by the given dictionary_id. |
149 | // If the deleted_dictionary is not NULL, the pointer to the | |
149 | // If the deleted_dictionary is not nullptr, the pointer to the | |
150 | 150 | // delete dictionary is stored into it. In other words, |
151 | 151 | // caller has responsibility to actual deletion of the instance. |
152 | 152 | // Returns true if succeeded, otherwise false. |
278 | 278 | &storage.dictionaries(0)); |
279 | 279 | EXPECT_TRUE(UserDictionaryUtil::GetUserDictionaryById(storage, 2) == |
280 | 280 | &storage.dictionaries(1)); |
281 | EXPECT_TRUE(UserDictionaryUtil::GetUserDictionaryById(storage, -1) == NULL); | |
281 | EXPECT_TRUE(UserDictionaryUtil::GetUserDictionaryById(storage, -1) == | |
282 | nullptr); | |
282 | 283 | } |
283 | 284 | |
284 | 285 | TEST(UserDictionaryUtilTest, GetMutableUserDictionaryById) { |
291 | 292 | EXPECT_TRUE(UserDictionaryUtil::GetMutableUserDictionaryById(&storage, 2) == |
292 | 293 | storage.mutable_dictionaries(1)); |
293 | 294 | EXPECT_TRUE(UserDictionaryUtil::GetMutableUserDictionaryById(&storage, -1) == |
294 | NULL); | |
295 | nullptr); | |
295 | 296 | } |
296 | 297 | |
297 | 298 | TEST(UserDictionaryUtilTest, GetUserDictionaryIndexById) { |
325 | 326 | &dictionary_id)); |
326 | 327 | |
327 | 328 | storage.Clear(); |
328 | EXPECT_EQ( | |
329 | UserDictionaryCommandStatus::UNKNOWN_ERROR, | |
330 | UserDictionaryUtil::CreateDictionary(&storage, "new dictionary", NULL)); | |
329 | EXPECT_EQ(UserDictionaryCommandStatus::UNKNOWN_ERROR, | |
330 | UserDictionaryUtil::CreateDictionary(&storage, "new dictionary", | |
331 | nullptr)); | |
331 | 332 | |
332 | 333 | ASSERT_EQ(UserDictionaryCommandStatus::USER_DICTIONARY_COMMAND_SUCCESS, |
333 | 334 | UserDictionaryUtil::CreateDictionary(&storage, "new dictionary", |
348 | 349 | |
349 | 350 | // Simplest deleting case. |
350 | 351 | int original_index; |
351 | ASSERT_TRUE( | |
352 | UserDictionaryUtil::DeleteDictionary(&storage, 1, &original_index, NULL)); | |
352 | ASSERT_TRUE(UserDictionaryUtil::DeleteDictionary(&storage, 1, &original_index, | |
353 | nullptr)); | |
353 | 354 | EXPECT_EQ(0, original_index); |
354 | 355 | ASSERT_EQ(1, storage.dictionaries_size()); |
355 | 356 | EXPECT_EQ(2, storage.dictionaries(0).id()); |
358 | 359 | storage.Clear(); |
359 | 360 | storage.add_dictionaries()->set_id(1); |
360 | 361 | storage.add_dictionaries()->set_id(2); |
361 | EXPECT_FALSE(UserDictionaryUtil::DeleteDictionary(&storage, 100, NULL, NULL)); | |
362 | EXPECT_FALSE( | |
363 | UserDictionaryUtil::DeleteDictionary(&storage, 100, nullptr, nullptr)); | |
362 | 364 | |
363 | 365 | // Keep deleted dictionary. |
364 | 366 | storage.Clear(); |
366 | 368 | storage.add_dictionaries()->set_id(2); |
367 | 369 | UserDictionary *expected_deleted_dictionary = storage.mutable_dictionaries(0); |
368 | 370 | UserDictionary *deleted_dictionary; |
369 | EXPECT_TRUE(UserDictionaryUtil::DeleteDictionary(&storage, 1, NULL, | |
371 | EXPECT_TRUE(UserDictionaryUtil::DeleteDictionary(&storage, 1, nullptr, | |
370 | 372 | &deleted_dictionary)); |
371 | 373 | ASSERT_EQ(1, storage.dictionaries_size()); |
372 | 374 | EXPECT_EQ(2, storage.dictionaries(0).id()); |
33 | 33 | |
34 | 34 | #include "base/logging.h" |
35 | 35 | #include "base/util.h" |
36 | #include "absl/memory/memory.h" | |
36 | 37 | #include "absl/strings/string_view.h" |
37 | 38 | |
38 | 39 | namespace mozc { |
159 | 160 | return true; |
160 | 161 | } |
161 | 162 | |
162 | UserPOS *UserPOS::CreateFromDataManager(const DataManagerInterface &manager) { | |
163 | std::unique_ptr<UserPOS> UserPOS::CreateFromDataManager( | |
164 | const DataManagerInterface &manager) { | |
163 | 165 | absl::string_view token_array_data, string_array_data; |
164 | 166 | manager.GetUserPOSData(&token_array_data, &string_array_data); |
165 | return new UserPOS(token_array_data, string_array_data); | |
167 | return absl::make_unique<UserPOS>(token_array_data, string_array_data); | |
166 | 168 | } |
167 | 169 | |
168 | 170 | } // namespace dictionary |
169 | 169 | |
170 | 170 | using const_iterator = iterator; |
171 | 171 | |
172 | static UserPOS *CreateFromDataManager(const DataManagerInterface &manager); | |
172 | static std::unique_ptr<UserPOS> CreateFromDataManager( | |
173 | const DataManagerInterface &manager); | |
173 | 174 | |
174 | 175 | // Initializes the user pos from the given binary data. The provided byte |
175 | 176 | // data must outlive this instance. |
88 | 88 | "//base:version", |
89 | 89 | "//gui/base:gui_base", |
90 | 90 | "//gui/base:singleton_window_helper", |
91 | "@com_google_absl//absl/memory", | |
91 | 92 | ], |
92 | 93 | ) |
93 | 94 |
29 | 29 | #include "gui/about_dialog/about_dialog.h" |
30 | 30 | |
31 | 31 | #include <QtGui/QtGui> |
32 | ||
33 | 32 | #include <string> |
34 | 33 | |
35 | 34 | #include "base/file_util.h" |
39 | 38 | #include "base/util.h" |
40 | 39 | #include "base/version.h" |
41 | 40 | #include "gui/base/util.h" |
41 | #include "absl/memory/memory.h" | |
42 | 42 | |
43 | 43 | namespace mozc { |
44 | 44 | namespace gui { |
111 | 111 | SetLabelText(label_terms); |
112 | 112 | SetLabelText(label_credits); |
113 | 113 | |
114 | product_image_.reset(new QImage(":/product_logo.png")); | |
114 | product_image_ = absl::make_unique<QImage>(":/product_logo.png"); | |
115 | 115 | } |
116 | 116 | |
117 | 117 | void AboutDialog::paintEvent(QPaintEvent *event) { |
32 | 32 | #define MOZC_GUI_ABOUT_DIALOG_ABOUT_DIALOG_H_ |
33 | 33 | |
34 | 34 | #include <QtWidgets/QDialog> |
35 | ||
36 | 35 | #include <memory> |
37 | 36 | |
38 | 37 | #include "base/port.h" |
56 | 55 | explicit AboutDialog(QWidget *parent = nullptr); |
57 | 56 | void SetLinkCallback(LinkCallbackInterface *callback); |
58 | 57 | |
59 | void paintEvent(QPaintEvent *event); | |
58 | void paintEvent(QPaintEvent *event) override; | |
60 | 59 | |
61 | 60 | public slots: |
62 | 61 | void linkActivated(const QString &link); |
28 | 28 | |
29 | 29 | #include <QtGui/QGuiApplication> |
30 | 30 | #include <QtGui/QtGui> |
31 | ||
31 | 32 | #include "base/system_util.h" |
32 | 33 | #include "gui/about_dialog/about_dialog.h" |
33 | 34 | #include "gui/base/singleton_window_helper.h" |
41 | 41 | |
42 | 42 | public: |
43 | 43 | AdministrationDialog(); |
44 | virtual ~AdministrationDialog(); | |
44 | ~AdministrationDialog() override; | |
45 | 45 | |
46 | 46 | protected slots: |
47 | 47 | virtual void clicked(QAbstractButton *button); |
48 | 48 | |
49 | 49 | protected: |
50 | bool eventFilter(QObject *obj, QEvent *event); | |
50 | bool eventFilter(QObject *obj, QEvent *event) override; | |
51 | 51 | |
52 | 52 | private: |
53 | 53 | bool CanStartService(); |
165 | 165 | "//base:version", |
166 | 166 | "//ipc", |
167 | 167 | "//ipc:window_info_proto", |
168 | "@com_google_absl//absl/memory", | |
168 | 169 | ], |
169 | 170 | ) |
46 | 46 | #include "base/util.h" |
47 | 47 | #include "gui/base/win_util.h" |
48 | 48 | #include "ipc/window_info.pb.h" |
49 | #include "absl/memory/memory.h" | |
49 | 50 | |
50 | 51 | namespace mozc { |
51 | 52 | namespace gui { |
112 | 113 | } // namespace |
113 | 114 | |
114 | 115 | SingletonWindowHelper::SingletonWindowHelper(const std::string &name) { |
115 | mutex_.reset(new mozc::ProcessMutex(name.c_str())); | |
116 | mutex_ = absl::make_unique<mozc::ProcessMutex>(name.c_str()); | |
116 | 117 | } |
117 | 118 | |
118 | 119 | SingletonWindowHelper::~SingletonWindowHelper() {} |
35 | 35 | #endif // CHANNEL_DEV && GOOGLE_JAPANESE_INPUT_BUILD |
36 | 36 | |
37 | 37 | |
38 | #include <memory> | |
39 | ||
40 | 38 | #include <QtCore/QObject> |
41 | 39 | #include <QtGui/QFont> |
42 | 40 | #include <QtGui/QGuiApplication> |
44 | 42 | #include <QtWidgets/QAbstractButton> |
45 | 43 | #include <QtWidgets/QApplication> |
46 | 44 | #include <QtWidgets/QStyleFactory> |
45 | #include <memory> | |
47 | 46 | |
47 | #include "base/logging.h" | |
48 | 48 | #include "absl/memory/memory.h" |
49 | #include "base/logging.h" | |
50 | 49 | |
51 | 50 | #ifdef MOZC_SHOW_BUILD_NUMBER_ON_TITLE |
52 | 51 | #include "gui/base/window_title_modifier.h" |
26 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | #include "gui/base/window_title_modifier.h" | |
30 | ||
29 | 31 | #include <QtCore/QObject> |
30 | 32 | #include <QtCore/QString> |
31 | 33 | #include <QtGui/QGuiApplication> |
33 | 35 | #include <QtWidgets/QWidget> |
34 | 36 | |
35 | 37 | #include "base/version.h" |
36 | #include "gui/base/window_title_modifier.h" | |
37 | 38 | |
38 | 39 | namespace mozc { |
39 | 40 | namespace gui { |
26 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | #ifndef MOZC_GUI_BASE_WINDOW_TITLE_MODIFIER_ | |
30 | #define MOZC_GUI_BASE_WINDOW_TITLE_MODIFIER_ | |
29 | #ifndef MOZC_GUI_BASE_WINDOW_TITLE_MODIFIER_H_ | |
30 | #define MOZC_GUI_BASE_WINDOW_TITLE_MODIFIER_H_ | |
31 | 31 | |
32 | 32 | #include <QtCore/QEvent> |
33 | 33 | #include <QtCore/QObject> |
43 | 43 | ~WindowTitleModifier() override = default; |
44 | 44 | |
45 | 45 | protected: |
46 | bool eventFilter(QObject *obj, QEvent *event); | |
46 | bool eventFilter(QObject *obj, QEvent *event) override; | |
47 | 47 | }; |
48 | 48 | } // namespace gui |
49 | 49 | } // namespace mozc |
50 | 50 | |
51 | #endif // MOZC_GUI_BASE_WINDOW_TITLE_MODIFIER_ | |
51 | #endif // MOZC_GUI_BASE_WINDOW_TITLE_MODIFIER_H_ |
208 | 208 | "//protocol:commands_proto", |
209 | 209 | "//protocol:config_proto", |
210 | 210 | "//session/internal:keymap", |
211 | "@com_google_absl//absl/memory", | |
211 | 212 | ], |
212 | 213 | ) |
213 | 214 |
30 | 30 | |
31 | 31 | #include <QtGui/QtGui> |
32 | 32 | #include <QtWidgets/QHeaderView> |
33 | ||
34 | 33 | #include <memory> |
35 | 34 | |
36 | 35 | #include "config/config_handler.h" |
37 | 36 | #include "gui/config_dialog/combobox_delegate.h" |
37 | #include "absl/memory/memory.h" | |
38 | 38 | |
39 | 39 | namespace mozc { |
40 | 40 | namespace gui { |
126 | 126 | |
127 | 127 | // make sure that table isn't empty. |
128 | 128 | if (config.character_form_rules_size() == 0) { |
129 | default_config.reset(new config::Config); | |
129 | default_config = absl::make_unique<config::Config>(); | |
130 | 130 | config::ConfigHandler::GetDefaultConfig(default_config.get()); |
131 | 131 | target_config = default_config.get(); |
132 | 132 | } |
30 | 30 | #define MOZC_GUI_CONFIG_DIALOG_CHARACTER_FORM_EDITOR_H_ |
31 | 31 | |
32 | 32 | #include <QtWidgets/QTableWidget> |
33 | ||
34 | 33 | #include <memory> |
35 | 34 | |
36 | 35 | #include "base/port.h" |
49 | 48 | Q_OBJECT |
50 | 49 | public: |
51 | 50 | explicit CharacterFormEditor(QWidget *parent = nullptr); |
52 | virtual ~CharacterFormEditor(); | |
51 | ~CharacterFormEditor() override; | |
53 | 52 | |
54 | 53 | void Load(const config::Config &config); |
55 | 54 | void Save(config::Config *config); |
44 | 44 | Q_OBJECT |
45 | 45 | public: |
46 | 46 | explicit ComboBoxDelegate(QObject *parent = nullptr); |
47 | virtual ~ComboBoxDelegate(); | |
47 | ~ComboBoxDelegate() override; | |
48 | 48 | |
49 | 49 | void SetItemList(const QStringList &item_list); |
50 | 50 | |
51 | 51 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, |
52 | const QModelIndex &index) const; | |
52 | const QModelIndex &index) const override; | |
53 | 53 | |
54 | void setEditorData(QWidget *editor, const QModelIndex &index) const; | |
54 | void setEditorData(QWidget *editor, const QModelIndex &index) const override; | |
55 | 55 | void setModelData(QWidget *editor, QAbstractItemModel *model, |
56 | const QModelIndex &index) const; | |
56 | const QModelIndex &index) const override; | |
57 | 57 | |
58 | 58 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, |
59 | const QModelIndex &index) const; | |
59 | const QModelIndex &index) const override; | |
60 | 60 | |
61 | 61 | private slots: |
62 | 62 | void CommitAndCloseEditor(const QString &str); |
33 | 33 | |
34 | 34 | #include <QtCore/QObject> |
35 | 35 | #include <QtCore/QTimer> |
36 | ||
37 | 36 | #include <map> |
38 | 37 | #include <memory> |
39 | 38 | #include <string> |
58 | 57 | |
59 | 58 | public: |
60 | 59 | ConfigDialog(); |
61 | virtual ~ConfigDialog(); | |
60 | ~ConfigDialog() override; | |
62 | 61 | |
63 | 62 | // Methods defined in the 'slots' section (Qt's extention) will be processed |
64 | 63 | // by Qt's moc tool (moc.exe on Windows). Unfortunately, preprocessor macros |
86 | 85 | virtual void EnableApplyButton(); |
87 | 86 | |
88 | 87 | protected: |
89 | bool eventFilter(QObject *obj, QEvent *event); | |
88 | bool eventFilter(QObject *obj, QEvent *event) override; | |
90 | 89 | |
91 | 90 | private: |
92 | 91 | bool GetConfig(config::Config *config); |
142 | 142 | rows.push_back(selected[i]->row()); |
143 | 143 | } |
144 | 144 | |
145 | std::vector<int>::iterator last = unique(rows.begin(), rows.end()); | |
145 | std::vector<int>::iterator last = std::unique(rows.begin(), rows.end()); | |
146 | 146 | rows.erase(last, rows.end()); |
147 | 147 | |
148 | 148 | if (rows.empty()) { |
45 | 45 | |
46 | 46 | public: |
47 | 47 | explicit GenericTableEditorDialog(QWidget *parent, size_t column_size); |
48 | virtual ~GenericTableEditorDialog(); | |
48 | ~GenericTableEditorDialog() override; | |
49 | 49 | |
50 | 50 | bool LoadFromString(const std::string &table); |
51 | 51 | const std::string &table() const; |
49 | 49 | #include "base/logging.h" |
50 | 50 | #include "base/util.h" |
51 | 51 | #include "gui/base/util.h" |
52 | #include "absl/memory/memory.h" | |
52 | 53 | |
53 | 54 | namespace mozc { |
54 | 55 | namespace gui { |
157 | 158 | class KeyBindingFilter : public QObject { |
158 | 159 | public: |
159 | 160 | KeyBindingFilter(QLineEdit *line_edit, QPushButton *ok_button); |
160 | virtual ~KeyBindingFilter(); | |
161 | ~KeyBindingFilter() override; | |
161 | 162 | |
162 | 163 | enum KeyState { DENY_KEY, ACCEPT_KEY, SUBMIT_KEY }; |
163 | 164 | |
164 | 165 | protected: |
165 | bool eventFilter(QObject *obj, QEvent *event); | |
166 | bool eventFilter(QObject *obj, QEvent *event) override; | |
166 | 167 | |
167 | 168 | private: |
168 | 169 | void Reset(); |
490 | 491 | KeyBindingEditorbuttonBox->button(QDialogButtonBox::Ok); |
491 | 492 | CHECK(ok_button != nullptr); |
492 | 493 | |
493 | filter_.reset(new KeyBindingFilter(KeyBindingLineEdit, ok_button)); | |
494 | filter_ = absl::make_unique<KeyBindingFilter>(KeyBindingLineEdit, ok_button); | |
494 | 495 | KeyBindingLineEdit->installEventFilter(filter_.get()); |
495 | 496 | |
496 | 497 | // no right click |
31 | 31 | |
32 | 32 | #include <QtGui/QGuiApplication> |
33 | 33 | #include <QtGui/QtGui> |
34 | ||
35 | 34 | #include <memory> |
36 | 35 | #include <string> |
37 | 36 | |
49 | 48 | // |trigger_parent| is the object who was the trigger of launching the editor. |
50 | 49 | // QPushButton can be a trigger_parent. |
51 | 50 | KeyBindingEditor(QWidget *parent, QWidget *trigger_parent); |
52 | virtual ~KeyBindingEditor(); | |
51 | ~KeyBindingEditor() override; | |
53 | 52 | |
54 | 53 | QWidget *mutable_trigger_parent() const { return trigger_parent_; } |
55 | 54 | |
60 | 59 | // For some reason, KeyBindingEditor lanuched by |
61 | 60 | // QItemDelegate looses focus. We overwrite |
62 | 61 | // setVisible() function to call raise() and activateWindow(). |
63 | virtual void setVisible(bool visible) { | |
62 | void setVisible(bool visible) override { | |
64 | 63 | QWidget::setVisible(visible); |
65 | 64 | if (visible) { |
66 | 65 | raise(); |
30 | 30 | |
31 | 31 | #include <QtGui/QtGui> |
32 | 32 | #include <QtWidgets/QPushButton> |
33 | ||
34 | 33 | #include <memory> |
35 | 34 | |
36 | 35 | #include "base/logging.h" |
26 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | #ifndef MOZC_GUI_KEYCOMMAND_DIALOG_KEYBINDING_EDITOR_DELEGATE_H_ | |
30 | #define MOZC_GUI_KEYCOMMAND_DIALOG_KEYBINDING_EDITOR_DELEGATE_H_ | |
29 | #ifndef MOZC_GUI_CONFIG_DIALOG_KEYBINDING_EDITOR_DELEGATE_H_ | |
30 | #define MOZC_GUI_CONFIG_DIALOG_KEYBINDING_EDITOR_DELEGATE_H_ | |
31 | 31 | |
32 | 32 | #include <QtCore/QModelIndex> |
33 | 33 | #include <QtCore/QObject> |
42 | 42 | Q_OBJECT |
43 | 43 | public: |
44 | 44 | explicit KeyBindingEditorDelegate(QObject *parent = nullptr); |
45 | virtual ~KeyBindingEditorDelegate(); | |
45 | ~KeyBindingEditorDelegate() override; | |
46 | 46 | |
47 | 47 | QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, |
48 | const QModelIndex &index) const; | |
48 | const QModelIndex &index) const override; | |
49 | 49 | |
50 | void setEditorData(QWidget *editor, const QModelIndex &index) const; | |
50 | void setEditorData(QWidget *editor, const QModelIndex &index) const override; | |
51 | 51 | void setModelData(QWidget *editor, QAbstractItemModel *model, |
52 | const QModelIndex &index) const; | |
52 | const QModelIndex &index) const override; | |
53 | 53 | |
54 | 54 | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, |
55 | const QModelIndex &index) const; | |
55 | const QModelIndex &index) const override; | |
56 | 56 | |
57 | 57 | private slots: |
58 | 58 | void CommitAndCloseEditor(); |
60 | 60 | }; |
61 | 61 | } // namespace gui |
62 | 62 | } // namespace mozc |
63 | #endif // MOZC_GUI_KEYCOMMAND_DIALOG_KEYBINDING_EDITOR_DELEGATE_H_ | |
63 | #endif // MOZC_GUI_CONFIG_DIALOG_KEYBINDING_EDITOR_DELEGATE_H_ |
37 | 37 | #include <QtWidgets/QFileDialog> |
38 | 38 | #include <QtWidgets/QMenu> |
39 | 39 | #include <QtWidgets/QMessageBox> |
40 | ||
41 | 40 | #include <algorithm> // for unique |
42 | 41 | #include <cctype> |
43 | 42 | #include <memory> |
325 | 324 | } |
326 | 325 | |
327 | 326 | std::string line; |
328 | if (!getline(*is, line)) { // must have 1st line | |
327 | if (!std::getline(*is, line)) { // must have 1st line | |
329 | 328 | return false; |
330 | 329 | } |
331 | 330 | |
336 | 335 | |
337 | 336 | invisible_keymap_table_.clear(); |
338 | 337 | direct_mode_commands_.clear(); |
339 | while (getline(*is, line)) { | |
338 | while (std::getline(*is, line)) { | |
340 | 339 | if (line.empty() || line[0] == '#') { |
341 | 340 | continue; |
342 | 341 | } |
30 | 30 | #define MOZC_GUI_CONFIG_DIALOG_KEYMAP_EDITOR_H_ |
31 | 31 | |
32 | 32 | #include <QtWidgets/QWidget> |
33 | ||
34 | 33 | #include <map> |
35 | 34 | #include <memory> |
36 | 35 | #include <set> |
52 | 51 | |
53 | 52 | public: |
54 | 53 | explicit KeyMapEditorDialog(QWidget *parent); |
55 | virtual ~KeyMapEditorDialog(); | |
54 | ~KeyMapEditorDialog() override; | |
56 | 55 | |
57 | 56 | // show a modal dialog |
58 | 57 | static bool Show(QWidget *parent, const std::string ¤t_keymap, |
59 | 58 | std::string *new_keymap); |
60 | 59 | |
61 | 60 | protected slots: |
62 | virtual void UpdateMenuStatus(); | |
63 | virtual void OnEditMenuAction(QAction *action); | |
61 | void UpdateMenuStatus() override; | |
62 | void OnEditMenuAction(QAction *action) override; | |
64 | 63 | |
65 | 64 | protected: |
66 | virtual std::string GetDefaultFilename() const { return "keymap.txt"; } | |
67 | virtual bool LoadFromStream(std::istream *is); | |
68 | virtual bool Update(); | |
65 | std::string GetDefaultFilename() const override { return "keymap.txt"; } | |
66 | bool LoadFromStream(std::istream *is) override; | |
67 | bool Update() override; | |
69 | 68 | |
70 | 69 | private: |
71 | 70 | std::string invisible_keymap_table_; |
96 | 96 | CHECK(ifs.get() != nullptr); // should never happen |
97 | 97 | std::string line, result; |
98 | 98 | std::vector<std::string> fields; |
99 | while (getline(*ifs.get(), line)) { | |
99 | while (std::getline(*ifs.get(), line)) { | |
100 | 100 | if (line.empty()) { |
101 | 101 | continue; |
102 | 102 | } |
127 | 127 | mutable_table_widget()->verticalHeader()->hide(); |
128 | 128 | |
129 | 129 | int row = 0; |
130 | while (getline(*is, line)) { | |
130 | while (std::getline(*is, line)) { | |
131 | 131 | if (line.empty()) { |
132 | 132 | continue; |
133 | 133 | } |
30 | 30 | #define MOZC_GUI_CONFIG_DIALOG_ROMAN_TABLE_EDITOR_H_ |
31 | 31 | |
32 | 32 | #include <QtWidgets/QWidget> |
33 | ||
34 | 33 | #include <memory> |
35 | 34 | #include <string> |
36 | 35 | |
47 | 46 | |
48 | 47 | public: |
49 | 48 | explicit RomanTableEditorDialog(QWidget *parent); |
50 | virtual ~RomanTableEditorDialog(); | |
49 | ~RomanTableEditorDialog() override; | |
51 | 50 | |
52 | 51 | // show a modal dialog |
53 | 52 | static bool Show(QWidget *parent, const std::string ¤t_keymap, |
54 | 53 | std::string *new_keymap); |
55 | 54 | |
56 | 55 | protected slots: |
57 | virtual void UpdateMenuStatus(); | |
58 | virtual void OnEditMenuAction(QAction *action); | |
56 | void UpdateMenuStatus() override; | |
57 | void OnEditMenuAction(QAction *action) override; | |
59 | 58 | |
60 | 59 | protected: |
61 | virtual std::string GetDefaultFilename() const { return "romantable.txt"; } | |
62 | virtual bool LoadFromStream(std::istream *is); | |
63 | virtual bool Update(); | |
60 | std::string GetDefaultFilename() const override { return "romantable.txt"; } | |
61 | bool LoadFromStream(std::istream *is) override; | |
62 | bool Update() override; | |
64 | 63 | |
65 | 64 | private: |
66 | 65 | bool LoadDefaultRomanTable(); |
26 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | #ifndef MOZC_GUI_CONFIRMATION_DIALOG_H_ | |
30 | #define MOZC_GUI_CONFIRMATION_DIALOG_H_ | |
29 | #ifndef MOZC_GUI_CONFIRMATION_DIALOG_CONFIRMATION_DIALOG_H_ | |
30 | #define MOZC_GUI_CONFIRMATION_DIALOG_CONFIRMATION_DIALOG_H_ | |
31 | 31 | |
32 | 32 | #include <QtCore/QObject> |
33 | 33 | #include <QtWidgets/QMessageBox> |
42 | 42 | |
43 | 43 | } // namespace gui |
44 | 44 | } // namespace mozc |
45 | #endif // MOZC_GUI_CONFIRMATION_DIALOG_H_ | |
45 | #endif // MOZC_GUI_CONFIRMATION_DIALOG_CONFIRMATION_DIALOG_H_ |
169 | 169 | "//protocol:commands_proto", |
170 | 170 | "//protocol:config_proto", |
171 | 171 | "//protocol:user_dictionary_storage_proto", |
172 | "@com_google_absl//absl/memory", | |
172 | 173 | ], |
173 | 174 | ) |
174 | 175 |
40 | 40 | public: |
41 | 41 | // overwrite paintEvent to draw a striped pattern to all |
42 | 42 | // view port area This makes the view port Mac style. |
43 | void paintEvent(QPaintEvent *event); | |
44 | void focusInEvent(QFocusEvent *event); | |
43 | void paintEvent(QPaintEvent *event) override; | |
44 | void focusInEvent(QFocusEvent *event) override; | |
45 | 45 | |
46 | 46 | // overwrite mouseDoubleClickEvent to catch click event |
47 | 47 | // when user double-clicked empty area in viewport |
48 | void mouseDoubleClickEvent(QMouseEvent *event); | |
48 | void mouseDoubleClickEvent(QMouseEvent *event) override; | |
49 | 49 | |
50 | 50 | explicit DictionaryContentTableWidget(QWidget *parent); |
51 | 51 |
68 | 68 | #include "gui/dictionary_tool/find_dialog.h" |
69 | 69 | #include "gui/dictionary_tool/import_dialog.h" |
70 | 70 | #include "protocol/user_dictionary_storage.pb.h" |
71 | #include "absl/memory/memory.h" | |
71 | 72 | |
72 | 73 | #ifdef OS_WIN |
73 | 74 | #include "gui/base/win_util.h" |
131 | 132 | progress_.reset(CreateProgressDialog(message, parent, file_.size())); |
132 | 133 | } |
133 | 134 | |
134 | bool IsAvailable() const { return file_.error() == QFile::NoError; } | |
135 | ||
136 | bool Next(std::string *line) { | |
135 | bool IsAvailable() const override { return file_.error() == QFile::NoError; } | |
136 | ||
137 | bool Next(std::string *line) override { | |
137 | 138 | if (stream_->atEnd()) { |
138 | 139 | return false; |
139 | 140 | } |
160 | 161 | return true; |
161 | 162 | } |
162 | 163 | |
163 | void Reset() { | |
164 | void Reset() override { | |
164 | 165 | file_.seek(0); |
165 | stream_.reset(new QTextStream); | |
166 | stream_ = absl::make_unique<QTextStream>(); | |
166 | 167 | stream_->setDevice(&file_); |
167 | 168 | stream_->setCodec("UTF-16"); |
168 | 169 | } |
189 | 190 | progress_.reset(CreateProgressDialog(message, parent, size)); |
190 | 191 | } |
191 | 192 | |
192 | bool IsAvailable() const { | |
193 | bool IsAvailable() const override { | |
193 | 194 | // This means that neither failbit nor badbit is set. |
194 | 195 | // TODO(yukawa): Consider to remove |ifs_->eof()|. Furthermore, we should |
195 | 196 | // replace IsAvailable() with something easier to understand, e.g., |
197 | 198 | return ifs_->good() || ifs_->eof(); |
198 | 199 | } |
199 | 200 | |
200 | bool Next(std::string *line) { | |
201 | bool Next(std::string *line) override { | |
201 | 202 | if (!ifs_->good()) { |
202 | 203 | return false; |
203 | 204 | } |
239 | 240 | return true; |
240 | 241 | } |
241 | 242 | |
242 | void Reset() { | |
243 | void Reset() override { | |
243 | 244 | // Clear state bit (eofbit, failbit, badbit). |
244 | 245 | ifs_->clear(); |
245 | 246 | ifs_->seekg(0, std::ios_base::beg); |
1030 | 1031 | } |
1031 | 1032 | |
1032 | 1033 | std::sort(rows->begin(), rows->end(), std::greater<int>()); |
1033 | std::vector<int>::const_iterator end = unique(rows->begin(), rows->end()); | |
1034 | std::vector<int>::const_iterator end = | |
1035 | std::unique(rows->begin(), rows->end()); | |
1034 | 1036 | |
1035 | 1037 | rows->resize(end - rows->begin()); |
1036 | 1038 | } |
33 | 33 | #include <QtWidgets/QPushButton> |
34 | 34 | #include <QtWidgets/QSplitter> |
35 | 35 | #include <QtWidgets/QSplitterHandle> |
36 | ||
37 | 36 | #include <memory> |
38 | 37 | #include <string> |
39 | 38 | #include <vector> |
64 | 63 | |
65 | 64 | public: |
66 | 65 | explicit DictionaryTool(QWidget *parent = 0); |
67 | virtual ~DictionaryTool(); | |
66 | ~DictionaryTool() override; | |
68 | 67 | |
69 | 68 | // return true DictionaryTool is available. |
70 | 69 | bool IsAvailable() const { return is_available_; } |
72 | 71 | protected: |
73 | 72 | // Override the default implementation to check unsaved |
74 | 73 | // modifications on data before closing the window. |
75 | void closeEvent(QCloseEvent *event); | |
76 | ||
77 | bool eventFilter(QObject *obj, QEvent *event); | |
74 | void closeEvent(QCloseEvent *event) override; | |
75 | ||
76 | bool eventFilter(QObject *obj, QEvent *event) override; | |
78 | 77 | |
79 | 78 | private slots: |
80 | 79 | void CreateDictionary(); |
29 | 29 | #include <QtCore/QTextCodec> |
30 | 30 | #include <QtCore/QTranslator> |
31 | 31 | #include <QtGui/QGuiApplication> |
32 | #include <string> | |
32 | 33 | |
33 | #include <string> | |
34 | 34 | #include "base/logging.h" |
35 | 35 | #include "base/system_util.h" |
36 | 36 | #include "gui/base/singleton_window_helper.h" |
44 | 44 | |
45 | 45 | public: |
46 | 46 | FindDialog(QWidget *parent, QTableWidget *table); |
47 | virtual ~FindDialog(); | |
47 | ~FindDialog() override; | |
48 | 48 | |
49 | 49 | protected: |
50 | void closeEvent(QCloseEvent *event); | |
51 | void showEvent(QShowEvent *event); | |
50 | void closeEvent(QCloseEvent *event) override; | |
51 | void showEvent(QShowEvent *event) override; | |
52 | 52 | |
53 | 53 | private slots: |
54 | 54 | void FindForward(); |
50 | 50 | |
51 | 51 | public: |
52 | 52 | ImportDialog(QWidget *parent = 0); |
53 | virtual ~ImportDialog(); | |
53 | ~ImportDialog() override; | |
54 | 54 | |
55 | 55 | // Accessor methods to get form values. |
56 | 56 | const QString file_name() const; |
39 | 39 | |
40 | 40 | public: |
41 | 41 | ZeroWidthSplitterHandle(Qt::Orientation orientation, QSplitter *parent); |
42 | virtual ~ZeroWidthSplitterHandle(); | |
42 | ~ZeroWidthSplitterHandle() override; | |
43 | 43 | |
44 | void paintEvent(QPaintEvent *event); | |
45 | QSize sizeHint() const; | |
44 | void paintEvent(QPaintEvent *event) override; | |
45 | QSize sizeHint() const override; | |
46 | 46 | }; |
47 | 47 | |
48 | 48 | class ZeroWidthSplitter : public QSplitter { |
49 | 49 | public: |
50 | 50 | ZeroWidthSplitter(QWidget *parent); |
51 | QSplitterHandle *createHandle(); | |
51 | QSplitterHandle *createHandle() override; | |
52 | 52 | }; |
53 | 53 | |
54 | 54 | #endif // MOZC_GUI_DICTIONARY_TOOL_ZERO_WIDTH_SPLITTER_H_ |
26 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | #ifndef MOZC_GUI_ERROR_MESSAGE_DIALOG_H_ | |
30 | #define MOZC_GUI_ERROR_MESSAGE_DIALOG_H_ | |
29 | #ifndef MOZC_GUI_ERROR_MESSAGE_DIALOG_ERROR_MESSAGE_DIALOG_H_ | |
30 | #define MOZC_GUI_ERROR_MESSAGE_DIALOG_ERROR_MESSAGE_DIALOG_H_ | |
31 | 31 | |
32 | 32 | #include <QtCore/QObject> |
33 | 33 | #include <QtWidgets/QMessageBox> |
51 | 51 | |
52 | 52 | public: |
53 | 53 | DeleyedMessageDialogHandler(QMessageBox *message_box); |
54 | virtual ~DeleyedMessageDialogHandler(); | |
54 | ~DeleyedMessageDialogHandler() override; | |
55 | 55 | |
56 | 56 | void Exec(); |
57 | 57 | |
63 | 63 | }; |
64 | 64 | } // namespace gui |
65 | 65 | } // namespace mozc |
66 | #endif // MOZC_GUI_ERROR_MESSAGE_DIALOG_H_ | |
66 | #endif // MOZC_GUI_ERROR_MESSAGE_DIALOG_ERROR_MESSAGE_DIALOG_H_ |
33 | 33 | #endif |
34 | 34 | |
35 | 35 | #include <QtGui/QtGui> |
36 | ||
36 | 37 | #include "base/logging.h" |
37 | 38 | #include "base/process.h" |
38 | 39 | #include "base/run_level.h" |
26 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | #ifndef MOZC_GUI_POST_INSTALL_DIALOG_H_ | |
30 | #define MOZC_GUI_POST_INSTALL_DIALOG_H_ | |
29 | #ifndef MOZC_GUI_POST_INSTALL_DIALOG_POST_INSTALL_DIALOG_H_ | |
30 | #define MOZC_GUI_POST_INSTALL_DIALOG_POST_INSTALL_DIALOG_H_ | |
31 | 31 | |
32 | 32 | #include <memory> |
33 | 33 | |
47 | 47 | |
48 | 48 | public: |
49 | 49 | PostInstallDialog(); |
50 | virtual ~PostInstallDialog(); | |
50 | ~PostInstallDialog() override; | |
51 | 51 | |
52 | 52 | protected slots: |
53 | 53 | virtual void OnOk(); |
54 | 54 | virtual void OnsetAsDefaultCheckBoxToggled(int state); |
55 | virtual void reject(); | |
55 | void reject() override; | |
56 | 56 | |
57 | 57 | private: |
58 | 58 | // - Sets this IME as the default IME if the check box on the |
67 | 67 | } // namespace gui |
68 | 68 | } // namespace mozc |
69 | 69 | |
70 | #endif // MOZC_GUI_POST_INSTALL_DIALOG_H_ | |
70 | #endif // MOZC_GUI_POST_INSTALL_DIALOG_POST_INSTALL_DIALOG_H_ |
33 | 33 | #endif |
34 | 34 | |
35 | 35 | #include <QtGui/QtGui> |
36 | ||
37 | 36 | #include <memory> |
38 | 37 | |
39 | 38 | #include "base/logging.h" |
40 | 40 | |
41 | 41 | public: |
42 | 42 | SetDefaultDialog(); |
43 | virtual ~SetDefaultDialog(); | |
43 | ~SetDefaultDialog() override; | |
44 | 44 | |
45 | 45 | protected slots: |
46 | virtual void accept(); | |
47 | virtual void reject(); | |
46 | void accept() override; | |
47 | void reject() override; | |
48 | 48 | |
49 | 49 | private: |
50 | 50 | bool SetCheckDefault(bool check_default); |
32 | 32 | "//tools/build_defs:qt.bzl", |
33 | 33 | "cc_qt_binary_mozc", |
34 | 34 | "cc_qt_library_mozc", |
35 | ) | |
36 | ||
37 | package( | |
38 | default_visibility = ["//gui:__subpackages__"], | |
39 | 35 | ) |
40 | 36 | |
41 | 37 | cc_library_mozc( |
26 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | #ifndef MOZC_GUI_WORD_REGISTER_DIALOG_H_ | |
30 | #define MOZC_GUI_WORD_REGISTER_DIALOG_H_ | |
29 | #ifndef MOZC_GUI_WORD_REGISTER_DIALOG_WORD_REGISTER_DIALOG_H_ | |
30 | #define MOZC_GUI_WORD_REGISTER_DIALOG_WORD_REGISTER_DIALOG_H_ | |
31 | 31 | |
32 | 32 | #include <QtCore/QString> |
33 | 33 | #include <QtGui/QtGui> |
34 | 34 | #include <QtWidgets/QDialog> |
35 | ||
36 | 35 | #include <memory> |
37 | 36 | |
38 | 37 | #include "base/port.h" |
56 | 55 | |
57 | 56 | public: |
58 | 57 | WordRegisterDialog(); |
59 | virtual ~WordRegisterDialog(); | |
58 | ~WordRegisterDialog() override; | |
60 | 59 | |
61 | 60 | bool IsAvailable() const; |
62 | 61 | |
120 | 119 | } // namespace gui |
121 | 120 | } // namespace mozc |
122 | 121 | |
123 | #endif // MOZC_GUI_WORD_REGISTER_DIALOG_H_ | |
122 | #endif // MOZC_GUI_WORD_REGISTER_DIALOG_WORD_REGISTER_DIALOG_H_ |
53 | 53 | explicit IPCServerThread(IPCServer *server) : server_(server) {} |
54 | 54 | virtual ~IPCServerThread() {} |
55 | 55 | virtual void Run() { |
56 | if (server_ != NULL) { | |
56 | if (server_ != nullptr) { | |
57 | 57 | server_->Loop(); |
58 | 58 | } |
59 | 59 | } |
67 | 67 | } // namespace |
68 | 68 | |
69 | 69 | void IPCServer::LoopAndReturn() { |
70 | if (server_thread_.get() == NULL) { | |
70 | if (server_thread_ == nullptr) { | |
71 | 71 | server_thread_.reset(new IPCServerThread(this)); |
72 | 72 | server_thread_->SetJoinable(true); |
73 | 73 | server_thread_->Start("IPCServer"); |
77 | 77 | } |
78 | 78 | |
79 | 79 | void IPCServer::Wait() { |
80 | if (server_thread_.get() != NULL) { | |
80 | if (server_thread_ != nullptr) { | |
81 | 81 | server_thread_->Join(); |
82 | 82 | server_thread_.reset(); |
83 | 83 | } |
136 | 136 | #ifdef OS_WIN |
137 | 137 | HANDLE handle = |
138 | 138 | ::OpenProcess(PROCESS_TERMINATE, false, static_cast<DWORD>(pid)); |
139 | if (NULL == handle) { | |
139 | if (nullptr == handle) { | |
140 | 140 | LOG(ERROR) << "OpenProcess failed: " << ::GetLastError(); |
141 | 141 | return false; |
142 | 142 | } |
189 | 189 | |
190 | 190 | IPCPathManager *IPCPathManager::GetIPCPathManager(const std::string &name) { |
191 | 191 | IPCPathManagerMap *manager_map = Singleton<IPCPathManagerMap>::get(); |
192 | DCHECK(manager_map != NULL); | |
192 | DCHECK(manager_map != nullptr); | |
193 | 193 | return manager_map->GetIPCPathManager(name); |
194 | 194 | } |
195 | 195 | |
203 | 203 | |
204 | 204 | bool IPCPathManager::SavePathName() { |
205 | 205 | scoped_lock l(mutex_.get()); |
206 | if (path_mutex_.get() != NULL) { | |
206 | if (path_mutex_ != nullptr) { | |
207 | 207 | return true; |
208 | 208 | } |
209 | 209 | |
274 | 274 | } |
275 | 275 | |
276 | 276 | bool IPCPathManager::GetPathName(std::string *ipc_name) const { |
277 | if (ipc_name == NULL) { | |
278 | LOG(ERROR) << "ipc_name is NULL"; | |
277 | if (ipc_name == nullptr) { | |
278 | LOG(ERROR) << "ipc_name is nullptr"; | |
279 | 279 | return false; |
280 | 280 | } |
281 | 281 | |
388 | 388 | #ifdef __APPLE__ |
389 | 389 | int name[] = {CTL_KERN, KERN_PROCARGS, static_cast<int>(pid)}; |
390 | 390 | size_t data_len = 0; |
391 | if (sysctl(name, arraysize(name), NULL, &data_len, NULL, 0) < 0) { | |
391 | if (sysctl(name, arraysize(name), nullptr, &data_len, nullptr, 0) < 0) { | |
392 | 392 | LOG(ERROR) << "sysctl KERN_PROCARGS failed"; |
393 | 393 | return false; |
394 | 394 | } |
395 | 395 | |
396 | 396 | server_path_.resize(data_len); |
397 | if (sysctl(name, arraysize(name), &server_path_[0], &data_len, NULL, 0) < 0) { | |
397 | if (sysctl(name, arraysize(name), &server_path_[0], &data_len, nullptr, 0) < | |
398 | 0) { | |
398 | 399 | LOG(ERROR) << "sysctl KERN_PROCARGS failed"; |
399 | 400 | return false; |
400 | 401 | } |
485 | 486 | ScopedHandle handle( |
486 | 487 | ::CreateFileW(wfilename.c_str(), GENERIC_READ, |
487 | 488 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
488 | NULL, OPEN_EXISTING, 0, NULL)); | |
489 | nullptr, OPEN_EXISTING, 0, nullptr)); | |
489 | 490 | |
490 | 491 | // ScopedHandle does not receive INVALID_HANDLE_VALUE and |
491 | // NULL check is appropriate here. | |
492 | if (NULL == handle.get()) { | |
492 | // nullptr check is appropriate here. | |
493 | if (nullptr == handle.get()) { | |
493 | 494 | LOG(ERROR) << "cannot open: " << filename << " " << ::GetLastError(); |
494 | 495 | return false; |
495 | 496 | } |
496 | 497 | |
497 | const DWORD size = ::GetFileSize(handle.get(), NULL); | |
498 | const DWORD size = ::GetFileSize(handle.get(), nullptr); | |
498 | 499 | if (-1 == static_cast<int>(size)) { |
499 | 500 | LOG(ERROR) << "GetFileSize failed: " << ::GetLastError(); |
500 | 501 | return false; |
509 | 510 | std::unique_ptr<char[]> buf(new char[size]); |
510 | 511 | |
511 | 512 | DWORD read_size = 0; |
512 | if (!::ReadFile(handle.get(), buf.get(), size, &read_size, NULL)) { | |
513 | if (!::ReadFile(handle.get(), buf.get(), size, &read_size, nullptr)) { | |
513 | 514 | LOG(ERROR) << "ReadFile failed: " << ::GetLastError(); |
514 | 515 | return false; |
515 | 516 | } |
122 | 122 | for (size_t i = 0; i < threads.size(); ++i) { |
123 | 123 | threads[i]->Join(); |
124 | 124 | delete threads[i]; |
125 | threads[i] = NULL; | |
125 | threads[i] = nullptr; | |
126 | 126 | } |
127 | 127 | } |
128 | 128 |
65 | 65 | class MultiConnections : public mozc::Thread { |
66 | 66 | public: |
67 | 67 | #ifdef __APPLE__ |
68 | MultiConnections() : mach_port_manager_(NULL) {} | |
68 | MultiConnections() : mach_port_manager_(nullptr) {} | |
69 | 69 | |
70 | 70 | void SetMachPortManager(mozc::MachPortManagerInterface *manager) { |
71 | 71 | mach_port_manager_ = manager; |
141 | 141 | for (size_t i = 0; i < cons.size(); ++i) { |
142 | 142 | cons[i]->Join(); |
143 | 143 | delete cons[i]; |
144 | cons[i] = NULL; | |
144 | cons[i] = nullptr; | |
145 | 145 | } |
146 | 146 | |
147 | 147 | mozc::IPCClient kill(kServerAddress, ""); |
29 | 29 | // The IPC implementation using core Mach APIs. |
30 | 30 | #ifdef __APPLE__ |
31 | 31 | |
32 | #include "ipc/ipc.h" | |
33 | ||
34 | #include <map> | |
35 | ||
36 | 32 | #include <launch.h> |
37 | 33 | #include <mach/mach.h> |
38 | 34 | #include <servers/bootstrap.h> |
35 | ||
36 | #include <map> | |
39 | 37 | |
40 | 38 | #include "base/logging.h" |
41 | 39 | #include "base/mac_util.h" |
42 | 40 | #include "base/singleton.h" |
43 | 41 | #include "base/thread.h" |
42 | #include "ipc/ipc.h" | |
44 | 43 | #include "ipc/ipc_path_manager.h" |
45 | 44 | |
46 | 45 | namespace mozc { |
100 | 99 | LAUNCH_KEY_GETJOB); |
101 | 100 | launch_data_t job = launch_msg(request); |
102 | 101 | launch_data_free(request); |
103 | if (job == NULL) { | |
102 | if (job == nullptr) { | |
104 | 103 | LOG(ERROR) << "Server job not found"; |
105 | 104 | return false; |
106 | 105 | } |
112 | 111 | } |
113 | 112 | |
114 | 113 | launch_data_t pid_data = launch_data_dict_lookup(job, LAUNCH_JOBKEY_PID); |
115 | if (pid_data == NULL || | |
114 | if (pid_data == nullptr || | |
116 | 115 | launch_data_get_type(pid_data) != LAUNCH_DATA_INTEGER) { |
117 | 116 | // PID information is unavailable, which means server is not running. |
118 | 117 | VLOG(2) << "Returned job is formatted wrongly: cannot find PID data."; |
189 | 188 | |
190 | 189 | // Client implementation |
191 | 190 | IPCClient::IPCClient(const string &name) |
192 | : name_(name), mach_port_manager_(NULL), ipc_path_manager_(NULL) { | |
191 | : name_(name), mach_port_manager_(nullptr), ipc_path_manager_(nullptr) { | |
193 | 192 | Init(name, ""); |
194 | 193 | } |
195 | 194 | |
196 | 195 | IPCClient::IPCClient(const string &name, const string &server_path) |
197 | : name_(name), mach_port_manager_(NULL), ipc_path_manager_(NULL) { | |
196 | : name_(name), mach_port_manager_(nullptr), ipc_path_manager_(nullptr) { | |
198 | 197 | Init(name, server_path); |
199 | 198 | } |
200 | 199 | |
213 | 212 | size_t *response_size, int32 timeout) { |
214 | 213 | last_ipc_error_ = IPC_NO_ERROR; |
215 | 214 | MachPortManagerInterface *manager = mach_port_manager_; |
216 | if (manager == NULL) { | |
215 | if (manager == nullptr) { | |
217 | 216 | manager = Singleton<DefaultClientMachPortManager>::get(); |
218 | 217 | } |
219 | 218 | |
220 | 219 | // Obtain the server port |
221 | 220 | mach_port_t server_port; |
222 | if (manager == NULL || !manager->GetMachPort(name_, &server_port)) { | |
221 | if (manager == nullptr || !manager->GetMachPort(name_, &server_port)) { | |
223 | 222 | last_ipc_error_ = IPC_NO_CONNECTION; |
224 | 223 | LOG(ERROR) << "Cannot connect to the server"; |
225 | 224 | return false; |
335 | 334 | } |
336 | 335 | |
337 | 336 | MachPortManagerInterface *manager = mach_port_manager_; |
338 | if (manager == NULL) { | |
337 | if (manager == nullptr) { | |
339 | 338 | manager = Singleton<DefaultClientMachPortManager>::get(); |
340 | 339 | } |
341 | 340 | |
344 | 343 | |
345 | 344 | // Server implementation |
346 | 345 | IPCServer::IPCServer(const string &name, int32 num_connections, int32 timeout) |
347 | : name_(name), mach_port_manager_(NULL), timeout_(timeout) { | |
346 | : name_(name), mach_port_manager_(nullptr), timeout_(timeout) { | |
348 | 347 | // This is a fake IPC path manager: it just stores the server |
349 | 348 | // version and IPC name but we don't use the stored IPC name itself. |
350 | 349 | // It's just for compatibility. |
369 | 368 | |
370 | 369 | bool IPCServer::Connected() const { |
371 | 370 | MachPortManagerInterface *manager = mach_port_manager_; |
372 | if (manager == NULL) { | |
371 | if (manager == nullptr) { | |
373 | 372 | manager = Singleton<DefaultServerMachPortManager>::get(); |
374 | 373 | } |
375 | 374 | |
378 | 377 | |
379 | 378 | void IPCServer::Loop() { |
380 | 379 | MachPortManagerInterface *manager = mach_port_manager_; |
381 | if (manager == NULL) { | |
380 | if (manager == nullptr) { | |
382 | 381 | manager = Singleton<DefaultServerMachPortManager>::get(); |
383 | 382 | } |
384 | 383 | |
385 | 384 | // Obtain the server port |
386 | 385 | mach_port_t server_port; |
387 | if (manager == NULL || !manager->GetMachPort(name_, &server_port)) { | |
386 | if (manager == nullptr || !manager->GetMachPort(name_, &server_port)) { | |
388 | 387 | LOG(ERROR) << "Failed to reserve the port."; |
389 | 388 | return; |
390 | 389 | } |
76 | 76 | } // namespace |
77 | 77 | |
78 | 78 | const std::string NamedEventUtil::GetEventPath(const char *name) { |
79 | name = (name == NULL) ? "NULL" : name; | |
79 | name = (name == nullptr) ? "nullptr" : name; | |
80 | 80 | std::string event_name = kEventPathPrefix; |
81 | 81 | event_name += SystemUtil::GetUserSidAsString(); |
82 | 82 | event_name += "."; |
102 | 102 | |
103 | 103 | #ifdef OS_WIN |
104 | 104 | NamedEventListener::NamedEventListener(const char *name) |
105 | : is_owner_(false), handle_(NULL) { | |
105 | : is_owner_(false), handle_(nullptr) { | |
106 | 106 | std::wstring event_path; |
107 | 107 | Util::UTF8ToWide(NamedEventUtil::GetEventPath(name), &event_path); |
108 | 108 | |
109 | 109 | handle_ = ::OpenEventW(EVENT_ALL_ACCESS, false, event_path.c_str()); |
110 | 110 | |
111 | if (handle_ == NULL) { | |
111 | if (handle_ == nullptr) { | |
112 | 112 | SECURITY_ATTRIBUTES security_attributes; |
113 | 113 | if (!WinSandbox::MakeSecurityAttributes(WinSandbox::kSharableEvent, |
114 | 114 | &security_attributes)) { |
119 | 119 | handle_ = |
120 | 120 | ::CreateEventW(&security_attributes, true, false, event_path.c_str()); |
121 | 121 | ::LocalFree(security_attributes.lpSecurityDescriptor); |
122 | if (handle_ == NULL) { | |
122 | if (handle_ == nullptr) { | |
123 | 123 | LOG(ERROR) << "CreateEvent() failed: " << ::GetLastError(); |
124 | 124 | return; |
125 | 125 | } |
131 | 131 | } |
132 | 132 | |
133 | 133 | NamedEventListener::~NamedEventListener() { |
134 | if (NULL != handle_) { | |
134 | if (nullptr != handle_) { | |
135 | 135 | ::CloseHandle(handle_); |
136 | 136 | } |
137 | handle_ = NULL; | |
138 | } | |
139 | ||
140 | bool NamedEventListener::IsAvailable() const { return (handle_ != NULL); } | |
137 | handle_ = nullptr; | |
138 | } | |
139 | ||
140 | bool NamedEventListener::IsAvailable() const { return (handle_ != nullptr); } | |
141 | 141 | |
142 | 142 | bool NamedEventListener::IsOwner() const { |
143 | 143 | return (IsAvailable() && is_owner_); |
168 | 168 | } |
169 | 169 | |
170 | 170 | HANDLE handle = ::OpenProcess(SYNCHRONIZE, FALSE, static_cast<DWORD>(pid)); |
171 | if (NULL == handle) { | |
171 | if (nullptr == handle) { | |
172 | 172 | LOG(ERROR) << "OpenProcess: failed() " << ::GetLastError() << " " << pid; |
173 | 173 | if (::GetLastError() == ERROR_INVALID_PARAMETER) { |
174 | 174 | LOG(ERROR) << "No such process found: " << pid; |
182 | 182 | |
183 | 183 | HANDLE handles[2] = {handle_, handle}; |
184 | 184 | |
185 | const DWORD handles_size = (handle == NULL) ? 1 : 2; | |
185 | const DWORD handles_size = (handle == nullptr) ? 1 : 2; | |
186 | 186 | |
187 | 187 | const DWORD ret = |
188 | 188 | ::WaitForMultipleObjects(handles_size, handles, FALSE, msec); |
205 | 205 | break; |
206 | 206 | } |
207 | 207 | |
208 | if (NULL != handle) { | |
208 | if (nullptr != handle) { | |
209 | 209 | ::CloseHandle(handle); |
210 | 210 | } |
211 | 211 | |
212 | 212 | return result; |
213 | 213 | } |
214 | 214 | |
215 | NamedEventNotifier::NamedEventNotifier(const char *name) : handle_(NULL) { | |
215 | NamedEventNotifier::NamedEventNotifier(const char *name) : handle_(nullptr) { | |
216 | 216 | std::wstring event_path; |
217 | 217 | Util::UTF8ToWide(NamedEventUtil::GetEventPath(name), &event_path); |
218 | 218 | handle_ = ::OpenEventW(EVENT_MODIFY_STATE, false, event_path.c_str()); |
219 | if (handle_ == NULL) { | |
219 | if (handle_ == nullptr) { | |
220 | 220 | LOG(ERROR) << "Cannot open Event name: " << name; |
221 | 221 | return; |
222 | 222 | } |
223 | 223 | } |
224 | 224 | |
225 | 225 | NamedEventNotifier::~NamedEventNotifier() { |
226 | if (NULL != handle_) { | |
226 | if (nullptr != handle_) { | |
227 | 227 | ::CloseHandle(handle_); |
228 | 228 | } |
229 | handle_ = NULL; | |
230 | } | |
231 | ||
232 | bool NamedEventNotifier::IsAvailable() const { return handle_ != NULL; } | |
229 | handle_ = nullptr; | |
230 | } | |
231 | ||
232 | bool NamedEventNotifier::IsAvailable() const { return handle_ != nullptr; } | |
233 | 233 | |
234 | 234 | bool NamedEventNotifier::Notify() { |
235 | 235 | if (!IsAvailable()) { |
45 | 45 | |
46 | 46 | #ifdef OS_WIN |
47 | 47 | ProcessWatchDog::ProcessWatchDog() |
48 | : event_(::CreateEventW(NULL, TRUE, FALSE, NULL)), | |
48 | : event_(::CreateEventW(nullptr, TRUE, FALSE, nullptr)), | |
49 | 49 | process_id_(UnknownProcessID), |
50 | 50 | thread_id_(UnknownThreadID), |
51 | 51 | timeout_(-1), |
52 | 52 | is_finished_(false), |
53 | 53 | mutex_(new Mutex) { |
54 | if (event_.get() == NULL) { | |
54 | if (event_.get() == nullptr) { | |
55 | 55 | LOG(ERROR) << "::CreateEvent() failed."; |
56 | 56 | return; |
57 | 57 | } |
61 | 61 | ProcessWatchDog::~ProcessWatchDog() { |
62 | 62 | is_finished_ = true; // set the flag to terminate the thread |
63 | 63 | |
64 | if (event_.get() != NULL) { | |
64 | if (event_.get() != nullptr) { | |
65 | 65 | ::SetEvent(event_.get()); // wake up WaitForMultipleObjects |
66 | 66 | } |
67 | 67 | |
70 | 70 | |
71 | 71 | bool ProcessWatchDog::SetID(ProcessID process_id, ThreadID thread_id, |
72 | 72 | int timeout) { |
73 | if (event_.get() == NULL) { | |
74 | LOG(ERROR) << "event is NULL"; | |
73 | if (event_.get() == nullptr) { | |
74 | LOG(ERROR) << "event is nullptr"; | |
75 | 75 | return false; |
76 | 76 | } |
77 | 77 | |
109 | 109 | const HANDLE handle = ::OpenProcess(SYNCHRONIZE, FALSE, process_id_); |
110 | 110 | const DWORD error = ::GetLastError(); |
111 | 111 | process_handle.reset(handle); |
112 | if (process_handle.get() == NULL) { | |
112 | if (process_handle.get() == nullptr) { | |
113 | 113 | LOG(ERROR) << "OpenProcess failed: " << process_id_ << " " << error; |
114 | 114 | switch (error) { |
115 | 115 | case ERROR_ACCESS_DENIED: |
129 | 129 | const HANDLE handle = ::OpenThread(SYNCHRONIZE, FALSE, thread_id_); |
130 | 130 | const DWORD error = ::GetLastError(); |
131 | 131 | thread_handle.reset(handle); |
132 | if (thread_handle.get() == NULL) { | |
132 | if (thread_handle.get() == nullptr) { | |
133 | 133 | LOG(ERROR) << "OpenThread failed: " << thread_id_ << " " << error; |
134 | 134 | switch (error) { |
135 | 135 | case ERROR_ACCESS_DENIED: |
163 | 163 | |
164 | 164 | // set handles |
165 | 165 | DWORD size = 1; |
166 | if (process_handle.get() != NULL) { | |
166 | if (process_handle.get() != nullptr) { | |
167 | 167 | VLOG(2) << "Inserting process handle"; |
168 | 168 | handles[size] = process_handle.get(); |
169 | 169 | types[size] = ProcessWatchDog::PROCESS_SIGNALED; |
170 | 170 | ++size; |
171 | 171 | } |
172 | 172 | |
173 | if (thread_handle.get() != NULL) { | |
173 | if (thread_handle.get() != nullptr) { | |
174 | 174 | VLOG(2) << "Inserting thread handle"; |
175 | 175 | handles[size] = thread_handle.get(); |
176 | 176 | types[size] = ProcessWatchDog::THREAD_SIGNALED; |
275 | 275 | if (errno == EPERM) { |
276 | 276 | Signaled(ProcessWatchDog::PROCESS_ACCESS_DENIED_SIGNALED); |
277 | 277 | } else if (errno == ESRCH) { |
278 | // Since we are polling the process by NULL signal, | |
278 | // Since we are polling the process by nullptr signal, | |
279 | 279 | // it is essentially impossible to tell the process is not found |
280 | 280 | // or terminated. |
281 | 281 | Signaled(ProcessWatchDog::PROCESS_SIGNALED); |
29 | 29 | // OS_LINUX only. Note that OS_ANDROID/OS_NACL don't reach here. |
30 | 30 | #if defined(OS_LINUX) |
31 | 31 | |
32 | #include "ipc/ipc.h" | |
33 | ||
34 | 32 | #include <arpa/inet.h> |
35 | 33 | #include <fcntl.h> |
36 | 34 | #include <libgen.h> |
50 | 48 | #include "base/file_util.h" |
51 | 49 | #include "base/logging.h" |
52 | 50 | #include "base/thread.h" |
51 | #include "ipc/ipc.h" | |
53 | 52 | #include "ipc/ipc_path_manager.h" |
54 | 53 | |
55 | 54 | #ifndef UNIX_PATH_MAX |
81 | 80 | FD_SET(socket, &fds); |
82 | 81 | tv.tv_sec = timeout / 1000; |
83 | 82 | tv.tv_usec = 1000 * (timeout % 1000); |
84 | if (select(socket + 1, &fds, NULL, NULL, &tv) < 0) { | |
83 | if (select(socket + 1, &fds, nullptr, nullptr, &tv) < 0) { | |
85 | 84 | // Mac OS X and glibc implementations of strerror() return a pointer to a |
86 | 85 | // string literal whenever errno is in a valid range, and thus thread-safe. |
87 | 86 | // Probably we don't have to use the cumbersome strerror_r() function. |
106 | 105 | FD_SET(socket, &fds); |
107 | 106 | tv.tv_sec = timeout / 1000; |
108 | 107 | tv.tv_usec = 1000 * (timeout % 1000); |
109 | if (select(socket + 1, NULL, &fds, NULL, &tv) < 0) { | |
108 | if (select(socket + 1, nullptr, &fds, nullptr, &tv) < 0) { | |
110 | 109 | LOG(WARNING) << "select() failed: " << strerror(errno); |
111 | 110 | return true; |
112 | 111 | } |
227 | 226 | IPCClient::IPCClient(const std::string &name) |
228 | 227 | : socket_(kInvalidSocket), |
229 | 228 | connected_(false), |
230 | ipc_path_manager_(NULL), | |
229 | ipc_path_manager_(nullptr), | |
231 | 230 | last_ipc_error_(IPC_NO_ERROR) { |
232 | 231 | Init(name, ""); |
233 | 232 | } |
235 | 234 | IPCClient::IPCClient(const std::string &name, const std::string &server_path) |
236 | 235 | : socket_(kInvalidSocket), |
237 | 236 | connected_(false), |
238 | ipc_path_manager_(NULL), | |
237 | ipc_path_manager_(nullptr), | |
239 | 238 | last_ipc_error_(IPC_NO_ERROR) { |
240 | 239 | Init(name, server_path); |
241 | 240 | } |
245 | 244 | |
246 | 245 | // Try twice, because key may be changed. |
247 | 246 | IPCPathManager *manager = IPCPathManager::GetIPCPathManager(name); |
248 | if (manager == NULL) { | |
247 | if (manager == nullptr) { | |
249 | 248 | LOG(ERROR) << "IPCPathManager::GetIPCPathManager failed"; |
250 | 249 | return; |
251 | 250 | } |
407 | 406 | } |
408 | 407 | |
409 | 408 | IPCServer::~IPCServer() { |
410 | if (server_thread_.get() != NULL) { | |
409 | if (server_thread_.get() != nullptr) { | |
411 | 410 | server_thread_->Terminate(); |
412 | 411 | } |
413 | 412 | ::shutdown(socket_, SHUT_RDWR); |
429 | 428 | IPCErrorType last_ipc_error = IPC_NO_ERROR; |
430 | 429 | pid_t pid = 0; |
431 | 430 | while (!error) { |
432 | const int new_sock = ::accept(socket_, NULL, NULL); | |
431 | const int new_sock = ::accept(socket_, nullptr, nullptr); | |
433 | 432 | if (new_sock < 0) { |
434 | 433 | LOG(FATAL) << "accept() failed: " << strerror(errno); |
435 | 434 | return; |
70 | 70 | : output_string_(output_string), |
71 | 71 | max_data_size_(max_data_size), |
72 | 72 | output_size_(0) { |
73 | if (NULL != output_string_) { | |
73 | if (nullptr != output_string_) { | |
74 | 74 | output_string_->clear(); |
75 | 75 | } |
76 | 76 | VLOG(2) << "max_data_size=" << max_data_size; |
84 | 84 | LOG(WARNING) << "too long data max_data_size=" << max_data_size_; |
85 | 85 | } |
86 | 86 | |
87 | if (output_string_ != NULL) { | |
87 | if (output_string_ != nullptr) { | |
88 | 88 | VLOG(2) << "Recived: " << size << " bytes to std::string"; |
89 | 89 | output_string_->append(buf, size); |
90 | 90 | } |
108 | 108 | public: |
109 | 109 | explicit ScopedHINTERNET(HINTERNET internet) : internet_(internet) {} |
110 | 110 | virtual ~ScopedHINTERNET() { |
111 | if (NULL != internet_) { | |
111 | if (nullptr != internet_) { | |
112 | 112 | VLOG(2) << "InternetCloseHandle() called"; |
113 | 113 | ::InternetCloseHandle(internet_); |
114 | 114 | } |
115 | internet_ = NULL; | |
115 | internet_ = nullptr; | |
116 | 116 | } |
117 | 117 | |
118 | 118 | HINTERNET get() { return internet_; } |
177 | 177 | |
178 | 178 | Stopwatch stopwatch = stopwatch.StartNew(); |
179 | 179 | |
180 | HANDLE event = ::CreateEvent(NULL, FALSE, FALSE, NULL); | |
181 | if (NULL == event) { | |
180 | HANDLE event = ::CreateEvent(nullptr, FALSE, FALSE, nullptr); | |
181 | if (nullptr == event) { | |
182 | 182 | LOG(ERROR) << "CreateEvent() failed: " << ::GetLastError(); |
183 | 183 | return false; |
184 | 184 | } |
185 | 185 | |
186 | ScopedHINTERNET internet(::InternetOpenA(kUserAgent, | |
187 | INTERNET_OPEN_TYPE_PRECONFIG, NULL, | |
188 | NULL, INTERNET_FLAG_ASYNC)); | |
189 | ||
190 | if (NULL == internet.get()) { | |
186 | ScopedHINTERNET internet( | |
187 | ::InternetOpenA(kUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, nullptr, | |
188 | nullptr, INTERNET_FLAG_ASYNC)); | |
189 | ||
190 | if (nullptr == internet.get()) { | |
191 | 191 | LOG(ERROR) << "InternetOpen() failed: " << ::GetLastError() << " " << url; |
192 | 192 | ::CloseHandle(event); |
193 | 193 | return false; |
234 | 234 | } |
235 | 235 | |
236 | 236 | ScopedHINTERNET session(::InternetConnect(internet.get(), uc.lpszHostName, |
237 | uc.nPort, NULL, NULL, | |
237 | uc.nPort, nullptr, nullptr, | |
238 | 238 | INTERNET_SERVICE_HTTP, 0, 0)); |
239 | 239 | |
240 | if (NULL == session.get()) { | |
240 | if (nullptr == session.get()) { | |
241 | 241 | LOG(ERROR) << "InternetConnect() failed: " << ::GetLastError() << " " |
242 | 242 | << url; |
243 | 243 | return false; |
254 | 254 | CHECK(method); |
255 | 255 | |
256 | 256 | ScopedHINTERNET handle(::HttpOpenRequestW( |
257 | session.get(), method, uri.c_str(), NULL, NULL, NULL, | |
257 | session.get(), method, uri.c_str(), nullptr, nullptr, nullptr, | |
258 | 258 | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_NO_UI | |
259 | 259 | INTERNET_FLAG_PRAGMA_NOCACHE | |
260 | 260 | (uc.nScheme == INTERNET_SCHEME_HTTPS ? INTERNET_FLAG_SECURE : 0), |
261 | 261 | reinterpret_cast<DWORD_PTR>(event))); |
262 | 262 | |
263 | if (NULL == handle.get()) { | |
263 | if (nullptr == handle.get()) { | |
264 | 264 | LOG(ERROR) << "HttpOpenRequest() failed: " << ::GetLastError() << " " |
265 | 265 | << url; |
266 | 266 | return false; |
279 | 279 | } |
280 | 280 | } |
281 | 281 | |
282 | if (!::HttpSendRequest(handle.get(), NULL, 0, | |
283 | (type == HTTP_POST) ? (LPVOID)post_data : NULL, | |
282 | if (!::HttpSendRequest(handle.get(), nullptr, 0, | |
283 | (type == HTTP_POST) ? (LPVOID)post_data : nullptr, | |
284 | 284 | (type == HTTP_POST) ? post_size : 0)) { |
285 | 285 | if (!CheckTimeout(event, stopwatch.GetElapsedMilliseconds(), |
286 | 286 | option.timeout)) { |
426 | 426 | Singleton<CurlInitializer>::get()->Init(); |
427 | 427 | |
428 | 428 | CURL *curl = curl_easy_init(); |
429 | if (NULL == curl) { | |
429 | if (nullptr == curl) { | |
430 | 430 | LOG(ERROR) << "curl_easy_init() failed"; |
431 | 431 | return false; |
432 | 432 | } |
461 | 461 | } |
462 | 462 | } |
463 | 463 | |
464 | struct curl_slist *slist = NULL; | |
464 | struct curl_slist *slist = nullptr; | |
465 | 465 | for (size_t i = 0; i < option.headers.size(); ++i) { |
466 | 466 | VLOG(2) << "Add header: " << option.headers[i]; |
467 | 467 | slist = curl_slist_append(slist, option.headers[i].c_str()); |
468 | 468 | } |
469 | 469 | |
470 | if (slist != NULL) { | |
470 | if (slist != nullptr) { | |
471 | 471 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); |
472 | 472 | } |
473 | 473 | |
511 | 511 | } |
512 | 512 | |
513 | 513 | curl_easy_cleanup(curl); |
514 | if (slist != NULL) { | |
514 | if (slist != nullptr) { | |
515 | 515 | curl_slist_free_all(slist); |
516 | 516 | } |
517 | 517 | |
542 | 542 | public: |
543 | 543 | virtual bool Get(const std::string &url, const HTTPClient::Option &option, |
544 | 544 | std::string *output_string) const { |
545 | return RequestInternal(HTTP_GET, url, NULL, 0, option, output_string); | |
545 | return RequestInternal(HTTP_GET, url, nullptr, 0, option, output_string); | |
546 | 546 | } |
547 | 547 | |
548 | 548 | virtual bool Head(const std::string &url, const HTTPClient::Option &option, |
549 | 549 | std::string *output_string) const { |
550 | return RequestInternal(HTTP_HEAD, url, NULL, 0, option, output_string); | |
550 | return RequestInternal(HTTP_HEAD, url, nullptr, 0, option, output_string); | |
551 | 551 | } |
552 | 552 | |
553 | 553 | virtual bool Post(const std::string &url, const std::string &data, |
560 | 560 | |
561 | 561 | namespace { |
562 | 562 | |
563 | const HTTPClientInterface *g_http_connection_handler = NULL; | |
563 | const HTTPClientInterface *g_http_connection_handler = nullptr; | |
564 | 564 | |
565 | 565 | const HTTPClientInterface &GetHTTPClient() { |
566 | if (g_http_connection_handler == NULL) { | |
566 | if (g_http_connection_handler == nullptr) { | |
567 | 567 | g_http_connection_handler = Singleton<HTTPClientImpl>::get(); |
568 | 568 | } |
569 | 569 | return *g_http_connection_handler; |
284 | 284 | return false; |
285 | 285 | } |
286 | 286 | return JsonUtil::JsonValueToProtobufMessage( |
287 | value, reflection->MutableMessage(message, field, NULL)); | |
287 | value, reflection->MutableMessage(message, field, nullptr)); | |
288 | 288 | break; |
289 | 289 | } |
290 | 290 | default: { |
438 | 438 | result = false; |
439 | 439 | } else { |
440 | 440 | if (!JsonUtil::JsonValueToProtobufMessage( |
441 | value[i], reflection->AddMessage(message, field, NULL))) { | |
441 | value[i], reflection->AddMessage(message, field, nullptr))) { | |
442 | 442 | result = false; |
443 | 443 | } |
444 | 444 | } |
179 | 179 | } |
180 | 180 | } else if (reflection->HasField(*message, field)) { |
181 | 181 | if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) { |
182 | FillRequiredFields(reflection->MutableMessage(message, field, NULL)); | |
182 | FillRequiredFields(reflection->MutableMessage(message, field, nullptr)); | |
183 | 183 | } |
184 | 184 | } else if (field->is_required()) { |
185 | 185 | switch (field->cpp_type()) { |
220 | 220 | break; |
221 | 221 | } |
222 | 222 | case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { |
223 | FillRequiredFields(reflection->MutableMessage(message, field, NULL)); | |
223 | FillRequiredFields( | |
224 | reflection->MutableMessage(message, field, nullptr)); | |
224 | 225 | break; |
225 | 226 | } |
226 | 227 | default: { |
54 | 54 | // http://developer.apple.com/samplecode/CFProxySupportTool/ |
55 | 55 | static void PACResultCallback(void *client, CFArrayRef proxies, |
56 | 56 | CFErrorRef error) { |
57 | DCHECK((proxies == NULL && error != NULL) || | |
58 | (proxies != NULL && error == NULL)); | |
57 | DCHECK((proxies == nullptr && error != nullptr) || | |
58 | (proxies != nullptr && error == nullptr)); | |
59 | 59 | CFTypeRef *result = static_cast<CFTypeRef *>(client); |
60 | 60 | |
61 | if (result != NULL && *result == NULL) { | |
62 | if (error != NULL) { | |
61 | if (result != nullptr && *result == nullptr) { | |
62 | if (error != nullptr) { | |
63 | 63 | *result = CFRetain(error); |
64 | 64 | } else { |
65 | 65 | *result = CFRetain(proxies); |
76 | 76 | // The implementation is inspired by |
77 | 77 | // http://developer.apple.com/samplecode/CFProxySupportTool/ |
78 | 78 | CFDictionaryRef RetainOrExpandPacFile(CFURLRef cfurl, CFDictionaryRef proxy) { |
79 | CFDictionaryRef final_proxy = NULL; | |
79 | CFDictionaryRef final_proxy = nullptr; | |
80 | 80 | CFStringRef proxy_type = reinterpret_cast<CFStringRef>( |
81 | 81 | CFDictionaryGetValue(proxy, kCFProxyTypeKey)); |
82 | 82 | |
85 | 85 | // the pac URL server. However, it seems that the function seems to |
86 | 86 | // take care of caching in memory. So there are no additional |
87 | 87 | // latency problems here. |
88 | if (proxy_type != NULL && CFGetTypeID(proxy_type) == CFStringGetTypeID() && | |
88 | if (proxy_type != nullptr && CFGetTypeID(proxy_type) == CFStringGetTypeID() && | |
89 | 89 | CFEqual(proxy_type, kCFProxyTypeAutoConfigurationURL)) { |
90 | 90 | CFURLRef script_url = reinterpret_cast<CFURLRef>( |
91 | 91 | CFDictionaryGetValue(proxy, kCFProxyAutoConfigurationURLKey)); |
92 | if (script_url != NULL && CFGetTypeID(script_url) == CFURLGetTypeID()) { | |
93 | CFTypeRef result = NULL; | |
94 | CFStreamClientContext context = {0, &result, NULL, NULL, NULL}; | |
92 | if (script_url != nullptr && CFGetTypeID(script_url) == CFURLGetTypeID()) { | |
93 | CFTypeRef result = nullptr; | |
94 | CFStreamClientContext context = {0, &result, nullptr, nullptr, nullptr}; | |
95 | 95 | scoped_cftyperef<CFRunLoopSourceRef> runloop_source( |
96 | 96 | CFNetworkExecuteProxyAutoConfigurationURL( |
97 | 97 | script_url, cfurl, PACResultCallback, &context)); |
98 | 98 | const string label = MacUtil::GetLabelForSuffix("ProxyResolverMac"); |
99 | 99 | scoped_cftyperef<CFStringRef> private_runloop_mode( |
100 | CFStringCreateWithBytes(NULL, | |
100 | CFStringCreateWithBytes(nullptr, | |
101 | 101 | reinterpret_cast<const UInt8 *>(label.data()), |
102 | 102 | label.size(), kCFStringEncodingUTF8, false)); |
103 | 103 | CFRunLoopAddSource(CFRunLoopGetCurrent(), runloop_source.get(), |
107 | 107 | private_runloop_mode.get()); |
108 | 108 | |
109 | 109 | // resolving PAC succeeds |
110 | if (result != NULL && CFGetTypeID(result) == CFArrayGetTypeID() && | |
110 | if (result != nullptr && CFGetTypeID(result) == CFArrayGetTypeID() && | |
111 | 111 | CFArrayGetCount(reinterpret_cast<CFArrayRef>(result)) > 0) { |
112 | 112 | final_proxy = reinterpret_cast<CFDictionaryRef>( |
113 | 113 | CFArrayGetValueAtIndex(reinterpret_cast<CFArrayRef>(result), 0)); |
116 | 116 | LOG(WARNING) << "Failed to resolve PAC file. " |
117 | 117 | << "Possibly wrong PAC file is specified."; |
118 | 118 | } |
119 | if (result != NULL) { | |
119 | if (result != nullptr) { | |
120 | 120 | CFRelease(result); |
121 | 121 | } |
122 | 122 | } |
124 | 124 | |
125 | 125 | // If configuration isn't PAC or resolving PAC fails, just returns |
126 | 126 | // the retained proxy. |
127 | if (final_proxy == NULL) { | |
127 | if (final_proxy == nullptr) { | |
128 | 128 | final_proxy = reinterpret_cast<CFDictionaryRef>(CFRetain(proxy)); |
129 | 129 | } |
130 | 130 | |
145 | 145 | CFNetworkCopySystemProxySettings()); |
146 | 146 | #else |
147 | 147 | scoped_cftyperef<CFDictionaryRef> proxy_settings( |
148 | SCDynamicStoreCopyProxies(NULL)); | |
148 | SCDynamicStoreCopyProxies(nullptr)); | |
149 | 149 | #endif // OS_IOS |
150 | 150 | if (!proxy_settings.Verify(CFDictionaryGetTypeID())) { |
151 | 151 | LOG(ERROR) << "Failed to create proxy setting"; |
152 | 152 | return false; |
153 | 153 | } |
154 | 154 | |
155 | scoped_cftyperef<CFURLRef> cfurl( | |
156 | CFURLCreateWithBytes(NULL, reinterpret_cast<const UInt8 *>(url.data()), | |
157 | url.size(), kCFStringEncodingUTF8, NULL)); | |
155 | scoped_cftyperef<CFURLRef> cfurl(CFURLCreateWithBytes( | |
156 | nullptr, reinterpret_cast<const UInt8 *>(url.data()), url.size(), | |
157 | kCFStringEncodingUTF8, nullptr)); | |
158 | 158 | if (!cfurl.Verify(CFURLGetTypeID())) { |
159 | 159 | LOG(ERROR) << "Failed to create URL object from the specified URL"; |
160 | 160 | return false; |
188 | 188 | if (host.Verify(CFStringGetTypeID())) { |
189 | 189 | scoped_cftyperef<CFStringRef> host_desc; |
190 | 190 | if (port.Verify(CFNumberGetTypeID())) { |
191 | host_desc.reset(CFStringCreateWithFormat(NULL, NULL, CFSTR("%@:%@"), | |
192 | host.get(), port.get())); | |
191 | host_desc.reset(CFStringCreateWithFormat( | |
192 | nullptr, nullptr, CFSTR("%@:%@"), host.get(), port.get())); | |
193 | 193 | } else { |
194 | 194 | host_desc.reset(reinterpret_cast<CFStringRef>(host.get())); |
195 | 195 | CFRetain(host.get()); |
197 | 197 | if (host_desc.Verify(CFStringGetTypeID())) { |
198 | 198 | const char *hostdata_ptr = |
199 | 199 | CFStringGetCStringPtr(host_desc.get(), kCFStringEncodingUTF8); |
200 | if (hostdata_ptr != NULL) { | |
200 | if (hostdata_ptr != nullptr) { | |
201 | 201 | hostdata->assign(hostdata_ptr); |
202 | 202 | proxy_available = true; |
203 | 203 | } else { |
209 | 209 | } |
210 | 210 | if (proxy_available && username.Verify(CFStringGetTypeID()) && |
211 | 211 | password.Verify(CFStringGetTypeID())) { |
212 | scoped_cftyperef<CFStringRef> auth_desc(CFStringCreateWithFormat( | |
213 | NULL, NULL, CFSTR("%@:%@"), username.get(), password.get())); | |
212 | scoped_cftyperef<CFStringRef> auth_desc( | |
213 | CFStringCreateWithFormat(nullptr, nullptr, CFSTR("%@:%@"), | |
214 | username.get(), password.get())); | |
214 | 215 | if (auth_desc.Verify(CFStringGetTypeID())) { |
215 | 216 | const char *authdata_ptr = |
216 | 217 | CFStringGetCStringPtr(auth_desc.get(), kCFStringEncodingUTF8); |
217 | if (authdata_ptr != NULL) { | |
218 | if (authdata_ptr != nullptr) { | |
218 | 219 | authdata->assign(authdata_ptr); |
219 | 220 | } |
220 | 221 | } |
236 | 237 | namespace { |
237 | 238 | |
238 | 239 | // This is only used for dependency injection |
239 | ProxyManagerInterface *g_proxy_manager = NULL; | |
240 | ProxyManagerInterface *g_proxy_manager = nullptr; | |
240 | 241 | |
241 | 242 | ProxyManagerInterface *GetProxyManager() { |
242 | if (g_proxy_manager == NULL) { | |
243 | if (g_proxy_manager == nullptr) { | |
243 | 244 | #ifdef __APPLE__ |
244 | 245 | return Singleton<MacProxyManager>::get(); |
245 | 246 | #else |
967 | 967 | // candidates that have the same key length as |input_key| so that we can set |
968 | 968 | // a slightly smaller cost to REALTIME_TOP than these. |
969 | 969 | int realtime_cost_min = kInfinity; |
970 | Result *realtime_top_result = NULL; | |
970 | Result *realtime_top_result = nullptr; | |
971 | 971 | |
972 | 972 | for (size_t i = 0; i < results->size(); ++i) { |
973 | 973 | const Result &result = results->at(i); |
1043 | 1043 | |
1044 | 1044 | // Ensure that the REALTIME_TOP candidate has relatively smaller cost than |
1045 | 1045 | // those of REALTIME candidates. |
1046 | if (realtime_top_result != NULL) { | |
1046 | if (realtime_top_result != nullptr) { | |
1047 | 1047 | realtime_top_result->cost = std::max(0, realtime_cost_min - 10); |
1048 | 1048 | } |
1049 | 1049 | } |
94 | 94 | filter->Insert(words[i]); |
95 | 95 | } |
96 | 96 | |
97 | char *buf = NULL; | |
97 | char *buf = nullptr; | |
98 | 98 | size_t size = 0; |
99 | 99 | |
100 | 100 | LOG(INFO) << "writing bloomfilter: " << FLAGS_output; |
368 | 368 | // If |entry| is the target of prediction, |
369 | 369 | // create a new result and insert it to |results|. |
370 | 370 | // Can set |prev_entry| if there is a history segment just before |input_key|. |
371 | // |prev_entry| is an optional field. If set NULL, this field is just ignored. | |
372 | // This method adds a new result entry with score, pair<score, entry>, to | |
373 | // |results|. | |
371 | // |prev_entry| is an optional field. If set nullptr, this field is just | |
372 | // ignored. This method adds a new result entry with score, | |
373 | // pair<score, entry>, to |results|. | |
374 | 374 | bool LookupEntry(RequestType request_type, const std::string &input_key, |
375 | 375 | const std::string &key_base, |
376 | 376 | const Trie<std::string> *key_expanded, const Entry *entry, |
57 | 57 | "//ipc", |
58 | 58 | "//ipc:named_event", |
59 | 59 | "//protocol:renderer_proto", |
60 | "@com_google_absl//absl/memory", | |
60 | 61 | ] + select_mozc( |
61 | 62 | ios = ["//base:mac_util"], |
62 | 63 | ), |
45 | 45 | #include "ipc/ipc.h" |
46 | 46 | #include "ipc/named_event.h" |
47 | 47 | #include "protocol/renderer_command.pb.h" |
48 | #include "absl/memory/memory.h" | |
48 | 49 | |
49 | 50 | #ifdef __APPLE__ |
50 | 51 | #include "base/mac_util.h" |
83 | 84 | |
84 | 85 | class RendererLauncher : public RendererLauncherInterface, public Thread { |
85 | 86 | public: |
86 | bool CanConnect() const { | |
87 | bool CanConnect() const override { | |
87 | 88 | switch (renderer_status_) { |
88 | 89 | case RendererLauncher::RENDERER_UNKNOWN: |
89 | 90 | case RendererLauncher::RENDERER_READY: |
110 | 111 | return false; |
111 | 112 | } |
112 | 113 | |
113 | bool IsAvailable() const { | |
114 | bool IsAvailable() const override { | |
114 | 115 | return (renderer_status_ == RendererLauncher::RENDERER_READY); |
115 | 116 | } |
116 | 117 | |
117 | void StartRenderer(const std::string &name, const std::string &path, | |
118 | bool disable_renderer_path_check, | |
119 | IPCClientFactoryInterface *ipc_client_factory_interface) { | |
118 | void StartRenderer( | |
119 | const std::string &name, const std::string &path, | |
120 | bool disable_renderer_path_check, | |
121 | IPCClientFactoryInterface *ipc_client_factory_interface) override { | |
120 | 122 | renderer_status_ = RendererLauncher::RENDERER_LAUNCHING; |
121 | 123 | name_ = name; |
122 | 124 | path_ = path; |
125 | 127 | Thread::Start("Renderer"); |
126 | 128 | } |
127 | 129 | |
128 | void Run() { | |
130 | void Run() override { | |
129 | 131 | last_launch_time_ = Clock::GetTime(); |
130 | 132 | |
131 | 133 | NamedEventListener listener(name_.c_str()); |
205 | 207 | } |
206 | 208 | } |
207 | 209 | |
208 | bool ForceTerminateRenderer(const std::string &name) { | |
210 | bool ForceTerminateRenderer(const std::string &name) override { | |
209 | 211 | return IPCClient::TerminateServer(name); |
210 | 212 | } |
211 | 213 | |
212 | void OnFatal(RendererErrorType type) { | |
214 | void OnFatal(RendererErrorType type) override { | |
213 | 215 | LOG(ERROR) << "OnFatal is called: " << static_cast<int>(type); |
214 | 216 | |
215 | 217 | std::string error_type; |
230 | 232 | } |
231 | 233 | } |
232 | 234 | |
233 | void SetPendingCommand(const commands::RendererCommand &command) { | |
235 | void SetPendingCommand(const commands::RendererCommand &command) override { | |
234 | 236 | // ignore NOOP|SHUTDOWN |
235 | 237 | if (command.type() == commands::RendererCommand::UPDATE) { |
236 | 238 | scoped_lock l(&pending_command_mutex_); |
237 | 239 | if (!pending_command_) { |
238 | pending_command_.reset(new commands::RendererCommand); | |
240 | pending_command_ = absl::make_unique<commands::RendererCommand>(); | |
239 | 241 | } |
240 | pending_command_->CopyFrom(command); | |
241 | } | |
242 | } | |
243 | ||
244 | void set_suppress_error_dialog(bool suppress) { | |
242 | *pending_command_ = command; | |
243 | } | |
244 | } | |
245 | ||
246 | void set_suppress_error_dialog(bool suppress) override { | |
245 | 247 | suppress_error_dialog_ = suppress; |
246 | 248 | } |
247 | 249 | |
253 | 255 | suppress_error_dialog_(false), |
254 | 256 | ipc_client_factory_interface_(nullptr) {} |
255 | 257 | |
256 | virtual ~RendererLauncher() { | |
258 | ~RendererLauncher() override { | |
257 | 259 | if (!IsRunning()) { |
258 | 260 | return; |
259 | 261 | } |
80 | 80 | class RendererClient : public RendererInterface { |
81 | 81 | public: |
82 | 82 | RendererClient(); |
83 | virtual ~RendererClient(); | |
83 | ~RendererClient() override; | |
84 | 84 | |
85 | 85 | // set IPC factory |
86 | 86 | void SetIPCClientFactory( |
95 | 95 | // platform separately. |
96 | 96 | // TODO(taku): move win32 code into RendererClient |
97 | 97 | void SetSendCommandInterface( |
98 | client::SendCommandInterface *send_command_interface) {} | |
98 | client::SendCommandInterface *send_command_interface) override {} | |
99 | 99 | |
100 | 100 | // activate renderer server |
101 | bool Activate(); | |
101 | bool Activate() override; | |
102 | 102 | |
103 | 103 | // return true if the renderer is available |
104 | bool IsAvailable() const; | |
104 | bool IsAvailable() const override; | |
105 | 105 | |
106 | 106 | // shutdown the renderer if it is running |
107 | 107 | // return true if the function finishes without error. |
109 | 109 | // Otherwise command::RendererCommand::SHUDDOWN is used. |
110 | 110 | bool Shutdown(bool force); |
111 | 111 | |
112 | bool ExecCommand(const commands::RendererCommand &command); | |
112 | bool ExecCommand(const commands::RendererCommand &command) override; | |
113 | 113 | |
114 | 114 | // Don't check the renderer server path. |
115 | 115 | // DO NOT call it except for testing |
67 | 67 | class TestIPCClient : public IPCClientInterface { |
68 | 68 | public: |
69 | 69 | TestIPCClient() { g_server_product_version = Version::GetMozcVersion(); } |
70 | ~TestIPCClient() {} | |
71 | ||
72 | bool Connected() const { return g_connected; } | |
73 | ||
74 | uint32 GetServerProtocolVersion() const { return g_server_protocol_version; } | |
75 | ||
76 | const std::string &GetServerProductVersion() const { | |
70 | ~TestIPCClient() override {} | |
71 | ||
72 | bool Connected() const override { return g_connected; } | |
73 | ||
74 | uint32 GetServerProtocolVersion() const override { | |
75 | return g_server_protocol_version; | |
76 | } | |
77 | ||
78 | const std::string &GetServerProductVersion() const override { | |
77 | 79 | return g_server_product_version; |
78 | 80 | } |
79 | 81 | |
80 | uint32 GetServerProcessId() const { return 0; } | |
82 | uint32 GetServerProcessId() const override { return 0; } | |
81 | 83 | |
82 | 84 | // just count up how many times Call is called. |
83 | virtual bool Call(const char *request, size_t request_size, char *response, | |
84 | size_t *response_size, int32 timeout) { | |
85 | bool Call(const char *request, size_t request_size, char *response, | |
86 | size_t *response_size, int32 timeout) override { | |
85 | 87 | g_counter++; |
86 | 88 | return true; |
87 | 89 | } |
96 | 98 | g_server_protocol_version = version; |
97 | 99 | } |
98 | 100 | |
99 | virtual IPCErrorType GetLastIPCError() const { return IPC_NO_ERROR; } | |
101 | IPCErrorType GetLastIPCError() const override { return IPC_NO_ERROR; } | |
100 | 102 | }; |
101 | 103 | |
102 | 104 | class TestIPCClientFactory : public IPCClientFactoryInterface { |
103 | 105 | public: |
104 | 106 | TestIPCClientFactory() {} |
105 | ~TestIPCClientFactory() {} | |
106 | ||
107 | virtual IPCClientInterface *NewClient(const std::string &name, | |
108 | const std::string &path_name) { | |
107 | ~TestIPCClientFactory() override {} | |
108 | ||
109 | IPCClientInterface *NewClient(const std::string &name, | |
110 | const std::string &path_name) override { | |
109 | 111 | return new TestIPCClient; |
110 | 112 | } |
111 | 113 | |
112 | virtual IPCClientInterface *NewClient(const std::string &name) { | |
114 | IPCClientInterface *NewClient(const std::string &name) override { | |
113 | 115 | return new TestIPCClient; |
114 | 116 | } |
115 | 117 | }; |
121 | 123 | force_terminate_renderer_called_(false), |
122 | 124 | available_(false), |
123 | 125 | can_connect_(false) {} |
124 | ~TestRendererLauncher() {} | |
126 | ~TestRendererLauncher() override {} | |
125 | 127 | |
126 | 128 | // implement StartServer. |
127 | 129 | // return true if server can launched successfully. |
128 | void StartRenderer(const std::string &name, const std::string &renderer_path, | |
129 | bool disable_renderer_path_check, | |
130 | IPCClientFactoryInterface *ipc_client_factory_interface) { | |
130 | void StartRenderer( | |
131 | const std::string &name, const std::string &renderer_path, | |
132 | bool disable_renderer_path_check, | |
133 | IPCClientFactoryInterface *ipc_client_factory_interface) override { | |
131 | 134 | start_renderer_called_ = true; |
132 | 135 | LOG(INFO) << name << " " << renderer_path; |
133 | 136 | } |
134 | 137 | |
135 | bool ForceTerminateRenderer(const std::string &name) { | |
138 | bool ForceTerminateRenderer(const std::string &name) override { | |
136 | 139 | force_terminate_renderer_called_ = true; |
137 | 140 | return true; |
138 | 141 | } |
139 | 142 | |
140 | void OnFatal(RendererErrorType type) { LOG(ERROR) << static_cast<int>(type); } | |
143 | void OnFatal(RendererErrorType type) override { | |
144 | LOG(ERROR) << static_cast<int>(type); | |
145 | } | |
141 | 146 | |
142 | 147 | // return true if the renderer is running |
143 | virtual bool IsAvailable() const { return available_; } | |
148 | bool IsAvailable() const override { return available_; } | |
144 | 149 | |
145 | 150 | // return true if client can make a IPC connection. |
146 | virtual bool CanConnect() const { return can_connect_; } | |
147 | ||
148 | virtual void SetPendingCommand(const commands::RendererCommand &command) { | |
151 | bool CanConnect() const override { return can_connect_; } | |
152 | ||
153 | void SetPendingCommand(const commands::RendererCommand &command) override { | |
149 | 154 | set_pending_command_called_ = true; |
150 | 155 | } |
151 | 156 | |
152 | virtual void set_suppress_error_dialog(bool suppress) {} | |
157 | void set_suppress_error_dialog(bool suppress) override {} | |
153 | 158 | |
154 | 159 | void Reset() { |
155 | 160 | start_renderer_called_ = false; |
87 | 87 | public: |
88 | 88 | explicit ParentApplicationWatchDog(RendererServer *renderer_server) |
89 | 89 | : renderer_server_(renderer_server) {} |
90 | virtual ~ParentApplicationWatchDog() {} | |
91 | ||
92 | void Signaled(ProcessWatchDog::SignalType type) { | |
90 | ~ParentApplicationWatchDog() override {} | |
91 | ||
92 | void Signaled(ProcessWatchDog::SignalType type) override { | |
93 | 93 | if (renderer_server_ == nullptr) { |
94 | 94 | LOG(ERROR) << "renderer_server is nullptr"; |
95 | 95 | return; |
118 | 118 | class RendererServerSendCommand : public client::SendCommandInterface { |
119 | 119 | public: |
120 | 120 | RendererServerSendCommand() : receiver_handle_(0) {} |
121 | virtual ~RendererServerSendCommand() {} | |
121 | ~RendererServerSendCommand() override {} | |
122 | 122 | |
123 | 123 | bool SendCommand(const mozc::commands::SessionCommand &command, |
124 | mozc::commands::Output *output) { | |
124 | mozc::commands::Output *output) override { | |
125 | 125 | #ifdef OS_WIN |
126 | 126 | if ((command.type() != commands::SessionCommand::SELECT_CANDIDATE) && |
127 | 127 | (command.type() != commands::SessionCommand::HIGHLIGHT_CANDIDATE) && |
47 | 47 | class RendererServer : public IPCServer { |
48 | 48 | public: |
49 | 49 | RendererServer(); |
50 | virtual ~RendererServer(); | |
50 | ~RendererServer() override; | |
51 | 51 | |
52 | 52 | void SetRendererInterface(RendererInterface *renderer_interface); |
53 | 53 | |
58 | 58 | int StartServer(); |
59 | 59 | |
60 | 60 | bool Process(const char *request, size_t request_size, char *response, |
61 | size_t *response_size); | |
61 | size_t *response_size) override; | |
62 | 62 | |
63 | 63 | // DEPRECATED: this functions is never called |
64 | 64 | virtual void AsyncHide() {} |
50 | 50 | public: |
51 | 51 | TestRenderer() : counter_(0), finished_(false) {} |
52 | 52 | |
53 | bool Activate() { return true; } | |
53 | bool Activate() override { return true; } | |
54 | 54 | |
55 | bool IsAvailable() const { return true; } | |
55 | bool IsAvailable() const override { return true; } | |
56 | 56 | |
57 | bool ExecCommand(const commands::RendererCommand &command) { | |
57 | bool ExecCommand(const commands::RendererCommand &command) override { | |
58 | 58 | if (finished_) { |
59 | 59 | return false; |
60 | 60 | } |
77 | 77 | public: |
78 | 78 | TestRendererServer() {} |
79 | 79 | |
80 | virtual ~TestRendererServer() {} | |
80 | ~TestRendererServer() override {} | |
81 | 81 | |
82 | int StartMessageLoop() { return 0; } | |
82 | int StartMessageLoop() override { return 0; } | |
83 | 83 | |
84 | 84 | // Not async for testing |
85 | bool AsyncExecCommand(std::string *proto_message) { | |
85 | bool AsyncExecCommand(std::string *proto_message) override { | |
86 | 86 | commands::RendererCommand command; |
87 | 87 | command.ParseFromString(*proto_message); |
88 | 88 | delete proto_message; |
93 | 93 | // A renderer launcher which does nothing. |
94 | 94 | class DummyRendererLauncher : public RendererLauncherInterface { |
95 | 95 | public: |
96 | void StartRenderer(const std::string &name, const std::string &renderer_path, | |
97 | bool disable_renderer_path_check, | |
98 | IPCClientFactoryInterface *ipc_client_factory_interface) { | |
96 | void StartRenderer( | |
97 | const std::string &name, const std::string &renderer_path, | |
98 | bool disable_renderer_path_check, | |
99 | IPCClientFactoryInterface *ipc_client_factory_interface) override { | |
99 | 100 | LOG(INFO) << name << " " << renderer_path; |
100 | 101 | } |
101 | 102 | |
102 | bool ForceTerminateRenderer(const std::string &name) { return true; } | |
103 | bool ForceTerminateRenderer(const std::string &name) override { return true; } | |
103 | 104 | |
104 | void OnFatal(RendererErrorType type) { LOG(ERROR) << static_cast<int>(type); } | |
105 | void OnFatal(RendererErrorType type) override { | |
106 | LOG(ERROR) << static_cast<int>(type); | |
107 | } | |
105 | 108 | |
106 | virtual bool IsAvailable() const { return true; } | |
109 | bool IsAvailable() const override { return true; } | |
107 | 110 | |
108 | virtual bool CanConnect() const { return true; } | |
111 | bool CanConnect() const override { return true; } | |
109 | 112 | |
110 | virtual void SetPendingCommand(const commands::RendererCommand &command) {} | |
113 | void SetPendingCommand(const commands::RendererCommand &command) override {} | |
111 | 114 | |
112 | virtual void set_suppress_error_dialog(bool suppress) {} | |
115 | void set_suppress_error_dialog(bool suppress) override {} | |
113 | 116 | }; |
114 | 117 | } // namespace |
115 | 118 | |
116 | 119 | class RendererServerTest : public ::testing::Test { |
117 | 120 | protected: |
118 | virtual void SetUp() { | |
121 | void SetUp() override { | |
119 | 122 | SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir); |
120 | 123 | } |
121 | 124 | }; |
72 | 72 | } |
73 | 73 | |
74 | 74 | bool RendererStyleHandlerImpl::GetRendererStyle(RendererStyle *style) { |
75 | style->CopyFrom(this->style_); | |
75 | *style = this->style_; | |
76 | 76 | return true; |
77 | 77 | } |
78 | 78 | bool RendererStyleHandlerImpl::SetRendererStyle(const RendererStyle &style) { |
79 | style_.CopyFrom(style); | |
79 | style_ = style; | |
80 | 80 | return true; |
81 | 81 | } |
82 | 82 | void RendererStyleHandlerImpl::GetDefaultRendererStyle(RendererStyle *style) { |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | 29 | #include "renderer/renderer_style_handler.h" |
30 | ||
30 | 31 | #include "protocol/renderer_style.pb.h" |
31 | 32 | #include "testing/base/public/gunit.h" |
32 | 33 |
173 | 173 | } |
174 | 174 | |
175 | 175 | const int all_cell_width = |
176 | accumulate(column_width_list_.begin(), column_width_list_.end(), 0); | |
176 | std::accumulate(column_width_list_.begin(), column_width_list_.end(), 0); | |
177 | 177 | |
178 | 178 | const int table_width = |
179 | 179 | row_rect_padding_pixels_ * 2 + // padding left and right |
214 | 214 | DCHECK(0 <= row && row < number_of_rows_); |
215 | 215 | DCHECK(0 <= column && column < column_width_list_.size()); |
216 | 216 | |
217 | const int width_of_left_cells = accumulate( | |
217 | const int width_of_left_cells = std::accumulate( | |
218 | 218 | column_width_list_.begin(), column_width_list_.begin() + column, 0); |
219 | 219 | |
220 | 220 | const int left = window_border_pixels_ + // border left |
345 | 345 | } |
346 | 346 | DCHECK(0 <= column && column < column_width_list_.size()); |
347 | 347 | |
348 | const int width_of_left_cells = accumulate( | |
348 | const int width_of_left_cells = std::accumulate( | |
349 | 349 | column_width_list_.begin(), column_width_list_.begin() + column, 0); |
350 | 350 | |
351 | 351 | const int left = window_border_pixels_ + // border left |
113 | 113 | TableLayout(); |
114 | 114 | |
115 | 115 | // Reset layout freeze and initialize the number of rows and columns. |
116 | void Initialize(int num_rows, int num_columns); | |
116 | void Initialize(int num_rows, int num_columns) override; | |
117 | 117 | |
118 | 118 | // Set layout element. |
119 | void SetVScrollBar(int width_in_pixels); | |
120 | void SetWindowBorder(int width_in_pixels); | |
121 | void SetRowRectPadding(int width_pixels); | |
119 | void SetVScrollBar(int width_in_pixels) override; | |
120 | void SetWindowBorder(int width_in_pixels) override; | |
121 | void SetRowRectPadding(int width_pixels) override; | |
122 | 122 | |
123 | 123 | // Ensure the cell size is same to or larger than the specified size. |
124 | 124 | // - size.width affects cells within the specified column. |
125 | 125 | // - size.height affects all cells. |
126 | 126 | // You should not call this function when the layout is frozen. |
127 | void EnsureCellSize(int column, const Size &size); | |
127 | void EnsureCellSize(int column, const Size &size) override; | |
128 | 128 | |
129 | 129 | // Ensure the total width from "from_column" to "to_width" is at |
130 | 130 | // least "width" or larger. If total width is smaller than the |
133 | 133 | // will be used. Note that "to_column" should be bigger than |
134 | 134 | // "from_column". Otherwise, the call will be ignored. If you |
135 | 135 | // want to ensure a cell width, you should use EnsureCellSize instead. |
136 | void EnsureColumnsWidth(int from_column, int to_column, int width); | |
136 | void EnsureColumnsWidth(int from_column, int to_column, int width) override; | |
137 | 137 | |
138 | 138 | // Ensure the size of header/footer is same to or larger than the |
139 | 139 | // specified size. |
140 | 140 | // You should not call this function when the layout is frozen. |
141 | void EnsureFooterSize(const Size &size_in_pixels); | |
142 | void EnsureHeaderSize(const Size &size_in_pixels); | |
141 | void EnsureFooterSize(const Size &size_in_pixels) override; | |
142 | void EnsureHeaderSize(const Size &size_in_pixels) override; | |
143 | 143 | |
144 | 144 | // Fix the layout and calculate the total size. |
145 | void FreezeLayout(); | |
146 | bool IsLayoutFrozen() const; | |
145 | void FreezeLayout() override; | |
146 | bool IsLayoutFrozen() const override; | |
147 | 147 | |
148 | 148 | // Get the rect which is bounding the specified cell. |
149 | 149 | // This rect does not include RowRectPadding. |
150 | 150 | // You should call FreezeLayout prior to this function. |
151 | Rect GetCellRect(int row, int column) const; | |
151 | Rect GetCellRect(int row, int column) const override; | |
152 | 152 | |
153 | 153 | // Get specified component rect. |
154 | 154 | // You should call FreezeLayout prior to these function. |
155 | Size GetTotalSize() const; | |
156 | Rect GetHeaderRect() const; | |
157 | Rect GetFooterRect() const; | |
158 | Rect GetVScrollBarRect() const; | |
155 | Size GetTotalSize() const override; | |
156 | Rect GetHeaderRect() const override; | |
157 | Rect GetFooterRect() const override; | |
158 | Rect GetVScrollBarRect() const override; | |
159 | 159 | Rect GetVScrollIndicatorRect(int begin_index, int end_index, |
160 | int candidates_total) const; | |
160 | int candidates_total) const override; | |
161 | 161 | |
162 | 162 | // Get the rect which is bounding the specified row. |
163 | 163 | // This rect includes RowRectPadding. |
164 | 164 | // You should call FreezeLayout prior to these function. |
165 | Rect GetRowRect(int row) const; | |
165 | Rect GetRowRect(int row) const override; | |
166 | 166 | |
167 | 167 | // Get specified row rect. |
168 | 168 | // This rect includes RowRectPadding. |
169 | 169 | // You should call FreezeLayout prior to these function. |
170 | Rect GetColumnRect(int column) const; | |
170 | Rect GetColumnRect(int column) const override; | |
171 | 171 | |
172 | 172 | // Parameter getters |
173 | int number_of_rows() const; | |
174 | int number_of_columns() const; | |
173 | int number_of_rows() const override; | |
174 | int number_of_columns() const override; | |
175 | 175 | |
176 | 176 | private: |
177 | 177 | std::vector<int> column_width_list_; |
27 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | 28 | |
29 | 29 | #include "renderer/table_layout.h" |
30 | ||
30 | 31 | #include "testing/base/public/gunit.h" |
31 | 32 | |
32 | 33 | namespace mozc { |
35 | 35 | namespace mozc { |
36 | 36 | |
37 | 37 | ConversionRequest::ConversionRequest() |
38 | : composer_(NULL), | |
38 | : composer_(nullptr), | |
39 | 39 | request_(&commands::Request::default_instance()), |
40 | 40 | config_(&config::ConfigHandler::DefaultConfig()), |
41 | 41 | use_actual_converter_for_realtime_conversion_(false), |
56 | 56 | |
57 | 57 | ConversionRequest::~ConversionRequest() {} |
58 | 58 | |
59 | bool ConversionRequest::has_composer() const { return composer_ != NULL; } | |
59 | bool ConversionRequest::has_composer() const { return composer_ != nullptr; } | |
60 | 60 | |
61 | 61 | const composer::Composer &ConversionRequest::composer() const { |
62 | 62 | DCHECK(composer_); |
83 | 83 | const dictionary::POSMatcher pos_matcher(data_manager.GetPOSMatcherData()); |
84 | 84 | open_bracket_id_ = pos_matcher.GetOpenBracketId(); |
85 | 85 | close_bracket_id_ = pos_matcher.GetCloseBracketId(); |
86 | user_pos_.reset(dictionary::UserPOS::CreateFromDataManager(data_manager)); | |
86 | user_pos_ = dictionary::UserPOS::CreateFromDataManager(data_manager); | |
87 | 87 | } |
88 | 88 | |
89 | 89 | DictionaryGenerator::~DictionaryGenerator() {} |
29 | 29 | |
30 | 30 | """Stub build rules.""" |
31 | 31 | |
32 | def portable_proto_library(name, proto_deps, deps = [], **kwargs): | |
33 | _ignore = [kwargs] | |
34 | native.cc_proto_library(name = name, deps = deps + proto_deps) | |
32 | def bzl_library(**kwargs): | |
33 | # Do nothing for OSS. | |
34 | _ignore = kwargs | |
35 | pass | |
36 | ||
37 | def cc_embed_data(**kwargs): | |
38 | # Do nothing for OSS. | |
39 | _ignore = kwargs | |
40 | pass | |
35 | 41 | |
36 | 42 | def jspb_proto_library(**kwargs): |
37 | 43 | # Do nothing for OSS. |
38 | 44 | _ignore = kwargs |
39 | 45 | pass |
40 | 46 | |
41 | def bzl_library(**kwargs): | |
42 | # Do nothing for OSS. | |
43 | _ignore = kwargs | |
44 | pass | |
47 | def portable_proto_library(name, proto_deps, deps = [], **kwargs): | |
48 | _ignore = [kwargs] | |
49 | native.cc_proto_library(name = name, deps = deps + proto_deps) | |
45 | 50 | |
46 | 51 | def py2and3_test(**kwargs): |
47 | 52 | native.py_test(**kwargs) |
37 | 37 | "cc_test_mozc", |
38 | 38 | ) |
39 | 39 | |
40 | package(default_visibility = ["//:__subpackages__"]) | |
41 | ||
42 | 40 | cc_binary_mozc( |
43 | 41 | name = "mozc_emacs_helper", |
44 | 42 | srcs = ["mozc_emacs_helper.cc"], |
45 | 43 | copts = [ |
46 | 44 | "$(STACK_FRAME_UNLIMITED)", # mozc_emacs_helper.cc |
47 | 45 | ], |
46 | visibility = ["//:__subpackages__"], | |
48 | 47 | deps = [ |
49 | 48 | ":mozc_emacs_helper_lib", |
50 | 49 | "//base", |