/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * 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.
 */

syntax = "proto2";

package android.os.statsd.sdksandbox;

import "frameworks/proto_logging/stats/atom_field_options.proto";
import "frameworks/proto_logging/stats/atoms.proto";

option java_package = "com.android.os.sdksandbox";
option java_multiple_files = true;

extend Atom {
  // Pushed atoms
  optional SandboxApiCalled sandbox_api_called = 488 [(module) = "sdksandbox"];
  optional SandboxActivityEventOccurred sandbox_activity_event_occurred = 735
      [(module) = "sdksandbox", (truncate_timestamp) = true];
  optional SdkSandboxRestrictedAccessInSession sdk_sandbox_restricted_access_in_session = 796
      [(module) = "sdksandbox", (truncate_timestamp) = true];

  // Pulled atoms
  optional SandboxSdkStorage sandbox_sdk_storage = 10159 [(module) = "sdksandbox"];
}

// Logs when an API call from app to sandbox process is made
// Next ID: 6
message SandboxApiCalled {
  // Next ID: 16
  enum Method {
    METHOD_UNSPECIFIED = 0;
    LOAD_SDK = 1;
    REQUEST_SURFACE_PACKAGE = 3;
    // Api used by Apps via SdkSandboxManager
    GET_SANDBOXED_SDKS = 5;
    SYNC_DATA_FROM_CLIENT = 6;
    UNLOAD_SDK = 7;
    ADD_SDK_SANDBOX_LIFECYCLE_CALLBACK = 8;
    REMOVE_SDK_SANDBOX_LIFECYCLE_CALLBACK = 9;
    REGISTER_APP_OWNED_SDK_SANDBOX_INTERFACE = 11;
    UNREGISTER_APP_OWNED_SDK_SANDBOX_INTERFACE = 12;
    GET_APP_OWNED_SDK_SANDBOX_INTERFACES = 13;
    // Api used by SDKs in the sandbox via SdkSandboxController
    GET_SANDBOXED_SDKS_VIA_CONTROLLER = 10;
    LOAD_SDK_VIA_CONTROLLER = 14;
    GET_APP_OWNED_SDK_SANDBOX_INTERFACES_VIA_CONTROLLER = 15;

    LOAD_SANDBOX_AND_SDK = 2 [deprecated = true];
    SEND_DATA = 4 [deprecated = true];
  }
  // The method which was called
  optional Method method = 1;

  // Next ID: 12
  enum Stage {
    STAGE_UNSPECIFIED = 0;
    APP_TO_SYSTEM_SERVER = 1;
    SYSTEM_SERVER_APP_TO_SANDBOX = 2;
    LOAD_SANDBOX = 3;
    SYSTEM_SERVER_TO_SANDBOX = 4;
    SANDBOX = 5;
    SDK = 6;
    SANDBOX_TO_SYSTEM_SERVER = 7;
    SYSTEM_SERVER_SANDBOX_TO_APP = 8;
    SYSTEM_SERVER_TO_APP = 9;
    TOTAL = 10;
    TOTAL_WITH_LOAD_SANDBOX = 11;
  }
  // The stage at which latency is being measured
  optional Stage stage = 4;

  // The success status of the call at stage
  optional bool success = 3;

  //Time taken from the initiation of the API till the callback was received
  optional int32 latency_millis = 2;

  // Uid of the app that made a call to sandbox
  optional int32 uid = 5 [(is_uid) = true];

  // Next ID: 7
  enum ResultCode {
    RESULT_CODE_UNSPECIFIED = 0;
    LOAD_SDK_NOT_FOUND = 1;
    LOAD_SDK_ALREADY_LOADED = 2;
    LOAD_SDK_SDK_DEFINED_ERROR = 3;
    LOAD_SDK_SDK_SANDBOX_DISABLED = 4;
    LOAD_SDK_INTERNAL_ERROR = 5;
    SDK_SANDBOX_PROCESS_NOT_AVAILABLE = 6;
  }

  // The result code of the overall call
  optional ResultCode result_code = 6;
}

//Logs periodically the storage of SDKs used by the app
message SandboxSdkStorage {

  // Boolean value to specify if the storage belongs to SDK or is shared
  optional bool shared = 1;

  // Storage
  optional int32 storage_kb = 2;

  // Uid of the app that made a call to sandbox
  optional int32 uid = 3 [(is_uid) = true];
}


