[Author: aa]
[google-gears.git] / gears / base / ie / detect_version_collision.cc
blob17345009a2f689858e0cd2f47ac4b415a5357963
1 // Copyright 2007, Google Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <assert.h>
27 #include <atlbase.h>
28 #include <atlsync.h>
29 #include <windows.h>
30 #include "common/genfiles/product_constants.h" // from OUTDIR
31 #include "gears/base/ie/detect_version_collision.h"
33 // We run our detection code once at startup
34 static bool OneTimeDetectVersionCollision();
35 static bool detected_collision = OneTimeDetectVersionCollision();
37 bool DetectedVersionCollision() {
38 return detected_collision;
41 // We use two named mutex objects to determine if another version of
42 // Gears is running. The first indicates at least one instance of some
43 // version of Gears is running. The second indicates at least one instance
44 // of our version of Gears is running. The embedded GUID in the mutex names
45 // should not be changed across different versions.
46 // TODO(michaeln): Ideally, these should be per user kernel objects rather
47 // than per local session to detect when the same user is running Gears in
48 // another windows session. See //depot/googleclient/ci/common/synchronized.cpp
49 // for some clues.
50 static CMutex running_mutex;
51 static CMutex our_version_running_mutex;
53 static const wchar_t *kMutexName =
54 L"IsRunning{685E0F7D-005A-40a0-B9F8-168FBA824248}";
56 static const wchar_t *kOurVersionMutexName =
57 L"IsRunning{685E0F7D-005A-40a0-B9F8-168FBA824248}-" PRODUCT_VERSION_STRING;
59 // Returns true if we detect that a different version of Gears is running.
60 // If no collision is detected, leaves mutex handles open to indicate that
61 // our version is running. If a collision is detected, this instance of Gears
62 // will be crippled, so we close all mutex handles so others don't see this
63 // instance as 'running'. Should only be called once.
64 static bool OneTimeDetectVersionCollision() {
65 // Detect if an instance of any version is running
66 if (!running_mutex.Create(NULL, FALSE, kMutexName)) {
67 assert(false);
68 return true;
70 bool already_running = (GetLastError() == ERROR_ALREADY_EXISTS);
72 // Detect if another instance of our version is running
73 if (!our_version_running_mutex.Create(NULL, FALSE, kOurVersionMutexName)) {
74 assert(false);
75 running_mutex.Close();
76 return true;
78 bool our_version_already_running = (GetLastError() == ERROR_ALREADY_EXISTS);
80 if (!already_running) {
81 // No collision, we are the first instance to run
82 assert(!our_version_already_running);
83 return false;
84 } else if (our_version_already_running) {
85 // No collision, other instances of our version are running
86 return false;
87 } else {
88 // A collision with a different version!
89 our_version_running_mutex.Close();
90 running_mutex.Close();
91 return true;
95 // Low tech UI to notify the user
96 static bool alerted_user = false;
97 const wchar_t *kVersionCollisionErrorMessage =
98 L"A " PRODUCT_FRIENDLY_NAME L" update has been downloaded.\n\n"
99 L"Please close all browser windows to complete the upgrade process.\n";
101 void MaybeNotifyUserOfVersionCollision() {
102 assert(detected_collision);
103 if (!alerted_user) {
104 NotifyUserOfVersionCollision();
108 void NotifyUserOfVersionCollision() {
109 assert(detected_collision);
110 alerted_user = true;
111 MessageBox(NULL, kVersionCollisionErrorMessage,
112 L"Please restart your browser", MB_OK);