1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CRASH_REPORTER_UNCLEAN_SHUTDOWN_COLLECTOR_H_
18 #define CRASH_REPORTER_UNCLEAN_SHUTDOWN_COLLECTOR_H_
19 
20 #include <string>
21 
22 #include <base/files/file_path.h>
23 #include <base/macros.h>
24 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
25 
26 #include "crash_collector.h"
27 
28 // Unclean shutdown collector.
29 class UncleanShutdownCollector : public CrashCollector {
30  public:
31   UncleanShutdownCollector();
32   ~UncleanShutdownCollector() override;
33 
34   // Enable collection - signal that a boot has started.
35   bool Enable();
36 
37   // Collect if there is was an unclean shutdown. Returns true if
38   // there was, false otherwise.
39   bool Collect();
40 
41   // Disable collection - signal that the system has been shutdown cleanly.
42   bool Disable();
43 
44  private:
45   friend class UncleanShutdownCollectorTest;
46   FRIEND_TEST(UncleanShutdownCollectorTest, EnableCannotWrite);
47   FRIEND_TEST(UncleanShutdownCollectorTest, CollectDeadBatterySuspended);
48 
49   bool DeleteUncleanShutdownFiles();
50 
51   // Check for unclean shutdown due to battery running out by analyzing powerd
52   // trace files.
53   bool DeadBatteryCausedUncleanShutdown();
54 
55   const char *unclean_shutdown_file_;
56   base::FilePath powerd_trace_path_;
57   base::FilePath powerd_suspended_file_;
58 
59   DISALLOW_COPY_AND_ASSIGN(UncleanShutdownCollector);
60 };
61 
62 #endif  // CRASH_REPORTER_UNCLEAN_SHUTDOWN_COLLECTOR_H_
63