| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/window_controller_list.h" | 5 #include "chrome/browser/extensions/window_controller_list.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "chrome/browser/extensions/extension_function.h" | 9 #include "chrome/browser/extensions/extension_function.h" |
| 10 #include "chrome/browser/extensions/window_controller_list_observer.h" |
| 10 #include "chrome/browser/sessions/session_id.h" | 11 #include "chrome/browser/sessions/session_id.h" |
| 11 #include "chrome/browser/ui/base_window.h" | 12 #include "chrome/browser/ui/base_window.h" |
| 12 | 13 |
| 13 namespace extensions { | 14 namespace extensions { |
| 14 | 15 |
| 15 /////////////////////////////////////////////////////////////////////////////// | 16 /////////////////////////////////////////////////////////////////////////////// |
| 16 // WindowControllerList | 17 // WindowControllerList |
| 17 | 18 |
| 18 // static | 19 // static |
| 19 WindowControllerList* WindowControllerList::GetInstance() { | 20 WindowControllerList* WindowControllerList::GetInstance() { |
| 20 return Singleton<WindowControllerList>::get(); | 21 return Singleton<WindowControllerList>::get(); |
| 21 } | 22 } |
| 22 | 23 |
| 23 WindowControllerList::WindowControllerList() { | 24 WindowControllerList::WindowControllerList() { |
| 24 } | 25 } |
| 25 | 26 |
| 26 WindowControllerList::~WindowControllerList() { | 27 WindowControllerList::~WindowControllerList() { |
| 27 } | 28 } |
| 28 | 29 |
| 29 void WindowControllerList::AddExtensionWindow(WindowController* window) { | 30 void WindowControllerList::AddExtensionWindow(WindowController* window) { |
| 30 windows_.push_back(window); | 31 windows_.push_back(window); |
| 32 FOR_EACH_OBSERVER(WindowControllerListObserver, observers_, |
| 33 OnWindowControllerAdded(window)); |
| 31 } | 34 } |
| 32 | 35 |
| 33 void WindowControllerList::RemoveExtensionWindow(WindowController* window) { | 36 void WindowControllerList::RemoveExtensionWindow(WindowController* window) { |
| 34 ControllerList::iterator iter = std::find( | 37 ControllerList::iterator iter = std::find( |
| 35 windows_.begin(), windows_.end(), window); | 38 windows_.begin(), windows_.end(), window); |
| 36 if (iter != windows_.end()) | 39 if (iter != windows_.end()) { |
| 37 windows_.erase(iter); | 40 windows_.erase(iter); |
| 41 FOR_EACH_OBSERVER(WindowControllerListObserver, observers_, |
| 42 OnWindowControllerRemoved(window)); |
| 43 } |
| 44 } |
| 45 |
| 46 void WindowControllerList::AddObserver(WindowControllerListObserver* observer) { |
| 47 observers_.AddObserver(observer); |
| 48 } |
| 49 |
| 50 void WindowControllerList::RemoveObserver( |
| 51 WindowControllerListObserver* observer) { |
| 52 observers_.RemoveObserver(observer); |
| 38 } | 53 } |
| 39 | 54 |
| 40 WindowController* WindowControllerList::FindWindowForFunctionById( | 55 WindowController* WindowControllerList::FindWindowForFunctionById( |
| 41 const UIThreadExtensionFunction* function, | 56 const UIThreadExtensionFunction* function, |
| 42 int id) const { | 57 int id) const { |
| 43 for (ControllerList::const_iterator iter = windows().begin(); | 58 for (ControllerList::const_iterator iter = windows().begin(); |
| 44 iter != windows().end(); ++iter) { | 59 iter != windows().end(); ++iter) { |
| 45 if (function->CanOperateOnWindow(*iter) && (*iter)->GetWindowId() == id) | 60 if (function->CanOperateOnWindow(*iter) && (*iter)->GetWindowId() == id) |
| 46 return *iter; | 61 return *iter; |
| 47 } | 62 } |
| 48 return NULL; | 63 return NULL; |
| 49 } | 64 } |
| 50 | 65 |
| 51 WindowController* WindowControllerList::CurrentWindowForFunction( | 66 WindowController* WindowControllerList::CurrentWindowForFunction( |
| 52 const UIThreadExtensionFunction* function) const { | 67 const UIThreadExtensionFunction* function) const { |
| 53 WindowController* result = NULL; | 68 WindowController* result = NULL; |
| 54 // Returns either the focused window (if any), or the last window in the list. | 69 // Returns either the focused window (if any), or the last window in the list. |
| 55 for (ControllerList::const_iterator iter = windows().begin(); | 70 for (ControllerList::const_iterator iter = windows().begin(); |
| 56 iter != windows().end(); ++iter) { | 71 iter != windows().end(); ++iter) { |
| 57 if (function->CanOperateOnWindow(*iter)) { | 72 if (function->CanOperateOnWindow(*iter)) { |
| 58 result = *iter; | 73 result = *iter; |
| 59 if (result->window()->IsActive()) | 74 if (result->window()->IsActive()) |
| 60 break; // use focused window | 75 break; // use focused window |
| 61 } | 76 } |
| 62 } | 77 } |
| 63 return result; | 78 return result; |
| 64 } | 79 } |
| 65 | 80 |
| 66 } // namespace extensions | 81 } // namespace extensions |
| OLD | NEW |