Home | History | Annotate | Download | only in tabs
      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 CHROME_BROWSER_EXTENSIONS_API_TABS_WINDOWS_EVENT_ROUTER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_TABS_WINDOWS_EVENT_ROUTER_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "chrome/browser/extensions/window_controller_list_observer.h"
     11 #include "content/public/browser/notification_observer.h"
     12 #include "content/public/browser/notification_registrar.h"
     13 #if defined(TOOLKIT_VIEWS)
     14 #include "ui/views/focus/widget_focus_manager.h"
     15 #elif defined(TOOLKIT_GTK)
     16 #include "ui/base/x/active_window_watcher_x_observer.h"
     17 #endif
     18 
     19 class Profile;
     20 
     21 namespace base {
     22 class ListValue;
     23 }
     24 
     25 namespace extensions {
     26 
     27 // The WindowsEventRouter sends chrome.windows.* events to listeners
     28 // inside extension process renderers. The router listens to *all* events,
     29 // but will only route eventes within a profile to extension processes in the
     30 // same profile.
     31 class WindowsEventRouter : public WindowControllerListObserver,
     32 #if defined(TOOLKIT_VIEWS)
     33                           public views::WidgetFocusChangeListener,
     34 #elif defined(TOOLKIT_GTK)
     35                           public ui::ActiveWindowWatcherXObserver,
     36 #endif
     37                           public content::NotificationObserver {
     38  public:
     39   explicit WindowsEventRouter(Profile* profile);
     40   virtual ~WindowsEventRouter();
     41 
     42   // WindowControllerListObserver methods:
     43   virtual void OnWindowControllerAdded(
     44       WindowController* window_controller) OVERRIDE;
     45   virtual void OnWindowControllerRemoved(
     46       WindowController* window) OVERRIDE;
     47 
     48 #if defined(TOOLKIT_VIEWS)
     49   virtual void OnNativeFocusChange(gfx::NativeView focused_before,
     50                                    gfx::NativeView focused_now) OVERRIDE;
     51 #elif defined(TOOLKIT_GTK)
     52   virtual void ActiveWindowChanged(GdkWindow* active_window) OVERRIDE;
     53 #endif
     54 
     55   // content::NotificationObserver.
     56   virtual void Observe(int type,
     57                        const content::NotificationSource& source,
     58                        const content::NotificationDetails& details) OVERRIDE;
     59 
     60   // |window_controller| is NULL to indicate a focused window has lost focus.
     61   void OnActiveWindowChanged(WindowController* window_controller);
     62 
     63  private:
     64   void DispatchEvent(const char* event_name,
     65                      Profile* profile,
     66                      scoped_ptr<base::ListValue> args);
     67 
     68   content::NotificationRegistrar registrar_;
     69 
     70   // The main profile that owns this event router.
     71   Profile* profile_;
     72 
     73   // The profile the currently focused window belongs to; either the main or
     74   // incognito profile or NULL (none of the above). We remember this in order
     75   // to correctly handle focus changes between non-OTR and OTR windows.
     76   Profile* focused_profile_;
     77 
     78   // The currently focused window. We keep this so as to avoid sending multiple
     79   // windows.onFocusChanged events with the same windowId.
     80   int focused_window_id_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(WindowsEventRouter);
     83 };
     84 
     85 }  // namespace extensions
     86 
     87 #endif  // CHROME_BROWSER_EXTENSIONS_API_TABS_WINDOWS_EVENT_ROUTER_H_
     88