11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Copyright 2014 The Chromium Authors. All rights reserved.
21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Use of this source code is governed by a BSD-style license that can be
31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// found in the LICENSE file.
41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/apps/app_window_registry_util.h"
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include <vector>
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/browser_process.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/profiles/profile.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/profiles/profile_manager.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window_registry.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/native_app_window.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciusing extensions::AppWindow;
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciusing extensions::AppWindowRegistry;
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccitypedef AppWindowRegistry::AppWindowList AppWindowList;
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccitypedef AppWindowRegistry::Factory Factory;
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// static
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciAppWindow* AppWindowRegistryUtil::GetAppWindowForNativeWindowAnyProfile(
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    gfx::NativeWindow window) {
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::vector<Profile*> profiles =
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      g_browser_process->profile_manager()->GetLoadedProfiles();
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  for (std::vector<Profile*>::const_iterator i = profiles.begin();
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci       i != profiles.end();
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci       ++i) {
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    AppWindowRegistry* registry =
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        Factory::GetForBrowserContext(*i, false /* create */);
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (!registry)
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      continue;
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    AppWindow* app_window = registry->GetAppWindowForNativeWindow(window);
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (app_window)
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return app_window;
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return NULL;
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// static
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool AppWindowRegistryUtil::IsAppWindowRegisteredInAnyProfile(
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    int window_type_mask) {
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::vector<Profile*> profiles =
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      g_browser_process->profile_manager()->GetLoadedProfiles();
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  for (std::vector<Profile*>::const_iterator i = profiles.begin();
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci       i != profiles.end();
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci       ++i) {
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    AppWindowRegistry* registry =
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        Factory::GetForBrowserContext(*i, false /* create */);
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (!registry)
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      continue;
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const AppWindowList& app_windows = registry->app_windows();
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (app_windows.empty())
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      continue;
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (window_type_mask == 0)
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return true;
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    for (AppWindowList::const_iterator j = app_windows.begin();
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci         j != app_windows.end(); ++j) {
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      if ((*j)->window_type() & window_type_mask)
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        return true;
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return false;
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// static
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid AppWindowRegistryUtil::CloseAllAppWindows() {
751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::vector<Profile*> profiles =
761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      g_browser_process->profile_manager()->GetLoadedProfiles();
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  for (std::vector<Profile*>::const_iterator i = profiles.begin();
781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci       i != profiles.end();
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci       ++i) {
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    AppWindowRegistry* registry =
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        Factory::GetForBrowserContext(*i, false /* create */);
821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (!registry)
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      continue;
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    while (!registry->app_windows().empty())
861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      registry->app_windows().front()->GetBaseWindow()->Close();
871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
89