Home | History | Annotate | Download | only in x
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef UI_GFX_X_X11_ATOM_CACHE_H_
      6 #define UI_GFX_X_X11_ATOM_CACHE_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "ui/gfx/gfx_export.h"
     10 
     11 #include <map>
     12 #include <string>
     13 
     14 #include <X11/Xlib.h>
     15 
     16 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class.
     17 #undef RootWindow
     18 
     19 namespace ui {
     20 
     21 // Pre-caches all Atoms on first use to minimize roundtrips to the X11
     22 // server. By default, GetAtom() will CHECK() that atoms accessed through
     23 // GetAtom() were passed to the constructor, but this behaviour can be changed
     24 // with allow_uncached_atoms().
     25 class GFX_EXPORT X11AtomCache {
     26  public:
     27   // Preinterns the NULL terminated list of string |to_cache_ on |xdisplay|.
     28   X11AtomCache(Display* xdisplay, const char** to_cache);
     29   ~X11AtomCache();
     30 
     31   // Returns the pre-interned Atom without having to go to the x server.
     32   ::Atom GetAtom(const char*) const;
     33 
     34   // When an Atom isn't in the list of items we've cached, we should look it
     35   // up, cache it locally, and then return the result.
     36   void allow_uncached_atoms() { uncached_atoms_allowed_ = true; }
     37 
     38  private:
     39   Display* xdisplay_;
     40 
     41   bool uncached_atoms_allowed_;
     42 
     43   mutable std::map<std::string, ::Atom> cached_atoms_;
     44 
     45   DISALLOW_COPY_AND_ASSIGN(X11AtomCache);
     46 };
     47 
     48 }  // namespace ui
     49 
     50 #endif  // UI_GFX_X_X11_ATOM_CACHE_H_
     51