Boris Nagaev avatar Boris Nagaev committed b338f33

update Gather to accept any AbstractStore (SWFStore and LocalStore)

Comments (0)

Files changed (2)

  * See the LICENSE file for terms of use.
  */
 
+#include <boost/foreach.hpp>
 #include <boost/algorithm/string/case_conv.hpp>
 
 #include <Wt/WApplication>
 #include <Wt/WEnvironment>
 
 #include "Gather.hpp"
+#include "LocalStore.hpp"
 #include "SWFStore.hpp"
 #include "rand.hpp"
 #include "util.hpp"
 namespace Wc {
 
 const std::string cookie_key = "userid";
-const std::string swf_key = "userid";
+const std::string store_key = "userid";
 
 Gather::Gather(const DataExplorer& explorer, WObject* parent):
     WObject(parent),
-    explorer_(explorer), swfstore_(0), signal_(this, "gather"),
+    explorer_(explorer), signal_(this, "gather"),
     honor_dnt_(false), dnt_(false) {
     const WEnvironment& env = wApp->environment();
     if (env.headerValue("DNT") == "1" ||
     bound_post(boost::bind(&Gather::explore_all, this))();
 }
 
+void Gather::add_store(AbstractStore* store, DataType type) {
+    store->value().connect(boost::bind(&Gather::store_handler, this,
+                                       _1, _2, type, store));
+    StoreAndType sat;
+    sat.store = store;
+    sat.type = type;
+    stores_.push_back(sat);
+}
+
 void Gather::set_swfstore(SWFStore* swfstore) {
-    swfstore_ = swfstore;
-    explore_swf();
+    add_store(swfstore, SWF);
+}
+
+void Gather::set_localstore(LocalStore* localstorage) {
+    add_store(localstorage, LOCAL_STORAGE);
 }
 
 // FIXME random numbers?? need statictical data
 int Gather::significance(DataType type) {
-    if (type == COOKIE || type == SWF) {
+    if (type == COOKIE || type == SWF || type == LOCAL_STORAGE) {
         return 10000;
     } else if (type == IP) {
         return 45;
     explore_simple();
     explore_cookie();
     explore_javascript();
-    explore_swf();
+    explore_stores();
 }
 
 void Gather::explore_simple() {
     doJavaScript(signal_.createCall(TO_S(JAVA), "navigator.javaEnabled()"));
 }
 
+void Gather::explore_stores() {
+    BOOST_FOREACH (const StoreAndType& sat, stores_) {
+        sat.store->get_value_of(store_key);
+    }
+}
+
 void Gather::explore_swf() {
-    if (swfstore_) {
-        swfstore_->value().connect(this, &Gather::swf_handler);
-        swfstore_->get_value_of(swf_key);
-    }
+    explore_stores();
 }
 
 void Gather::explorer_emitter(DataType type, const std::string& value) {
     explorer_emitter(static_cast<DataType>(type), value);
 }
 
-void Gather::swf_handler(std::string key, std::string value) {
-    if (key == swf_key) {
+void Gather::store_handler(std::string key, std::string value,
+                           DataType type, AbstractStore* store) {
+    if (key == store_key) {
         if (!value.empty()) {
-            explorer_emitter(SWF, value);
-        } else if (swfstore_) {
-            swfstore_->set_item(swf_key, rand_string());
+            explorer_emitter(type, value);
+        } else {
+            store->set_item(store_key, rand_string());
         }
     }
 }
 #ifndef WC_GATHER_HPP_
 #define WC_GATHER_HPP_
 
+#include <vector>
+
 #include <Wt/WGlobal>
 #include <Wt/WObject>
 #include <Wt/WJavaScript>
     enum DataType {
         COOKIE = 10, /**< Cookie value (significant virtuals evidence) */
         SWF = 20, /**< SWF value (significant virtuals evidence) */
+        LOCAL_STORAGE = 25, /**< localStorage (significant virtuals evidence) */
         IP = 30, /**< IP address (medium virtuals evidence) */
         PLUGINS = 40, /**< Plugins sorted list (medium virtuals evidence) */
         MIME_TYPES = 50, /**< Mime types sorted list (medium evidence) */
     */
     Gather(const DataExplorer& explorer, WObject* parent = 0);
 
+    /** Add object dealing with client-only data.
+    \note Ownership of the store is not transferred.
+    */
+    void add_store(AbstractStore* store, DataType type);
+
     /** Set SWF store to use information from browser shared flash storage.
     \note Ownership of the swfstore is not transferred.
     */
     void set_swfstore(SWFStore* swfstore);
 
+    /** Add localStorage.
+    \note Ownership of the swfstore is not transferred.
+    */
+    void set_localstore(LocalStore* localstorage);
+
     /** Return if Do Not Track header is honored.
     If Do Not Track header is honored and DNT: 1,
     then DataExplorer will not be called.
     */
     void explore_javascript();
 
+    /** Call explorer with added client-side stores.
+    The method is called from the constructor through bound_post().
+
+    The result is got through JavaScript call,
+    so the explorer is called not immediately.
+    */
+    void explore_stores();
+
     /** Call explorer with SWF.
     The method is called from set_swfstore().
 
-    You may want to recall this method later since
-    the explorer is not called if the flash cookie has just been created.
-
-    The result is got through JavaScript call,
-    so the explorer is called not immediately.
+    \deprecated Use explore_stores() instead.
     */
     void explore_swf();
 
 
 private:
     DataExplorer explorer_;
-    SWFStore* swfstore_;
+    struct StoreAndType {
+        AbstractStore* store;
+        DataType type;
+    };
+    std::vector<StoreAndType> stores_;
+    std::vector<AbstractStore*> to_delete_;
     JSignal<int, std::string> signal_; // int is DataType
     bool honor_dnt_;
     bool dnt_;
 
     void explorer_emitter(DataType type, const std::string& value);
     void explorer_emitter_helper(int type, std::string value);
-    void swf_handler(std::string key, std::string value);
+    void store_handler(std::string key, std::string value,
+                       DataType type, AbstractStore* store);
     static void doJavaScript(const std::string& javascript);
     void get_js_list(DataType type, const std::string& collection,
                      const std::string& property);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.