cylee | 1fee649 | 2016-03-09 20:04:41 | [diff] [blame] | 1 | // Copyright 2016 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. |
yusukes | cd2c2981 | 2016-06-02 06:24:03 | [diff] [blame] | 4 | |
Henrique Ferreiro | 2ef8230 | 2021-03-24 12:12:25 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_ASH_ARC_PROCESS_ARC_PROCESS_H_ |
| 6 | #define CHROME_BROWSER_ASH_ARC_PROCESS_ARC_PROCESS_H_ |
cylee | 1fee649 | 2016-03-09 20:04:41 | [diff] [blame] | 7 | |
cylee | c2e58df | 2016-07-25 10:28:26 | [diff] [blame] | 8 | #include <stdint.h> |
yusukes | c10020e | 2017-05-22 16:18:51 | [diff] [blame] | 9 | |
| 10 | #include <ostream> |
cylee | 1fee649 | 2016-03-09 20:04:41 | [diff] [blame] | 11 | #include <string> |
yusukes | cd2c2981 | 2016-06-02 06:24:03 | [diff] [blame] | 12 | #include <vector> |
cylee | 1fee649 | 2016-03-09 20:04:41 | [diff] [blame] | 13 | |
Yao Li | 0a8f82f | 2021-12-01 02:07:48 | [diff] [blame^] | 14 | #include "ash/components/arc/mojom/process.mojom-forward.h" |
cylee | 1fee649 | 2016-03-09 20:04:41 | [diff] [blame] | 15 | #include "base/process/process_handle.h" |
cylee | 1fee649 | 2016-03-09 20:04:41 | [diff] [blame] | 16 | |
| 17 | namespace arc { |
| 18 | |
yusukes | cd2c2981 | 2016-06-02 06:24:03 | [diff] [blame] | 19 | class ArcProcess { |
| 20 | public: |
| 21 | ArcProcess(base::ProcessId nspid, |
| 22 | base::ProcessId pid, |
| 23 | const std::string& process_name, |
Chris Morin | c585d41a | 2018-06-12 04:26:00 | [diff] [blame] | 24 | mojom::ProcessState process_state, |
cylee | c2e58df | 2016-07-25 10:28:26 | [diff] [blame] | 25 | bool is_focused, |
| 26 | int64_t last_activity_time); |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 27 | |
| 28 | ArcProcess(const ArcProcess&) = delete; |
| 29 | ArcProcess& operator=(const ArcProcess&) = delete; |
| 30 | |
yusukes | cd2c2981 | 2016-06-02 06:24:03 | [diff] [blame] | 31 | ~ArcProcess(); |
| 32 | |
| 33 | ArcProcess(ArcProcess&& other); |
| 34 | ArcProcess& operator=(ArcProcess&& other); |
| 35 | |
cylee | c2e58df | 2016-07-25 10:28:26 | [diff] [blame] | 36 | // Sort by descending importance. |
| 37 | bool operator<(const ArcProcess& rhs) const; |
| 38 | |
yusukes | cd2c2981 | 2016-06-02 06:24:03 | [diff] [blame] | 39 | base::ProcessId nspid() const { return nspid_; } |
| 40 | base::ProcessId pid() const { return pid_; } |
| 41 | const std::string& process_name() const { return process_name_; } |
Chris Morin | c585d41a | 2018-06-12 04:26:00 | [diff] [blame] | 42 | mojom::ProcessState process_state() const { return process_state_; } |
cylee | c2e58df | 2016-07-25 10:28:26 | [diff] [blame] | 43 | bool is_focused() const { return is_focused_; } |
| 44 | int64_t last_activity_time() const { return last_activity_time_; } |
yusukes | cd2c2981 | 2016-06-02 06:24:03 | [diff] [blame] | 45 | std::vector<std::string>& packages() { return packages_; } |
| 46 | const std::vector<std::string>& packages() const { return packages_; } |
| 47 | |
Chris Morin | c585d41a | 2018-06-12 04:26:00 | [diff] [blame] | 48 | void set_process_state(mojom::ProcessState process_state) { |
Chris Morin | 9702b305 | 2018-05-22 21:01:37 | [diff] [blame] | 49 | process_state_ = process_state; |
| 50 | } |
| 51 | |
yusukes | ee315ba | 2017-05-11 22:46:38 | [diff] [blame] | 52 | // Returns true if the process is important and should be protected more |
| 53 | // from OOM kills than other processes. |
| 54 | // TODO(cylee|yusukes): Check what stock Android does for handling OOM and |
| 55 | // modify this function as needed (crbug.com/719537). |
| 56 | bool IsImportant() const; |
| 57 | |
Kuo-Hsin Yang | 49d3354 | 2018-08-30 09:51:15 | [diff] [blame] | 58 | // Returns true if it is persistent process and should have a lower |
| 59 | // oom_score_adj. |
yusukes | ee315ba | 2017-05-11 22:46:38 | [diff] [blame] | 60 | // TODO(cylee|yusukes): Consider removing this function. Having only |
| 61 | // IsImportant() might be good enough. |
Kuo-Hsin Yang | 49d3354 | 2018-08-30 09:51:15 | [diff] [blame] | 62 | bool IsPersistent() const; |
yusukes | ee315ba | 2017-05-11 22:46:38 | [diff] [blame] | 63 | |
Willie Koomson | 6d36d66 | 2018-12-18 18:55:55 | [diff] [blame] | 64 | // Returns true if the process is cached or empty and should have a higher |
| 65 | // oom_score_adj to be killed earlier. |
| 66 | bool IsCached() const; |
| 67 | |
| 68 | // Returns true if process is in the background but should have a lower |
| 69 | // oom_score_adj. |
| 70 | bool IsBackgroundProtected() const; |
| 71 | |
yusukes | cd2c2981 | 2016-06-02 06:24:03 | [diff] [blame] | 72 | private: |
[email protected] | dedfa29 | 2018-04-05 21:52:55 | [diff] [blame] | 73 | // Returns true if this is ARC protected process which we don't allow to kill. |
| 74 | bool IsArcProtected() const; |
| 75 | |
yusukes | cd2c2981 | 2016-06-02 06:24:03 | [diff] [blame] | 76 | base::ProcessId nspid_; |
| 77 | base::ProcessId pid_; |
| 78 | std::string process_name_; |
Chris Morin | c585d41a | 2018-06-12 04:26:00 | [diff] [blame] | 79 | mojom::ProcessState process_state_; |
cylee | c2e58df | 2016-07-25 10:28:26 | [diff] [blame] | 80 | // If the Android app is the focused window. |
| 81 | bool is_focused_; |
| 82 | // A monotonic timer recording the last time this process was active. |
| 83 | // Milliseconds since Android boot. This info is passed from Android |
| 84 | // ActivityManagerService via IPC. |
| 85 | int64_t last_activity_time_; |
yusukes | cd2c2981 | 2016-06-02 06:24:03 | [diff] [blame] | 86 | std::vector<std::string> packages_; |
| 87 | |
cylee | 576b449 | 2017-05-18 23:36:32 | [diff] [blame] | 88 | friend std::ostream& operator<<(std::ostream& out, |
| 89 | const ArcProcess& arc_process); |
cylee | 1fee649 | 2016-03-09 20:04:41 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace arc |
| 93 | |
Henrique Ferreiro | 2ef8230 | 2021-03-24 12:12:25 | [diff] [blame] | 94 | #endif // CHROME_BROWSER_ASH_ARC_PROCESS_ARC_PROCESS_H_ |