Saturday, January 7, 2017

chromietabs - getting information about open tabs in Google Chrome browser

This blog post is about chromietabs [1] library, that provides information (URL) about currently open tabs in Google Chrome and Chromium web browsers.
TL;DR;

Motivation
I was always curious how do I spend time using my computer - what applications do I use, how much time do I spend using particular app etc. I know there is plenty of software that could track my activity on the market, however, none of them met my requirements, so couple of months ago I've started workertracker project [2], which does the job. I'll blog about the application in the future, since is not ready yet (there's a few pre-releases, so feel free to test it), however, the post is about quite important feature of the app - accessing current URL of the browser (in google-chrome, for now).

chromietabs library
Since I couldn't find any good solution on the internet, I've decided to implement a tiny library that will provide information about active tab in Google Chrome and Chromium web browsers.
An Interface of the chromietabs library is very simple, and it consists of few layers - depends on how detailed information you need, you should use another class. The example below demonstrates how can you access the URL of the current tab in google chrome:

ChromieTabs::SessionAnalyzer analyzer{
           ChromieTabs::SessionReader("Current Session")};
auto window_id = analyzer.get_current_window_id();
auto active_tab_id = analyzer.get_current_tab_id(window_id);
std::cout << analyzer.get_current_url(active_tab_id) << std::endl;

A full example can be found in the git repository [1].
You can also use the documentation [3].
Please note, that current release (0.1) is a pre-release, and the API might change a bit.

How does it work?
I've noticed, that when I kill (not close) google-chrome, it's able to restore my tabs after the crash. It means, that it has to constantly update some file saving information about the tabs. I was right - there is a binary file in your profile directory - Current Session - that stores that information.
Unfortunately, Current Session is a binary file, so I had to go through the chromium source code to figure out the file format.

Feedback
Feedback is always appreciated! Feel free to comment, report issues[4], or create pull requests [5].

Links
[1] https://github.com/loganek/chromietabs
[2] https://github.com/loganek/workertracker
[3] https://loganek.github.io/chromietabs/master//index.html
[4] https://github.com/loganek/chromietabs/issues
[5] https://github.com/loganek/chromietabs/pulls

Sunday, October 30, 2016

Automatic collection of instances of derived classes in C++

Currently I work on application which measures and registers time that I'm spending in particular application. Since I use various of operating systems (Windows at work, and Linux at home), application has to be available on multiple platforms.
Sometimes I don't want to be tracked (e.g. when the screen is locked), so I've defined collection of rules, when tracking should be suspended:
std::vector<std::shared_ptr<Rule>> rules;
rules.push_back(std::make_shared<Rule1>());
rules.push_back(std::make_shared<Rule2>());
rules.push_back(std::make_shared<Rule3>());
rules.push_back(std::make_shared<Rule4>());
However, some of the rules are OS-specific (e.g. Rule2 and Rule3 compile and work only in Windows, and Rule4 makes sense only when app is running on Linux). So what I did:
  • Conditionally enabled files rule2.cc, rule3.cc and rule4.cc in the build system
    if (WIN32)
      set (RULE_FILES rule2.cc rule3.cc)
    elseif (UNIX)
      set (RULE_FILES rule4.cc)
    endif (WIN32)
    
  • Conditionally pushed instances of Rule2, Rule3 and Rule4 to the std::vector.
    std::vector<std::shared_ptr<Rule>> rules;
    rules.push_back(std::make_shared<Rule1>());
    #ifdef _WIN32 // Windows specific rules
      rules.push_back(std::make_shared<Rule2>());
      rules.push_back(std::make_shared<Rule3>());
    #elif __linux__ // Linux specific rules
      rules.push_back(std::make_shared<Rule4>());
    #endif
    
Two steps - sounds redundant. Also, I don't really like using ifdefs in the code (code with ifdefs is more difficult to maintain, and it's less readable). So I've implemented very simple class(actually, two classes), which helps creating a collection of instances of inherited classes.
Here's a code:
#include <memory>
#include <typeindex>
#include <unordered_map>

template <typename Derived, typename Base>
struct Registrar
{
  template<typename ...Args>
  Registrar(Args&&... args)
  {
    Base::registry()[typeid(Derived)] =
      std::make_shared<Derived>(std::forward<Args>(args)...);
  }
};

