Revert of Don't install OEM default apps for enterprise users (patchset #1 id:1 of...
[chromium-blink-merge.git] / chrome / browser / extensions / window_controller_list.cc
blob0eadbea36d529a82bfe7af7410a431b125e6a5b5
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/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
19 // static
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()) {
40 windows_.erase(iter);
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)
59 return *iter;
61 return NULL;
64 WindowController* WindowControllerList::FindWindowForFunctionById(
65 const ChromeExtensionFunctionDetails& function_details,
66 int id) const {
67 WindowController* controller = FindWindowById(id);
68 if (controller && function_details.CanOperateOnWindow(controller))
69 return controller;
70 return NULL;
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)) {
80 result = *iter;
81 if (result->window()->IsActive())
82 break; // use focused window
85 return result;
88 } // namespace extensions