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 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
,
60 WindowController::GetDefaultWindowFilter());
63 WindowController
* WindowControllerList::FindWindowByIdWithFilter(
65 WindowController::TypeFilter filter
) const {
66 for (ControllerList::const_iterator iter
= windows().begin();
67 iter
!= windows().end(); ++iter
) {
68 if ((*iter
)->GetWindowId() == id
&& (*iter
)->MatchesFilter(filter
))
74 WindowController
* WindowControllerList::FindWindowForFunctionById(
75 const UIThreadExtensionFunction
* function
,
77 return FindWindowForFunctionByIdWithFilter(
78 function
, id
, WindowController::GetDefaultWindowFilter());
81 WindowController
* WindowControllerList::FindWindowForFunctionByIdWithFilter(
82 const UIThreadExtensionFunction
* function
,
84 WindowController::TypeFilter filter
) const {
85 WindowController
* controller
= FindWindowByIdWithFilter(id
, filter
);
87 windows_util::CanOperateOnWindow(function
, controller
, filter
))
92 WindowController
* WindowControllerList::CurrentWindowForFunction(
93 const UIThreadExtensionFunction
* function
) const {
94 return CurrentWindowForFunctionWithFilter(
95 function
, WindowController::GetDefaultWindowFilter());
98 WindowController
* WindowControllerList::CurrentWindowForFunctionWithFilter(
99 const UIThreadExtensionFunction
* function
,
100 WindowController::TypeFilter filter
) const {
101 WindowController
* result
= nullptr;
102 // Returns either the focused window (if any), or the last window in the list.
103 for (ControllerList::const_iterator iter
= windows().begin();
104 iter
!= windows().end(); ++iter
) {
105 if (windows_util::CanOperateOnWindow(function
, *iter
, filter
)) {
107 if (result
->window()->IsActive())
108 break; // use focused window
114 } // namespace extensions