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.
5 #include "chrome/browser/extensions/window_controller_list.h"
9 #include "chrome/browser/extensions/api/tabs/windows_util.h"
10 #include "chrome/browser/extensions/chrome_extension_function_details.h"
11 #include "chrome/browser/extensions/window_controller_list_observer.h"
12 #include "components/sessions/session_id.h"
13 #include "extensions/browser/extension_function.h"
14 #include "ui/base/base_window.h"
16 namespace extensions
{
18 ///////////////////////////////////////////////////////////////////////////////
19 // WindowControllerList
22 WindowControllerList
* WindowControllerList::GetInstance() {
23 return Singleton
<WindowControllerList
>::get();
26 WindowControllerList::WindowControllerList() {
29 WindowControllerList::~WindowControllerList() {
32 void WindowControllerList::AddExtensionWindow(WindowController
* window
) {
33 windows_
.push_back(window
);
34 FOR_EACH_OBSERVER(WindowControllerListObserver
, observers_
,
35 OnWindowControllerAdded(window
));
38 void WindowControllerList::RemoveExtensionWindow(WindowController
* window
) {
39 ControllerList::iterator iter
= std::find(
40 windows_
.begin(), windows_
.end(), window
);
41 if (iter
!= windows_
.end()) {
43 FOR_EACH_OBSERVER(WindowControllerListObserver
, observers_
,
44 OnWindowControllerRemoved(window
));
48 void WindowControllerList::AddObserver(WindowControllerListObserver
* observer
) {
49 observers_
.AddObserver(observer
);
52 void WindowControllerList::RemoveObserver(
53 WindowControllerListObserver
* observer
) {
54 observers_
.RemoveObserver(observer
);
57 WindowController
* WindowControllerList::FindWindowById(int id
) const {
58 for (ControllerList::const_iterator iter
= windows().begin();
59 iter
!= windows().end(); ++iter
) {
60 if ((*iter
)->GetWindowId() == id
)
66 WindowController
* WindowControllerList::FindWindowForFunctionById(
67 const UIThreadExtensionFunction
* function
,
69 WindowController
* controller
= FindWindowById(id
);
70 if (controller
&& windows_util::CanOperateOnWindow(function
, controller
))
75 WindowController
* WindowControllerList::CurrentWindowForFunction(
76 const UIThreadExtensionFunction
* function
) const {
77 WindowController
* result
= NULL
;
78 // Returns either the focused window (if any), or the last window in the list.
79 for (ControllerList::const_iterator iter
= windows().begin();
80 iter
!= windows().end(); ++iter
) {
81 if (windows_util::CanOperateOnWindow(function
, *iter
)) {
83 if (result
->window()->IsActive())
84 break; // use focused window
90 } // namespace extensions