What's new? | Help | Directory | Sign in
Google
gears
Improving Your Web Browser
  
  
  
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2007, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Adds timers that are usable in worker threads.

#ifndef GEARS_TIMER_TIMER_H__
#define GEARS_TIMER_TIMER_H__

#if BROWSER_FF
#include <gecko_internal/nsITimer.h>
#endif

#include <map>
#include "third_party/linked_ptr/linked_ptr.h"
#include "third_party/scoped_ptr/scoped_ptr.h"

#include "gears/base/common/base_class.h"
#include "gears/base/common/common.h"
#include "gears/base/common/js_runner.h"
#include "gears/base/common/scoped_token.h"

// TODO(mpcomplete): remove when we have a cross-platform timer abstraction
#if BROWSER_NPAPI && defined(WIN32)
#define BROWSER_IE 1
#endif

// As an implementation detail, on IE we have a single WindowsPlatformTimer
// per GearsTimer. This is basically a Window (in the HWND sense) that
// registers for WM_TIMER messages. We re-use the same WindowsPlatformTimer
// for all timeouts and intervals set on a single GearsTimer object (the
// result of a factory.create('beta.timer') call in JavaScript).
//
// For Firefox, we use multiple "@mozilla.org/timer;1" XPCOM timers per
// GearsTimer objects, one for each setTimeout or setInterval call.
#if BROWSER_IE
class GearsTimer;

class WindowsPlatformTimer
: public CWindowImpl<WindowsPlatformTimer> {
public:
BEGIN_MSG_MAP(WindowsPlatformTimer)
MESSAGE_HANDLER(WM_TIMER, OnTimer)
END_MSG_MAP()

WindowsPlatformTimer(GearsTimer *gears_timer);
~WindowsPlatformTimer();

void Initialize();
void CancelGearsTimer(int timer_id);

private:
void OnFinalMessage(HWND hwnd);
LRESULT OnTimer(UINT msg, WPARAM timer_id,
LPARAM unused_param, BOOL& handled);

GearsTimer *gears_timer_;
bool in_handler_;

DISALLOW_EVIL_CONSTRUCTORS(WindowsPlatformTimer);
};
#endif


class GearsTimer
: public ModuleImplBaseClassVirtual
, public JsEventHandlerInterface {
public:
GearsTimer()
: ModuleImplBaseClassVirtual("GearsTimer"),
#if BROWSER_IE
platform_timer_(new WindowsPlatformTimer(this)),
#endif
next_timer_id_(1) {}
~GearsTimer() {
#if BROWSER_IE
if (platform_timer_->IsWindow()) {
platform_timer_->DestroyWindow();
} else {
delete platform_timer_;
}
#endif
}

// IN: variant timer_code, long timeout
// OUT: long
void SetTimeout(JsCallContext *context);

// IN: long timer_id
// OUT: nothing
void ClearTimeout(JsCallContext *context);

// IN: variant timer_code, long timeout
// OUT: long
void SetInterval(JsCallContext *context);

// IN: long timer_id
// OUT: nothing
void ClearInterval(JsCallContext *context);

// This is the callback used to handle the 'JSEVENT_UNLOAD' event.
void HandleEvent(JsEventType event_type);

private:
// Contains the information that represents a single timeout or interval.
// This includes the data representing the platform's timer structure, and
// the callback information for when the timer fires.
struct TimerInfo {
TimerInfo() : timer_id(0) {}
~TimerInfo();

void SetOwner(GearsTimer *new_owner) {
owner = new_owner;
}

linked_ptr<JsRootedCallback> callback;
std::string16 script;
bool repeat;
int timer_id;
scoped_refptr<GearsTimer> owner;
#if BROWSER_WEBKIT
// TimerInfo is stored in an STL container, so we need a linked_ptr to
// act as an intermediary.
// scoped_timer ensures that timer is released properly by OS when
// deleted.
class CFRunLoopTimerTraits {
public:
static void Free(CFRunLoopTimerRef x) {
CFRunLoopTimerInvalidate(x);
CFRelease(x);
}
static CFRunLoopTimerRef Default() { return NULL; }
};
typedef scoped_token<CFRunLoopTimerRef, CFRunLoopTimerTraits> scoped_timer;
linked_ptr<scoped_timer> platform_timer;
#elif BROWSER_FF
nsCOMPtr<nsITimer> platform_timer;
#endif
};

#if BROWSER_IE
WindowsPlatformTimer *platform_timer_;
friend class WindowsPlatformTimer;
#endif

// SetTimeout and SetInterval are very similar - the only difference being
// that the first one is a one-off and the second one repeats. This function
// is the common implementation.
void SetTimeoutOrInterval(JsCallContext *context, bool repeat);

// Similarly, ClearTimeout and ClearInterval are just two names for the same
// thing.
void ClearTimeoutOrInterval(JsCallContext *context);

// Sets up any structures that aren't needed until the interface is used.
// It's ok to call this multiple times.
void Initialize();

// Creates the timer. Returns the id of the new timer, or 0 on failure.
int CreateTimer(const TimerInfo &timer_info, int timeout);

// Callback for when the timer fires.
void HandleTimer(TimerInfo *timer_info);

std::map<int, TimerInfo> timers_;
int next_timer_id_;
scoped_ptr<JsEventMonitor> unload_monitor_;

#ifdef BROWSER_WEBKIT
friend void TimerCallback(CFRunLoopTimerRef ref, void* closure);
#elif BROWSER_FF
static void TimerCallback(nsITimer *timer, void *closure);
#endif

DISALLOW_EVIL_CONSTRUCTORS(GearsTimer);
};

// TODO(mpcomplete): remove when we have a cross-platform timer abstraction
#if BROWSER_NPAPI && defined(WIN32)
#undef BROWSER_IE
#endif

#endif // GEARS_TIMER_TIMER_H__
Show details Hide details

Change log

r2371 by gears.daemon on Jul 15 (4 days ago)   Diff
[Author: bpm]

Add as_out_parameter idiom from
scoped_refptr.h to scoped_ptr.h and
scoped_token.h.
     Change scoped_token to use a
DefaultValue instead of has_value_.
     Remove spurious includes of
scoped_token.h.

PRESUBMIT=passed
R=cprince,bgarcia
...
Go to: 
Project members, sign in to write a code review

Older revisions

r1361 by gears.daemon on Apr 11, 2008   Diff
[Author: cprince]

Gears Relocation: update files to
reflect new /third_party location.

...
r1297 by gears.daemon on Apr 04, 2008   Diff
[Author: bpm]

Migrate existing Gears reference-
counting code to use newly
standardized conventions: RefCount,
...
r1214 by gears.daemon on Mar 16, 2008   Diff
[Author: playmobil]

Timer module support in Safari.

PRESUBMIT=passed
...
All revisions of this file

File info

Size: 6639 bytes, 204 lines