Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / extensions / window_controller_list.cc
blobc12646b5b41c1fb3ce3fc60f4a0d28db6617ca2f
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"
7 #include <algorithm>
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
22 // static
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()) {
43 windows_.erase(iter);
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(
64 int id,
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))
69 return *iter;
71 return nullptr;
74 WindowController* WindowControllerList::FindWindowForFunctionById(
75 const UIThreadExtensionFunction* function,
76 int id) const {
77 return FindWindowForFunctionByIdWithFilter(
78 function, id, WindowController::GetDefaultWindowFilter());
81 WindowController* WindowControllerList::FindWindowForFunctionByIdWithFilter(
82 const UIThreadExtensionFunction* function,
83 int id,
84 WindowController::TypeFilter filter) const {
85 WindowController* controller = FindWindowByIdWithFilter(id, filter);
86 if (controller &&
87 windows_util::CanOperateOnWindow(function, controller, filter))
88 return controller;
89 return nullptr;
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)) {
106 result = *iter;
107 if (result->window()->IsActive())
108 break; // use focused window
111 return result;
114 } // namespace extensions