00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef _PROPERTY_HH_
00025 #define _PROPERTY_HH_
00026
00027 #include <typeinfo>
00028 #include <set>
00029 #include <assert.h>
00030
00031 #include <vos/corelibs/vos/vos.hh>
00032 #include <typechain/typechain.hh>
00033
00034
00035 #if defined(_WIN32) && defined(_MSC_VER)
00036 # ifdef PROPERTY_EXPORTS
00037 # define PROPERTY_API __declspec(dllexport)
00038 # else
00039 # define PROPERTY_API __declspec(dllimport)
00040 # endif
00041 #else
00042 # define PROPERTY_API
00043 #endif
00044
00045 #include "propertylistener.hh"
00046
00047 class PropertyAccessControl;
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 class PROPERTY_API Property : public virtual MetaObject, public ObjectExciseListener
00061 {
00062 protected:
00063
00064
00065 string raw_data;
00066 string raw_datatype;
00067 int raw_length;
00068 string data;
00069 string datatype;
00070 int data_length;
00071 bool decoded;
00072
00073 Property(MetaObject* superobject);
00074
00075 Property(MetaObject* superobject, const string& d, const string& dt);
00076
00077 typedef map<PropertyListener*, PropertyListener*, PropertyListenerSiteWrapper::Cmp> PropertyListenerMap;
00078
00079 PropertyListenerMap propertyListeners;
00080
00081
00082 static void writeStringToFile(const string& s, const string& filename);
00083
00084
00085 virtual void checkDecoded() ;
00086
00087 public:
00088
00089
00090
00091
00092
00093 virtual int getLength() ;
00094
00095
00096
00097
00098 virtual void read(string& target) ;
00099
00100
00101
00102
00103
00104
00105
00106 virtual void read(string& target, int start, int length) ;
00107
00108
00109
00110
00111 virtual string read();
00112
00113
00114
00115 virtual string read(int start, int length) ;
00116
00117
00118 virtual int getRawLength();
00119
00120
00121 virtual void readRaw(string& target);
00122
00123
00124
00125
00126
00127
00128
00129
00130 virtual void readRaw(string& target, int start, int length);
00131
00132
00133 virtual string readRaw();
00134
00135
00136 virtual string readRaw(int start, int length);
00137
00138
00139 virtual void asyncRead();
00140
00141
00142 virtual void asyncRead(int start, int length);
00143
00144
00145 virtual void write(int start, const string& newdata);
00146
00147
00148
00149
00150
00151 virtual void replace(const string& newdata, const string& newtype = "?");
00152
00153
00154 virtual const string getDataType() ;
00155
00156
00157 virtual const string getRawDataType();
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 static void Property::setProperty(Vobject& on,
00175 const string& propname,
00176 const string& propval,
00177 const string& valtype,
00178 PropertyAccessControl* pac)
00179 throw (AccessControlError, TypeChain::DecodeError, RemoteError);
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 static Vobject::ParentChildRelation& Property::setPropertyWithReturn(Vobject& on,
00190 const string& propname,
00191 const string& propval,
00192 const string& valtype,
00193 PropertyAccessControl* pac,
00194 bool letErrorThrough = false)
00195 throw (AccessControlError, TypeChain::DecodeError, RemoteError);
00196
00197
00198
00199
00200
00201
00202
00203 static void Property::setProperty(Vobject& on,
00204 const string& propname,
00205 double propval,
00206 PropertyAccessControl* pac)
00207 throw (AccessControlError, TypeChain::DecodeError, RemoteError);
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 static Vobject::ParentChildRelation& Property::setPropertyWithReturn(
00220 Vobject& on, const string& propname, double propval,
00221 PropertyAccessControl* pac)
00222 throw (AccessControlError, TypeChain::DecodeError, RemoteError);
00223
00224 static string getProperty(Vobject& on, const string& name);
00225
00226 static string getProperty(Vobject& on, const string& name, const string& defaultVal);
00227
00228
00229 static int getIntProperty(Vobject& on, const string& name);
00230
00231 static int getIntProperty(Vobject& on, const string& name, int defaultVal);
00232
00233 static int getIntProperty(Vobject& on, const string& name, int lowerLimit,
00234 int upperLimit);
00235
00236 static int getIntProperty(Vobject& on, const string& name, int defaultVal,
00237 int lowerLimit, int upperLimit);
00238
00239 static void setIntProperty(Vobject& on, const string& name, int val,
00240 PropertyAccessControl* ac);
00241
00242 static void setIntProperty(Vobject& on, const string& name, int val,
00243 int lowerLimit, int upperLimit, PropertyAccessControl* ac);
00244
00245
00246
00247 static double Property::getFloatProperty(Vobject& on, const string& name,
00248 double lowLim, double upLim) ;
00249
00250 static double Property::getFloatProperty(Vobject& on, const string& name,
00251 double defaultVal, double lowerLimit, double upperLimit) ;
00252
00253 static double Property::getFloatProperty(Vobject& on, const string& name,
00254 double defaultVal) ;
00255
00256 static double Property::getFloatProperty(Vobject& on, const string& name) ;
00257
00258 static void setFloatProperty(Vobject& on, const string& name, double val,
00259 double lowerLimit, double upperLimit, PropertyAccessControl* ac);
00260
00261 static void setFloatProperty(Vobject& on, const string& name, double val,
00262 PropertyAccessControl* ac);
00263
00264
00265
00266 static void setBoolProperty(Vobject& on, const string& name, bool val,
00267 PropertyAccessControl* ac);
00268
00269 static bool Property::getBoolProperty(Vobject& on, const string& name);
00270
00271 static bool Property::getBoolProperty(Vobject& on, const string& name,
00272 bool defaultVal);
00273
00274
00275
00276 static void setFloatVectorProperty(Vobject& on, const string& name, vector<double> v, PropertyAccessControl* ac);
00277
00278
00279 static vector<double> getFloatVectorProperty(Vobject& on, const string& name);
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 virtual void addPropertyListener(PropertyListener* pl, bool refresh = true);
00290
00291
00292
00293
00294
00295 virtual void removePropertyListener(PropertyListener* pl);
00296
00297
00298
00299
00300 static void Property::writeToFile(Property* p, const string& filename, int offset=0, int length=-1) {
00301 p->writeToFile(filename, offset, length);
00302 }
00303
00304
00305
00306
00307 void Property::writeToFile(const string& filename, int offset = 0, int length = -1);
00308
00309
00310
00311
00312 static void Property::writeRawToFile(Property* p, const string& filename, int offset=0, int length=-1) {
00313 p->writeRawToFile(filename, offset, length);
00314 }
00315
00316
00317
00318
00319 void Property::writeRawToFile(const string& filename, int offset = 0, int length = 1);
00320
00321
00322
00323
00324
00325
00326
00327 void Property::readFromFile(const string& filename, const string& type = "");
00328
00329
00330 static void registerExtenders();
00331
00332
00333 virtual const string getType();
00334
00335 virtual void notifyObjectExcise(RefCounted* object);
00336 virtual void doExcise();
00337 virtual void doSaveState(MessageBlock& output, set<string>& types, bool portable);
00338 };
00339
00340 class LocalProperty;
00341
00342 typedef PropertyAccessControl* (*PropertyAccessControlFactory)(const string& type, LocalProperty* lv);
00343
00344
00345
00346
00347
00348 class PROPERTY_API PropertyAccessControl
00349 {
00350 private:
00351 struct AssignAC
00352 {
00353 PropertyAccessControl* ac;
00354 PropertyAccessControlFactory fac;
00355 };
00356 static map<string, AssignAC> policies;
00357
00358 public:
00359
00360
00361
00362
00363 virtual bool checkReadPermission(PropertyEvent& event, string& message) = 0;
00364
00365
00366
00367
00368
00369 virtual bool checkChangePermission(PropertyEvent& event, string& message) = 0;
00370
00371
00372 virtual const string getPolicyName() = 0;
00373
00374
00375
00376
00377
00378 static void addPolicy(PropertyAccessControl* ac);
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 static void addPolicyFactory(const string& name, PropertyAccessControlFactory ac);
00392
00393
00394
00395
00396
00397
00398
00399
00400 static PropertyAccessControl* getPolicy(const string& name, LocalProperty* lv);
00401
00402
00403
00404
00405 static void removePolicy(const string& name);
00406 };
00407
00408
00409
00410 class PROPERTY_API NoPropertyAccessControl : public PropertyAccessControl
00411 {
00412 public:
00413 static NoPropertyAccessControl static_;
00414
00415 virtual const string getPolicyName()
00416 { return "insecure"; }
00417
00418 virtual bool checkReadPermission(PropertyEvent& event, string& message)
00419 { return true; };
00420 virtual bool checkChangePermission(PropertyEvent& event, string& message)
00421 { return true; };
00422 };
00423
00424
00425
00426
00427 class PROPERTY_API ReadOnlyPropertyAccessControl : public PropertyAccessControl
00428 {
00429 public:
00430 static ReadOnlyPropertyAccessControl static_;
00431
00432 virtual const string getPolicyName()
00433 { return "readonly"; }
00434
00435 virtual bool checkReadPermission(PropertyEvent& event, string& message)
00436 { return true; };
00437 virtual bool checkChangePermission(PropertyEvent& event, string& message)
00438 { message = "This property is read-only."; return false; };
00439 };
00440
00441 class LocalProperty;
00442
00443 class PROPERTY_API PropertySecurityInitializer
00444 {
00445 public:
00446 virtual void initializePropertyAccessControl(LocalProperty* p, Vobject& requester) = 0;
00447 };
00448
00449 class PROPERTY_API DoNothingPropertySecurityInitializer : public PropertySecurityInitializer
00450 {
00451 public:
00452 static DoNothingPropertySecurityInitializer static_;
00453 virtual void initializePropertyAccessControl(LocalProperty* p, Vobject& requester);
00454 };
00455
00456
00457
00458
00459 class PROPERTY_API LocalProperty : public virtual Property
00460 {
00461 protected:
00462 deque<PropertyAccessControl*> accesscontrols;
00463 static PropertySecurityInitializer* securitycallback;
00464
00465 void getLengthHandler(Message* m);
00466 void readHandler(Message* m);
00467 void writeHandler(Message* m);
00468 void replaceHandler(Message* m);
00469 void startListeningHandler(Message* m);
00470 void stopListeningHandler(Message* m);
00471 public:
00472 LocalProperty(MetaObject* superobject);
00473 LocalProperty(MetaObject* superobject, const string& d, const string& dt = "?");
00474 virtual ~LocalProperty();
00475 static MetaObject* new_LocalProperty(MetaObject* superobject, const string& type);
00476 static void setSecurityCallback(PropertySecurityInitializer*);
00477 virtual void write(Vobject& initiator, int start, const string& newdata);
00478 virtual void write(int start, const string& newdata);
00479 virtual void replace(Vobject& initiator, const string& newdata,
00480 const string& newtype = "?");
00481 virtual void replace(const string& newdata, const string& newtype = "?");
00482 virtual void addPropertyListener(PropertyListener* pl, bool refresh = true);
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 virtual void insertPropertyAccessControl(int pos, PropertyAccessControl* ac);
00502
00503
00504
00505
00506
00507 virtual void insertPropertyAccessControl(int pos, const string& policyname);
00508
00509
00510
00511
00512 virtual void removePropertyAccessControl(int pos);
00513
00514
00515
00516
00517
00518 virtual void removePropertyAccessControl(PropertyAccessControl* m);
00519
00520
00521
00522
00523
00524 virtual void removePropertyAccessControl(const string& policyname);
00525
00526
00527
00528 virtual const deque<PropertyAccessControl*>& getPropertyAccessControls();
00529
00530
00531
00532
00533
00534
00535 virtual bool validatePropertyAccess(PropertyEvent& e, string& message);
00536
00537 virtual void initializeSecurity(Vobject& requester);
00538 virtual const string getSource() { return data; }
00539
00540
00541 virtual void initialize() {
00542 insertPropertyAccessControl(-1, &NoPropertyAccessControl::static_);
00543 }
00544 };
00545
00546
00547
00548
00549 class PROPERTY_API RemoteProperty : public virtual Property
00550 {
00551 private:
00552 void lengthUpdateHandler(Message* m);
00553 void writeUpdateHandler(Message* m);
00554 void replaceUpdateHandler(Message* m);
00555 void startListeningReplyHandler(Message* m);
00556
00557 protected:
00558 int offset;
00559 virtual void checkDecoded() ;
00560 bool first_read;
00561
00562 public:
00563 RemoteProperty(MetaObject* superobject);
00564 static MetaObject* new_RemoteProperty(MetaObject* superobject, const string& type);
00565 virtual void read(string& target, int start, int length) ;
00566 virtual void readRaw(string& target, int start, int length);
00567 virtual void asyncRead();
00568 virtual void asyncRead(int start, int length);
00569 virtual int getLength() ;
00570 virtual int getRawLength();
00571 virtual const string getRawDataType();
00572 virtual void write(int start, const string& newdata);
00573 virtual void clearCache();
00574 virtual void replace(const string& newdata, const string& newtype = "?");
00575 virtual void addPropertyListener(PropertyListener* pl, bool refresh = true);
00576 virtual void removePropertyListener(PropertyListener* pl);
00577 };
00578
00579
00580
00581
00582
00583 #endif