template<typename T>
struct Registrable
{
  typedef std::unordered_map<std::type_index, std::shared_ptr<T>> collection_t;
  template<typename R>
  using Registrar = Registrar<R, T>;

  static collection_t & registry()
  {
    static collection_t collection;
    return collection;
  }
};

I use std::unordered_map, because I want to make sure that I have only one instance of each class in my collection. If you accept, or even want to have multiple instances of the same class in the collection, you can replace it with std::vector, or any other collection.
And that's how the code above can be used:

class Base : public Registrable<Base>
{
public:
  virtual void print_myself() = 0;
};

class WithParameter : public Base
{
  int x;

public:
  WithParameter(int x) : x(x) {}

  void print_myself() override
  {
    std::cout << "WithParameter(" << x << ")" << std::endl;
  }

  static Registrar<WithParameter> registrar;
};
WithParameter::Registrar<WithParameter> WithParameter::registrar(25);

class NoParameter : public Base
{
public:
  void print_myself() override
  {
    std::cout << "NoParameter" << std::endl;
  }

  static Registrar<NoParameter> registrar;
};
NoParameter::Registrar<NoParameter> NoParameter::registrar;

I've created Base class, and two inherited classes (WithParameter and NoParameter). Both derived classes have static registrar object, which automatically adds instances of the classes to the collection.
Unfortunately, constructor parameters (if any), have to be compile time constants. But in my case, it's not a problem.
Now, we can e.g. enumerate our collection:
for (auto x : Base::registry())
{
  x.second->print_myself();
}

Full code with example can be found on my github's gist [1].

Links
[1] https://gist.github.com/loganek/c0c8dfb30cd72a5a1a47b3701b61d3da

Saturday, July 16, 2016

Generic C++ GObject signals wrapper

Recently I've discovered, that connecting to signals in gstreamermm can be really inconvenient. The problem doesn't exist in the other mm libraries, because most of the classes and their signals are wrapped.
But GStreamer allows to create user-defined elements, so it's actually impossible to wrap everything in gstreamermm (for now library supports wrappers for gstreamer plugins core and base).

Currently, if you want to connect to a signal in gstreamermm, you have two options:
  1. Using pure C API:

  2. auto typefind = Gst::ElementFactory::create_element("typefind");
    
    g_signal_connect (typefind->gobj(), 
                      "have-type", 
                      G_CALLBACK(cb_typefind_havetype), 
                     (gpointer *)typefind->gobj());
    
    static void cb_typefind_havetype (GstTypeFindElement *typefind,
                                      guint               probability,
                                      GstCaps            *caps,
                                      gpointer            user_data)
    {
      // callback implementation
    }
    Well, it's not very bad. But... you have to use C structures in the callback instead of C++ wrappers.

  3. Using gstreamermm API
  4. As I mentioned, gstreamermm provide wrappers for core and base plugins, so some of the elements (and their signals) are already wrapped in the library:
    auto typefind = Gst::TypeFindElement::create();
    typefind->signal_have_type().connect(
        [] (guint probability, const Glib::RefPtr& caps)
        {
          // callback implementation
        });
    
However, many plugins are not wrapped (and they never will), so usually you need to either write wrapper for element which you wanted to use (and then, maintain this wrapper as well), or use pure C API.
Moreover, I'm going to remove plugin API in the next release [1], so user won't be able to use gstreamermm API even for the base and core plugins. I was wondering, if it would be possible to write generic wrapper for the GObject signals. So... there you are! The solution is not perfect yet, and I haven't tested it so much, but so far it works fine with few plugins and signals.

namespace Glib
{  
  template <typename T>
  static constexpr T wrap (T v, bool=true)
  {
    return v;
  }

  template <typename T>
  static constexpr T unwrap (T v, bool=true)
  {
    return v;
  }

  template<typename T>
  using unwrapped_t = decltype(unwrap(*((typename std::remove_reference<T>::type*)nullptr)));

  template<typename T>
  constexpr T return_helper()
  {
    typedef unwrapped_t<T> Ret;
    return Ret();
  }

  template<>
  constexpr void return_helper()
  {
    return void();
  }
}


template<typename>
class signal_callback;

