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 "chrome/common/extensions/api/windows.h"
13 #include "components/sessions/session_id.h"
14 #include "extensions/browser/extension_function.h"
15 #include "ui/base/base_window.h"
17 namespace extensions
{
19 ///////////////////////////////////////////////////////////////////////////////
20 // WindowControllerList
23 WindowControllerList
* WindowControllerList::GetInstance() {
24 return base::Singleton
<WindowControllerList
>::get();
27 WindowControllerList::WindowControllerList() {
30 WindowControllerList::~WindowControllerList() {
33 void WindowControllerList::AddExtensionWindow(WindowController
* window
) {
34 windows_
.push_back(window
);
35 FOR_EACH_OBSERVER(WindowControllerListObserver
, observers_
,
36 OnWindowControllerAdded(window
));
39 void WindowControllerList::RemoveExtensionWindow(WindowController
* window
) {
40 ControllerList::iterator iter
= std::find(
41 windows_
.begin(), windows_
.end(), window
);
42 if (iter
!= windows_
.end()) {
44 FOR_EACH_OBSERVER(WindowControllerListObserver
, observers_
,
45 OnWindowControllerRemoved(window
));
49 void WindowControllerList::AddObserver(WindowControllerListObserver
* observer
) {
50 observers_
.AddObserver(observer
);
53 void WindowControllerList::RemoveObserver(
54 WindowControllerListObserver
* observer
) {
55 observers_
.RemoveObserver(observer
);
58 WindowController
* WindowControllerList::FindWindowById(int id
) const {
59 return FindWindowByIdWithFilter(id
, WindowController::GetAllWindowFilter());
62 WindowController
* WindowControllerList::FindWindowByIdWithFilter(
64 WindowController::TypeFilter filter
) const {
65 for (ControllerList::const_iterator iter
= windows().begin();
66 iter
!= windows().end(); ++iter
) {
67 if ((*iter
)->GetWindowId() == id
&& (*iter
)->MatchesFilter(filter
))
73 WindowController
* WindowControllerList::FindWindowForFunctionByIdWithFilter(
74 const UIThreadExtensionFunction
* function
,
76 WindowController::TypeFilter filter
) const {
77 for (ControllerList::const_iterator iter
= windows().begin();
78 iter
!= windows().end(); ++iter
) {
79 if ((*iter
)->GetWindowId() == id
) {
80 if (windows_util::CanOperateOnWindow(function
, *iter
, filter
))
88 WindowController
* WindowControllerList::CurrentWindowForFunction(
89 const UIThreadExtensionFunction
* function
) const {
90 return CurrentWindowForFunctionWithFilter(function
,
91 WindowController::kNoWindowFilter
);
94 WindowController
* WindowControllerList::CurrentWindowForFunctionWithFilter(
95 const UIThreadExtensionFunction
* function
,
96 WindowController::TypeFilter filter
) const {
97 WindowController
* result
= nullptr;
98 // Returns either the focused window (if any), or the last window in the list.
99 for (ControllerList::const_iterator iter
= windows().begin();
100 iter
!= windows().end(); ++iter
) {
101 if (windows_util::CanOperateOnWindow(function
, *iter
, filter
)) {
103 if (result
->window()->IsActive())
104 break; // use focused window
110 } // namespace extensions