~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to gui/base/singleton_window_helper.cc

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2010, Google Inc.
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are
 
6
// met:
 
7
//
 
8
//     * Redistributions of source code must retain the above copyright
 
9
// notice, this list of conditions and the following disclaimer.
 
10
//     * Redistributions in binary form must reproduce the above
 
11
// copyright notice, this list of conditions and the following disclaimer
 
12
// in the documentation and/or other materials provided with the
 
13
// distribution.
 
14
//     * Neither the name of Google Inc. nor the names of its
 
15
// contributors may be used to endorse or promote products derived from
 
16
// this software without specific prior written permission.
 
17
//
 
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 
 
30
#include "gui/base/singleton_window_helper.h"
 
31
 
 
32
#ifdef OS_WINDOWS
 
33
#include <windows.h>
 
34
#endif
 
35
 
 
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"
 
44
 
 
45
namespace mozc {
 
46
namespace gui {
 
47
namespace {
 
48
bool ReadWindowInfo(const string &lock_name,
 
49
                    ipc::WindowInfo *window_info) {
 
50
#ifdef OS_WINDOWS
 
51
  wstring wfilename;
 
52
  mozc::Util::UTF8ToWide(lock_name.c_str(), &wfilename);
 
53
  {
 
54
    mozc::ScopedHandle handle(
 
55
      ::CreateFileW(wfilename.c_str(),
 
56
                    GENERIC_READ,
 
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();
 
61
      return false;
 
62
    }
 
63
 
 
64
    const DWORD size = ::GetFileSize(handle.get(), NULL);
 
65
    if (-1 == static_cast<int>(size)) {
 
66
      LOG(ERROR) << "GetFileSize failed:" << ::GetLastError();
 
67
      return false;
 
68
    }
 
69
 
 
70
    const DWORD kMaxFileSize = 2096;
 
71
    if (size == 0 || size >= kMaxFileSize) {
 
72
      LOG(ERROR) << "Invalid file size: " << kMaxFileSize;
 
73
      return false;
 
74
    }
 
75
 
 
76
    scoped_array<char> buf(new char[size]);
 
77
 
 
78
    DWORD read_size = 0;
 
79
    if (!::ReadFile(handle.get(), buf.get(),
 
80
                    size, &read_size, NULL)) {
 
81
      LOG(ERROR) << "ReadFile failed: " << ::GetLastError();
 
82
      return false;
 
83
    }
 
84
 
 
85
    if (read_size != size) {
 
86
      LOG(ERROR) << "read_size != size";
 
87
      return false;
 
88
    }
 
89
 
 
90
    if (!window_info->ParseFromArray(buf.get(), static_cast<int>(size))) {
 
91
      LOG(ERROR) << "ParseFromArra failed";
 
92
      return false;
 
93
    }
 
94
  }
 
95
#else
 
96
  InputFileStream is(lock_name.c_str(), ios::binary|ios::in);
 
97
  if (!is) {
 
98
    LOG(ERROR) << "cannot open: " << lock_name;
 
99
    return false;
 
100
  }
 
101
 
 
102
  if (!window_info->ParseFromIstream(&is)) {
 
103
    LOG(ERROR) << "ParseFromStream failed";
 
104
    return false;
 
105
  }
 
106
#endif
 
107
  return true;
 
108
}
 
109
}  // namespace
 
110
 
 
111
SingletonWindowHelper::SingletonWindowHelper(const string &name) {
 
112
  mutex_.reset(new mozc::ProcessMutex(name.c_str()));
 
113
}
 
114
 
 
115
SingletonWindowHelper::~SingletonWindowHelper() {}
 
116
 
 
117
bool SingletonWindowHelper::FindPreviousWindow() {
 
118
  ipc::WindowInfo window_info;
 
119
#ifdef OS_WINDOWS
 
120
  window_info.set_process_id(static_cast<uint32>(::GetCurrentProcessId()));
 
121
#else
 
122
  window_info.set_process_id(static_cast<uint32>(getpid()));
 
123
#endif
 
124
 
 
125
  string window_info_str;
 
126
  if (!window_info.SerializeToString(&window_info_str)) {
 
127
    LOG(ERROR) << "SerializeToString failed";
 
128
    return false;
 
129
  }
 
130
 
 
131
  if (!mutex_->LockAndWrite(window_info_str)) {
 
132
    LOG(ERROR) << "config_dialog is already running";
 
133
    return true;
 
134
  }
 
135
  return false;
 
136
}
 
137
 
 
138
// Windows
 
139
//  Activate window using process_id
 
140
// Others
 
141
//  Not implemented.
 
142
bool SingletonWindowHelper::ActivatePreviousWindow() {
 
143
  ipc::WindowInfo window_info;
 
144
  if (!ReadWindowInfo(mutex_->lock_filename(), &window_info)) {
 
145
    LOG(ERROR) << "ReadWindowInfo failed";
 
146
    return false;
 
147
  }
 
148
#ifdef OS_WINDOWS
 
149
  WinUtil::ActivateWindow(window_info.process_id());
 
150
  return true;
 
151
#else
 
152
  // not implemented
 
153
  return false;
 
154
#endif
 
155
}
 
156
 
 
157
}  // namespace gui
 
158
}  // namespace mozc