[Author: steveblock]
[google-gears.git] / gears / localserver / firefox / update_task_ff.cc
blob276812d36a4fdc1d4aac0af803d2618dbb9689d7
1 // Copyright 2006, 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 <map>
28 #include "gears/base/common/mutex.h"
29 #include "gears/localserver/firefox/update_task_ff.h"
31 // We use a global map to ensure that only one update task per captured
32 // application executes at a time. Since Firefox only runs in a single
33 // process, this mutual exclusion only needs to work across threads rather
34 // than across processes.
35 typedef std::map< int64, FFUpdateTask* > UpdateTaskMap;
36 static Mutex running_tasks_mutex;
37 static UpdateTaskMap running_tasks;
39 void FFUpdateTask::Run() {
40 if (SetRunningTask(this)) {
41 UpdateTask::Run();
42 ClearRunningTask(this);
43 } else {
44 LOG(("FFUpdateTask - not running, another task is already running\n"));
45 NotifyTaskComplete(false);
49 // static
50 bool FFUpdateTask::SetRunningTask(FFUpdateTask *task) {
51 MutexLock lock(&running_tasks_mutex);
52 int64 key = task->store_.GetServerID();
53 UpdateTaskMap::iterator found = running_tasks.find(key);
54 if (found != running_tasks.end()) {
55 // An existing entry indicates another task is running, return false
56 // to prevent Run from proceeding.
57 return false;
58 } else {
59 running_tasks[key] = task;
60 return true;
64 // static
65 void FFUpdateTask::ClearRunningTask(FFUpdateTask *task) {
66 MutexLock lock(&running_tasks_mutex);
67 int64 key = task->store_.GetServerID();
68 UpdateTaskMap::iterator found = running_tasks.find(key);
69 if (found != running_tasks.end()) {
70 assert(found->second == task);
71 running_tasks.erase(found);
75 // Platform-specific implementation. See declaration in update_task.h.
76 // Returns true if an UpdateTask for the given store is running
77 bool UpdateTask::IsUpdateTaskForStoreRunning(int64 store_server_id) {
78 MutexLock lock(&running_tasks_mutex);
79 UpdateTaskMap::iterator found = running_tasks.find(store_server_id);
80 return (found != running_tasks.end());
83 // Platform-specific implementation. See declaration in update_task.h.
84 UpdateTask *UpdateTask::CreateUpdateTask() {
85 return new FFUpdateTask();