template<typename Ret, typename ...T>
class signal_callback<Ret(T...)>
{
  template<typename ...Args>
  static auto callback(void* self, Args ...v)
  {
    using Glib::wrap;
    typedef sigc::slot< void, decltype(wrap(v))... > SlotType;

    void* data = std::get<sizeof...(Args)-1>(std::tuple<Args...>(v...));

    // Do not try to call a signal on a disassociated wrapper.
    if(dynamic_cast<Glib::Object*>(Glib::ObjectBase::_get_current_wrapper((GObject*) self)))
      {
	try
	  {
	    if(sigc::slot_base *const slot = Glib::SignalProxyNormal::data_to_slot(data))
	      {
		(*static_cast<SlotType*>(slot))(wrap(std::forward<Args>(v), true)...);
	      }
	  }
	catch(...)
	  {
	    Glib::exception_handlers_invoke();
	  }
      }

    return Glib::return_helper<Ret>();
  }

public:
  auto operator()(const std::string& signal_name, const Glib::RefPtr<Glib::Object>& obj)
  {
    using Glib::unwrap;
    static std::map<std::pair<GType, std::string>, Glib::SignalProxyInfo> signal_infos;

    auto key = std::make_pair(G_TYPE_FROM_INSTANCE (obj->gobj()), signal_name);
    if (signal_infos.find(key) == signal_infos.end())
      {
	signal_infos[key] = {
	  signal_name.c_str(),
	  (GCallback) &callback<Glib::unwrapped_t<T>..., void*>,
	  (GCallback) &callback<Glib::unwrapped_t<T>..., void*>
	};
      }

    return Glib::SignalProxy<Ret, T... >(obj.operator->(), &signal_infos[key]);
  }
};


auto typefind = Gst::ElementFactory::create_element("typefind"),
signal_callback<void(guint, const Glib::RefPtr<Gst::Caps>&)> signal_wrapper;

signal_wrapper("have-type", typefind).connect(
  [&ready, &cv] (guint probability, const Glib::RefPtr<Gst::Caps>& caps) {
    std::cout << "have-type: probability=" << probability << std::endl;
    Gst::Structure structure = caps->get_structure(0);
    const Glib::ustring mime_type = structure.get_name();
    std::cout << "have-type: mime_type=" << mime_type << std::endl;

    structure.foreach([] (const Glib::QueryQuark& id, const Glib::ValueBase& value) {
      const Glib::ustring str_id = id;
      gchar* str_value = g_strdup_value_contents(value.gobj());
      std::cout << "Structure field: id=" << str_id << ", value=" << str_value << std::endl;
      g_free(str_value);		      
      return true;
  });
});

Full source of the code can be found on the github [2].
As you see, you still have to know the type of the callback, but at least you can use gstreamermm C++ classes.
There is couple of things to do in this code, like getting last parameter from the list in more efficient way than through the tuple, etc.
I don't feel it is stable enough to integrate it with gstreamermm, but probably in the future I'll do that. Also, we could even internally use it in the glibmm to reduce amount of generated code.

Links
[1] https://bugzilla.gnome.org/show_bug.cgi?id=755395
[2] https://gist.github.com/loganek/7833089caff73ff2e8b1f076c8f7910e

Sunday, August 30, 2015

GUADEC 2015

At the beginning of this month I've spent a few great days in Goteborg, at GUADEC conference. That was my second GUADEC (first time I've participated last year, in Strasbourg).

