1 : /* vim: se cin sw=2 ts=2 et : */
2 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is mozilla.org code.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Mozilla Foundation.
21 : * Portions created by the Initial Developer are Copyright (C) 2011
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef __mozilla_widget_GfxInfoCollector_h__
41 : #define __mozilla_widget_GfxInfoCollector_h__
42 :
43 : #include "jsapi.h"
44 :
45 : namespace mozilla {
46 : namespace widget {
47 :
48 :
49 : /* this is handy wrapper around JSAPI to make it more pleasant to use.
50 : * We collect the JSAPI errors and so that callers don't need to */
51 : class InfoObject
52 : {
53 : friend class GfxInfoBase;
54 :
55 : public:
56 : void DefineProperty(const char *name, int value)
57 : {
58 : if (!mOk)
59 : return;
60 :
61 : mOk = JS_DefineProperty(mCx, mObj, name, INT_TO_JSVAL(value), NULL, NULL, JSPROP_ENUMERATE);
62 : }
63 :
64 : void DefineProperty(const char *name, nsAString &value)
65 : {
66 : if (!mOk)
67 : return;
68 :
69 : const nsString &flat = PromiseFlatString(value);
70 : JSString *string = JS_NewUCStringCopyN(mCx, static_cast<const jschar*>(flat.get()), flat.Length());
71 : if (!string)
72 : mOk = JS_FALSE;
73 :
74 : if (!mOk)
75 : return;
76 :
77 : mOk = JS_DefineProperty(mCx, mObj, name, STRING_TO_JSVAL(string), NULL, NULL, JSPROP_ENUMERATE);
78 : }
79 :
80 : void DefineProperty(const char *name, const char *value)
81 : {
82 : nsAutoString string = NS_ConvertASCIItoUTF16(value);
83 : DefineProperty(name, string);
84 : }
85 :
86 : private:
87 : // We need to ensure that this object lives on the stack so that GC sees it properly
88 0 : InfoObject(JSContext *aCx) : mCx(aCx), mOk(JS_TRUE)
89 : {
90 0 : mObj = JS_NewObject(mCx, NULL, NULL, NULL);
91 0 : if (!mObj)
92 0 : mOk = JS_FALSE;
93 0 : }
94 : InfoObject(InfoObject&);
95 :
96 : JSContext *mCx;
97 : JSObject *mObj;
98 : JSBool mOk;
99 : };
100 :
101 : /*
102 :
103 : Here's an example usage:
104 :
105 : class Foo {
106 : Foo::Foo() : mInfoCollector(this, &Foo::GetAweseomeness) {}
107 :
108 : void GetAwesomeness(InfoObject &obj) {
109 : obj.DefineProperty("awesome", mAwesome);
110 : }
111 :
112 : int mAwesome;
113 :
114 : GfxInfoCollector<Foo> mInfoCollector;
115 : }
116 :
117 : This will define a property on the object
118 : returned from calling getInfo() on a
119 : GfxInfo object. e.g.
120 :
121 : gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo);
122 : info = gfxInfo.getInfo();
123 : if (info.awesome)
124 : alert(info.awesome);
125 :
126 : */
127 :
128 : class GfxInfoCollectorBase
129 : {
130 : public:
131 : GfxInfoCollectorBase();
132 : virtual void GetInfo(InfoObject &obj) = 0;
133 : virtual ~GfxInfoCollectorBase();
134 : };
135 :
136 : template<class T>
137 : class GfxInfoCollector : public GfxInfoCollectorBase
138 : {
139 : public:
140 : GfxInfoCollector(T* aPointer, void (T::*aFunc)(InfoObject &obj)) : mPointer(aPointer), mFunc(aFunc) {
141 : }
142 : virtual void GetInfo(InfoObject &obj) {
143 : (mPointer->*mFunc)(obj);
144 : }
145 :
146 : protected:
147 : T* mPointer;
148 : void (T::*mFunc)(InfoObject &obj);
149 :
150 : };
151 :
152 : }
153 : }
154 :
155 : #endif
|