• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "chrome/browser/extensions/window_controller_list.h"
6 
7 #include <algorithm>
8 
9 #include "chrome/browser/extensions/chrome_extension_function.h"
10 #include "chrome/browser/extensions/window_controller_list_observer.h"
11 #include "chrome/browser/sessions/session_id.h"
12 #include "ui/base/base_window.h"
13 
14 namespace extensions {
15 
16 ///////////////////////////////////////////////////////////////////////////////
17 // WindowControllerList
18 
19 // static
GetInstance()20 WindowControllerList* WindowControllerList::GetInstance() {
21   return Singleton<WindowControllerList>::get();
22 }
23 
WindowControllerList()24 WindowControllerList::WindowControllerList() {
25 }
26 
~WindowControllerList()27 WindowControllerList::~WindowControllerList() {
28 }
29 
AddExtensionWindow(WindowController * window)30 void WindowControllerList::AddExtensionWindow(WindowController* window) {
31   windows_.push_back(window);
32   FOR_EACH_OBSERVER(WindowControllerListObserver, observers_,
33                     OnWindowControllerAdded(window));
34 }
35 
RemoveExtensionWindow(WindowController * window)36 void WindowControllerList::RemoveExtensionWindow(WindowController* window) {
37   ControllerList::iterator iter = std::find(
38       windows_.begin(), windows_.end(), window);
39   if (iter != windows_.end()) {
40     windows_.erase(iter);
41     FOR_EACH_OBSERVER(WindowControllerListObserver, observers_,
42                       OnWindowControllerRemoved(window));
43   }
44 }
45 
AddObserver(WindowControllerListObserver * observer)46 void WindowControllerList::AddObserver(WindowControllerListObserver* observer) {
47   observers_.AddObserver(observer);
48 }
49 
RemoveObserver(WindowControllerListObserver * observer)50 void WindowControllerList::RemoveObserver(
51     WindowControllerListObserver* observer) {
52   observers_.RemoveObserver(observer);
53 }
54 
FindWindowById(int id) const55 WindowController* WindowControllerList::FindWindowById(int id) const {
56   for (ControllerList::const_iterator iter = windows().begin();
57        iter != windows().end(); ++iter) {
58     if ((*iter)->GetWindowId() == id)
59       return *iter;
60   }
61   return NULL;
62 }
63 
FindWindowForFunctionById(const ChromeAsyncExtensionFunction * function,int id) const64 WindowController* WindowControllerList::FindWindowForFunctionById(
65     const ChromeAsyncExtensionFunction* function,
66     int id) const {
67   WindowController* controller = FindWindowById(id);
68   if (controller && function->CanOperateOnWindow(controller))
69     return controller;
70   return NULL;
71 }
72 
CurrentWindowForFunction(const ChromeAsyncExtensionFunction * function) const73 WindowController* WindowControllerList::CurrentWindowForFunction(
74     const ChromeAsyncExtensionFunction* function) const {
75   WindowController* result = NULL;
76   // Returns either the focused window (if any), or the last window in the list.
77   for (ControllerList::const_iterator iter = windows().begin();
78        iter != windows().end(); ++iter) {
79     if (function->CanOperateOnWindow(*iter)) {
80       result = *iter;
81       if (result->window()->IsActive())
82         break;  // use focused window
83     }
84   }
85   return result;
86 }
87 
88 }  // namespace extensions
89