[Author: zork]
[google-gears.git] / gears / base / common / html_event_monitor_ff.cc
blobc49a65a4484ec90459d60f47733983d326f2884b
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 <gecko_sdk/include/nsIDOMEvent.h>
27 #include <gecko_sdk/include/nsIDOMEventListener.h>
28 #include <assert.h>
30 #include "gears/base/common/html_event_monitor.h"
32 #include "gears/base/firefox/dom_utils.h"
35 // Provides an nsIDOMEventListener interface for events to invoke.
36 class HtmlEventMonitorHook
37 : public nsIDOMEventListener {
38 public:
39 HtmlEventMonitorHook(HtmlEventMonitor::HtmlEventCallback function,
40 void *user_param)
41 : function_(function),
42 user_param_(user_param) {
45 NS_IMETHOD HandleEvent(nsIDOMEvent *event) {
46 function_(user_param_); // invoke user callback
47 return NS_OK;
50 NS_DECL_ISUPPORTS
52 private:
53 nsIDOMEventTarget *orig_target;
55 HtmlEventMonitor::HtmlEventCallback function_;
56 void *user_param_;
58 DISALLOW_EVIL_CONSTRUCTORS(HtmlEventMonitorHook);
61 NS_IMPL_ISUPPORTS1(HtmlEventMonitorHook, nsIDOMEventListener)
65 // HtmlEventMonitor
68 bool HtmlEventMonitor::Start(nsIDOMEventTarget *event_source) {
69 assert(!event_hook_);
70 assert(!event_source_);
72 // create the event hook
73 scoped_ptr<HtmlEventMonitorHook> event_hook(
74 new HtmlEventMonitorHook(function_, user_param_));
75 event_hook->AddRef();
77 // connect to the event source
78 nsDependentString firefox_event_name(&event_name_[2]); // skip leading "on"
79 nsresult nr = event_source->AddEventListener(firefox_event_name,
80 event_hook.get(), PR_FALSE);
81 if (NS_FAILED(nr)) { return false; }
83 // only modify data members if everything succeeded
84 event_hook_ = event_hook.release();
85 event_source_ = event_source;
86 return true;
89 void HtmlEventMonitor::Stop() {
90 assert(event_source_);
91 assert(event_hook_);
93 // disconnect from the event source
94 nsDependentString firefox_event_name(&event_name_[2]); // skip leading "on"
95 event_source_->RemoveEventListener(firefox_event_name,
96 event_hook_, PR_FALSE);
97 event_source_ = NULL;
99 // destroy the event hook
100 event_hook_->Release();
101 event_hook_ = NULL;