15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "ui/wm/public/dispatcher_client.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/callback.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/aura/window_property.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_WINDOW_PROPERTY_TYPE(aura::client::DispatcherClient*);
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace aura {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace client {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)DispatcherRunLoop::DispatcherRunLoop(DispatcherClient* client,
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                     base::MessagePumpDispatcher* dispatcher) {
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  client->PrepareNestedLoopClosures(dispatcher, &run_closure_, &quit_closure_);
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)DispatcherRunLoop::~DispatcherRunLoop() {
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void DispatcherRunLoop::Run() {
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  run_closure_.Run();
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)base::Closure DispatcherRunLoop::QuitClosure() {
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return quit_closure_;
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void DispatcherRunLoop::Quit() {
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  quit_closure_.Run();
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DEFINE_LOCAL_WINDOW_PROPERTY_KEY(DispatcherClient*, kDispatcherClientKey, NULL);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void SetDispatcherClient(Window* root_window, DispatcherClient* client) {
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_EQ(root_window->GetRootWindow(), root_window);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  root_window->SetProperty(kDispatcherClientKey, client);
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)DispatcherClient* GetDispatcherClient(Window* root_window) {
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (root_window)
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    DCHECK_EQ(root_window->GetRootWindow(), root_window);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return root_window ? root_window->GetProperty(kDispatcherClientKey) : NULL;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace client
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace aura
53