/**
 * Logs latency of API calls related to Sandbox Activity creation.
 */
message SandboxActivityEventOccurred {
  enum Method {
    STAGE_UNSPECIFIED = 0;
    // Register/unregister activity handlers APIs
    REGISTER_SDK_SANDBOX_ACTIVITY_HANDLER = 1;
    UNREGISTER_SDK_SANDBOX_ACTIVITY_HANDLER = 2;
    // Putting/removing handlers from the map
    // Doesn't include repeated/non-existent handlers
    PUT_SDK_SANDBOX_ACTIVITY_HANDLER = 3;
    REMOVE_SDK_SANDBOX_SCTIVITY_HANDLER = 4;
    // Start activity APIs
    START_SDK_SANDBOX_ACTIVITY = 5;
    ENFORCE_ALLOWED_TO_HOST_SANDBOXED_ACTIVITY = 6;
    INTERCEPT_SANDBOX_ACTIVITY = 7;
    CREATE_SANDBOXED_ACTIVITY = 8;
    NOTIFY_SDK_ON_ACTIVITY_CREATION = 9;
    TOTAL = 10;
  }

  enum CallResult {
    CALL_RESULT_UNSPECIFIED = 0;
    SUCCESS = 1;
    FAILURE = 2;
    // Validation check-specific failures
    FAILURE_SECURITY_EXCEPTION = 3;
    FAILURE_SECURITY_EXCEPTION_NO_SANDBOX_PROCESS = 4;
    FAILURE_ILLEGAL_ARGUMENT_EXCEPTION = 5;
  }

  // Sandbox Activity API method that's being called
  optional Method method = 1;

  // Sandbox Activity API call result
  optional CallResult call_result = 2;

  // Time taken to complete the call
  optional int32 latency_millis = 3;

  // Uid of the client app for which the activity is created
  optional int32 client_uid = 4 [(is_uid) = true];

  // Uid of the SDK that's loaded into client's sandbox process and for which the activity is created
  optional int32 sdk_uid = 5 [(is_uid) = true];
}

message ActivityStartRequest {
  optional string intent_action = 1;
  optional int32 access_count = 2;
}

message RepeatedActivityStartRequest {
  repeated ActivityStartRequest activity_start_request = 1;
}

message ContentProviderAccessRequest {
  optional string authority = 1;
  optional int32 access_count = 2;
}

message RepeatedContentProviderAccessRequest {
  repeated ContentProviderAccessRequest content_provider_access_request = 1;
}

message BroadcastReceiverRegisterRequest {
  optional string intent_actions = 1;
  optional int32 access_count = 2;
}

message RepeatedBroadcastReceiverRegisterRequest {
  repeated BroadcastReceiverRegisterRequest broadcast_receiver_register_request = 1;
}

message ServiceStartOrBindRequest {
  optional string action = 1;
  optional string package_name = 2;
  optional string component_class_name = 3;
  optional string component_package_name = 4;
  optional int32 access_count = 5;
}

message RepeatedServiceStartOrBindRequest {
  repeated ServiceStartOrBindRequest service_start_or_bind_request = 1;
}

/**
 * Logs information around SDKs using ContentProviders, BroadcastReceivers,
 * Activities, and Services from their allow lists.
 */
message SdkSandboxRestrictedAccessInSession {
  // List of all the SDKs that were loaded in that session
  repeated string loaded_sdks = 1;
  // The effective target SDK version
  optional int32 effective_target_sdk_version = 2;
  // List of all the ContentProviders that were requested to be accessed
  optional RepeatedContentProviderAccessRequest content_providers_access_request = 3 [(log_mode) = MODE_BYTES];
  // List of the broadcastReceivers that were requested to be registered
  optional RepeatedBroadcastReceiverRegisterRequest broadcast_receivers_register_request = 4 [(log_mode) = MODE_BYTES];
  // List of all the activities that were requested to be started
  optional RepeatedActivityStartRequest activities_start_request = 5 [(log_mode) = MODE_BYTES];
  // List of all the services that were requested to be started or bound
  optional RepeatedServiceStartOrBindRequest services_start_or_bind_request = 6 [(log_mode) = MODE_BYTES];
}
