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/chrome_extension_function_details.h"
10 #include "chrome/browser/extensions/window_controller_list_observer.h"
11 #include "components/sessions/session_id.h"
12 #include "ui/base/base_window.h"
14 namespace extensions
{
16 ///////////////////////////////////////////////////////////////////////////////
17 // WindowControllerList
20 WindowControllerList
* WindowControllerList::GetInstance() {
21 return Singleton
<WindowControllerList
>::get();
24 WindowControllerList::WindowControllerList() {
27 WindowControllerList::~WindowControllerList() {
30 void WindowControllerList::AddExtensionWindow(WindowController
* window
) {
31 windows_
.push_back(window
);
32 FOR_EACH_OBSERVER(WindowControllerListObserver
, observers_
,
33 OnWindowControllerAdded(window
));
36 void WindowControllerList::RemoveExtensionWindow(WindowController
* window
) {
37 ControllerList::iterator iter
= std::find(
38 windows_
.begin(), windows_
.end(), window
);
39 if (iter
!= windows_
.end()) {
41 FOR_EACH_OBSERVER(WindowControllerListObserver
, observers_
,
42 OnWindowControllerRemoved(window
));
46 void WindowControllerList::AddObserver(WindowControllerListObserver
* observer
) {
47 observers_
.AddObserver(observer
);
50 void WindowControllerList::RemoveObserver(
51 WindowControllerListObserver
* observer
) {
52 observers_
.RemoveObserver(observer
);
55 WindowController
* WindowControllerList::FindWindowById(int id
) const {
56 for (ControllerList::const_iterator iter
= windows().begin();
57 iter
!= windows().end(); ++iter
) {
58 if ((*iter
)->GetWindowId() == id
)
64 WindowController
* WindowControllerList::FindWindowForFunctionById(
65 const ChromeExtensionFunctionDetails
& function_details
,
67 WindowController
* controller
= FindWindowById(id
);
68 if (controller
&& function_details
.CanOperateOnWindow(controller
))
73 WindowController
* WindowControllerList::CurrentWindowForFunction(
74 const ChromeExtensionFunctionDetails
& function_details
) 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_details
.CanOperateOnWindow(*iter
)) {
81 if (result
->window()->IsActive())
82 break; // use focused window
88 } // namespace extensions