// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.platform;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.eventbus.Subscribe;
import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent;
import com.google.devtools.build.lib.buildtool.buildevent.BuildStartingEvent;
import com.google.devtools.build.lib.runtime.BlazeModule;
import com.google.devtools.build.lib.runtime.CommandEnvironment;
import com.google.devtools.build.lib.util.AbruptExitException;

/** Prevents the computer from going to sleep while a Bazel command is running. */
public final class SleepPreventionModule extends BlazeModule {

  /** Methods for dealing with sleep prevention on local hardware. */
  @VisibleForTesting
  public static final class SleepPrevention extends JniLoader {
    /**
     * Push a request to disable automatic sleep for hardware. Useful for making sure computers
     * don't go to sleep during long builds. Must be matched with a {@link #popDisableSleep} call.
     *
     * @return 0 on success, -1 if sleep is not supported.
     */
    public static native int pushDisableSleep();

    /**
     * Pop a request to disable automatic sleep for hardware. Useful for making sure computers don't
     * go to sleep during long builds. Must be matched with a previous {@link #pushDisableSleep}
     * call.
     *
     * @return 0 on success, -1 if sleep is not supported.
     */
    public static native int popDisableSleep();
  }

  @Override
  public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
    // Only register this component if JNI is available.
    if (!"0".equals(System.getProperty("io.bazel.EnableJni"))) {
      env.getEventBus().register(this);
    }
  }

  @Subscribe
  public void buildStarting(BuildStartingEvent event) {
    SleepPrevention.pushDisableSleep();
  }

  @Subscribe
  public void buildComplete(BuildCompleteEvent event) {
    SleepPrevention.popDisableSleep();
  }
}

@@ -718,209 +718,8 @@
 ng;%0A
-import com.google.common.eventbus.Subscribe;%0Aimport com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent;%0Aimport com.google.devtools.build.lib.buildtool.buildevent.BuildStartingEvent;%0A
 impo
@@ -1882,164 +1882,100 @@
 %0A%0A  
-@Override%0A  public void beforeCommand(CommandEnvironment env) throws AbruptExitException %7B%0A    // Only register this component if JNI is available.%0A    if (
+private final Boolean jniEnabled;%0A%0A  public SleepPreventionModule() %7B%0A    this.jniEnabled = 
 !%220%22
@@ -2027,57 +2027,9 @@
 i%22))
-) %7B%0A      env.getEventBus().register(this);%0A    %7D
+;
 %0A  %7D
@@ -2029,32 +2029,31 @@
 ));%0A  %7D%0A%0A  @
-Subscrib
+Overrid
 e%0A  public v
@@ -2061,53 +2061,100 @@
 id b
-uildStarting(BuildStartingEvent event
+eforeCommand(CommandEnvironment env) throws AbruptExitException %7B%0A    if (jniEnabled
 ) %7B%0A    
 Slee
@@ -2141,32 +2141,34 @@
 iEnabled) %7B%0A    
+  
 SleepPrevention.
@@ -2187,24 +2187,30 @@
 leep();%0A
+    %7D%0A
   %7D%0A%0A  @
 Subscrib
@@ -2205,16 +2205,15 @@
 %0A  @
-Subscrib
+Overrid
 e%0A  
@@ -2228,50 +2228,76 @@
 oid 
-buildComplete(BuildCompleteEvent event
+afterCommand() throws AbruptExitException %7B%0A    if (jniEnabled
 ) %7B%0A
+  
     
@@ -2327,18 +2327,24 @@
 bleSleep();%0A
+    %7D%0A
   %7D%0A%7D%0A

Move {push|pop}DisableSleep to beforeCommand/afterCommand instead of registering for the event bus.