1
// Copyright 2010, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
14
// * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
#include "gui/base/singleton_window_helper.h"
36
#include "base/base.h"
37
#include "base/file_stream.h"
38
#include "base/mutex.h"
39
#include "base/process_mutex.h"
40
#include "base/scoped_handle.h"
41
#include "base/util.h"
42
#include "gui/base/win_util.h"
43
#include "ipc/window_info.pb.h"
48
bool ReadWindowInfo(const string &lock_name,
49
ipc::WindowInfo *window_info) {
52
mozc::Util::UTF8ToWide(lock_name.c_str(), &wfilename);
54
mozc::ScopedHandle handle(
55
::CreateFileW(wfilename.c_str(),
57
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
58
NULL, OPEN_EXISTING, 0, NULL));
59
if (NULL == handle.get()) {
60
LOG(ERROR) << "cannot open: " << lock_name << " " << ::GetLastError();
64
const DWORD size = ::GetFileSize(handle.get(), NULL);
65
if (-1 == static_cast<int>(size)) {
66
LOG(ERROR) << "GetFileSize failed:" << ::GetLastError();
70
const DWORD kMaxFileSize = 2096;
71
if (size == 0 || size >= kMaxFileSize) {
72
LOG(ERROR) << "Invalid file size: " << kMaxFileSize;
76
scoped_array<char> buf(new char[size]);
79
if (!::ReadFile(handle.get(), buf.get(),
80
size, &read_size, NULL)) {
81
LOG(ERROR) << "ReadFile failed: " << ::GetLastError();
85
if (read_size != size) {
86
LOG(ERROR) << "read_size != size";
90
if (!window_info->ParseFromArray(buf.get(), static_cast<int>(size))) {
91
LOG(ERROR) << "ParseFromArra failed";
96
InputFileStream is(lock_name.c_str(), ios::binary|ios::in);
98
LOG(ERROR) << "cannot open: " << lock_name;
102
if (!window_info->ParseFromIstream(&is)) {
103
LOG(ERROR) << "ParseFromStream failed";
111
SingletonWindowHelper::SingletonWindowHelper(const string &name) {
112
mutex_.reset(new mozc::ProcessMutex(name.c_str()));
115
SingletonWindowHelper::~SingletonWindowHelper() {}
117
bool SingletonWindowHelper::FindPreviousWindow() {
118
ipc::WindowInfo window_info;
120
window_info.set_process_id(static_cast<uint32>(::GetCurrentProcessId()));
122
window_info.set_process_id(static_cast<uint32>(getpid()));
125
string window_info_str;
126
if (!window_info.SerializeToString(&window_info_str)) {
127
LOG(ERROR) << "SerializeToString failed";
131
if (!mutex_->LockAndWrite(window_info_str)) {
132
LOG(ERROR) << "config_dialog is already running";
139
// Activate window using process_id
142
bool SingletonWindowHelper::ActivatePreviousWindow() {
143
ipc::WindowInfo window_info;
144
if (!ReadWindowInfo(mutex_->lock_filename(), &window_info)) {
145
LOG(ERROR) << "ReadWindowInfo failed";
149
WinUtil::ActivateWindow(window_info.process_id());