Home | History | Annotate | Download | only in impl
      1 // Copyright 2015 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.
      4 
      5 package org.chromium.mojo.system.impl;
      6 
      7 import org.chromium.base.annotations.CalledByNative;
      8 import org.chromium.base.annotations.JNINamespace;
      9 import org.chromium.mojo.system.RunLoop;
     10 
     11 /**
     12  * Implementation of {@link RunLoop} suitable for the base:: message loop implementation.
     13  */
     14 @JNINamespace("mojo::android")
     15 class BaseRunLoop implements RunLoop {
     16     /**
     17      * Pointer to the C run loop.
     18      */
     19     private long mRunLoopID;
     20     private final CoreImpl mCore;
     21 
     22     BaseRunLoop(CoreImpl core) {
     23         this.mCore = core;
     24         this.mRunLoopID = nativeCreateBaseRunLoop();
     25     }
     26 
     27     @Override
     28     public void run() {
     29         assert mRunLoopID != 0 : "The run loop cannot run once closed";
     30         nativeRun(mRunLoopID);
     31     }
     32 
     33     @Override
     34     public void runUntilIdle() {
     35         assert mRunLoopID != 0 : "The run loop cannot run once closed";
     36         nativeRunUntilIdle(mRunLoopID);
     37     }
     38 
     39     @Override
     40     public void quit() {
     41         assert mRunLoopID != 0 : "The run loop cannot be quitted run once closed";
     42         nativeQuit(mRunLoopID);
     43     }
     44 
     45     @Override
     46     public void postDelayedTask(Runnable runnable, long delay) {
     47         assert mRunLoopID != 0 : "The run loop cannot run tasks once closed";
     48         nativePostDelayedTask(mRunLoopID, runnable, delay);
     49     }
     50 
     51     @Override
     52     public void close() {
     53         if (mRunLoopID == 0) {
     54             return;
     55         }
     56         // We don't want to de-register a different run loop!
     57         assert mCore.getCurrentRunLoop() == this : "Only the current run loop can be closed";
     58         mCore.clearCurrentRunLoop();
     59         nativeDeleteMessageLoop(mRunLoopID);
     60         mRunLoopID = 0;
     61     }
     62 
     63     @CalledByNative
     64     private static void runRunnable(Runnable runnable) {
     65         runnable.run();
     66     }
     67 
     68     private native long nativeCreateBaseRunLoop();
     69     private native void nativeRun(long runLoopID);
     70     private native void nativeRunUntilIdle(long runLoopID);
     71     private native void nativeQuit(long runLoopID);
     72     private native void nativePostDelayedTask(long runLoopID, Runnable runnable, long delay);
     73     private native void nativeDeleteMessageLoop(long runLoopID);
     74 }
     75