There were a lot of interesting presentations, but the most I enjoyed all of the keynotes. As a Google Summer of Code student (I've worked for GStreamer project [1]), I was able to give a lightning talk about my project. It seemed to be interested to some people, so after that someone asked me to show him my project "in action". We were talking about possible improvements and new features.

In the evenings organizers prepared for attendees some event, so I could integrate with other GNOME people. Moreover, they organized for us (GSOC students and mentors) a dinner, so I could meet other students a little bit better, talk mostly about our summer projects, but also about differences in education at their universities  (people came from different part of world).

In conclusion, I enjoyed GUADEC, all presentations and evening events. I'd like to thank the GNOME Foundation for sponsoring my travel and my hostel. My participation probably wasn't be able without Foundation's help. Thanks a lot!


[1] http://www.cookandcommit.eu/2015/06/gstreamer-debugger-introduction.html

Monday, June 29, 2015

GStreamer Debugger - introduction

Hi everyone,
This year I've been accepted to Google Summer of Code :) Last year I worked on Banshee project [1], and this year I joined to GStreamer [2] team.
This summer I work on tool for GStreamer-based applications - GStreamer Debugger.

Goals
At the end of this summer, I'm going to provide you an application, which allows you to connect to your remote pipeline (obviously, lo interface can be used as well :)), watch pipeline graph (and its changes), spy selected queries, events, log messages, messages from bus and log messages, and even buffers. Application won't allow user modify pipeline and pipeline's state, but who knows - if it is useful feature, I implement it in the future.
GStreamer doesn't provide possibility to connect to pipeline, so I have to do it on my own.

Progress
June is a month, when I've exams on my university (fortunately, I've already passed all of them!), so I didn't spend as much time as I wanted on this project. Anyway, I accomplished a few milestones.
There's a list, what already has been done:
  • gst-trace [3] plugin, containing tcp server. For now, it sends GstEvents, GstMessages, and log messages to clients (todo: send GstBuffers, and GstQueries)
  • client application, which displays events and log messages (todo: display GstBuffers, GstQueries, GstMessages). I have a lot of ideas, how to improve client application, but I'm not sure whether I meet GSOC deadline, so I suppose, most of them will be implement after Google's program. 
  • protocol - I used Google Protobuf library [4]. In general, I've defined most of protocol's structures, I just make minor improvements, when I need it.
Below you can find a few screenshoots of client application. Full code can be found on my github account ([5], [6]).




Links
[1] http://www.cookandcommit.eu/2014/04/google-summer-of-code-2014-with-gnome.html
[2] http://gstreamer.freedesktop.org/
[3] https://bugzilla.gnome.org/show_bug.cgi?id=733187
[4] https://developers.google.com/protocol-buffers/
[5] https://github.com/loganek/gstreamer
[6] https://github.com/loganek/gst-debugger

Friday, January 2, 2015

Beefsteak with greek salad

Ingredients:

  • 2x200g beefsteak(of ~2.5 cm thickness)
  • pepper 
  • 1/2 head of  iceberg lettuce
  • 4 tablespoons good-quality Greek extra virgin olive oil
  • 3/2 teaspoon dried oregano
  • 2 ripe tomatoes
  • 1 cucumber
  • 1 medium red onion
  • 1 jar of black olives, stoned
  • a few basil leaves
  • 200g block feta cheese
  • sea salt 

Method:

  • Greek salad
    1. Combine oil, oregano and pinch of salt.
    2. Rinse lettuce, dry it, cut it into bite and put into a salad bowl.
    3. Salt it down.
    4. Cut tomatoes into chunks.
    5. Peel a cucumber and slice.
    6. Peel an onion, and slice.
    7. Add tomatoes, cucumber, feta cheese, basil, olives and onion to a salad bowl and season with salt.
    8. Combine olive oil and oregano, and pour over the salad.
  • Beefsteak
    1. Get warm cast-iron frying pan.
    2. Oil steaks and season with salt and pepper
    3. Cook steak for 2 minutes, then turn it and cook further 2 minutes. At last, turn it again, and cook for 5-6 minutes.
  • Serve red wine (I've used medium-dry one).

Results




 Bon appetit!

Wednesday, November 19, 2014

"Learn IT, Girl!" mentorship program just started!

This week is official beginning of "Learn IT, Girl!" program [1]. It's international program for woman, started by a few girls from Poland and Romania, invented at the Google Anita Borg Scholarship [2] retreat in 2014. A main aim is to learn by women particular programming language. Every scholar is guided by her own mentor, who helps her. Girls learn language by doing selected earlier project (which must be open-source project ofc).

I take a part in this program as a mentor, and my my mentee is Corina Teodorescu. She's from Romania and studies Marketing. Corina has decided to learn C#, and she's going to write an mobile app for phones with Android OS. Because of a lot really great ideas, she doesn't finally choose her project. She's choosing between RSS reader and application for downloading/uploading/hash-tagging images - she didn't make a decision, but I think, both are quite useful, and Corina can learn a lot by making one of them(what's the most important during this program :)

Program takes 3 months, and I believe, that would be great time for Corina and for me as well. I hope, Corina will learn a lot, and I'm going to improve my teaching skills too. We will have fun for sure :)

 Wish me and Corina luck!

Learn IT, Girl!


Links
[1] https://sites.google.com/site/learnitgirl/
[2] http://www.google.com/anitaborg/