# cap-go/capacitor-inappbrowser architecture

> Architecture diagram of the GitHub repository cap-go/capacitor-inappbrowser, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cap-go/capacitor-inappbrowser
- Repository: https://github.com/cap-go/capacitor-inappbrowser

## Overview

##### Project Type and Purpose
- This is a Capacitor plugin (“@capgo/inappbrowser”) that exposes a unified JavaScript API for opening and controlling in-app browsers across Android, iOS, and Web.
- The plugin wraps native WebView / Custom Tabs / SFSafariViewController and provides two-way messaging, camera/microphone support, event listeners, cookie/cache management, etc.
- It ships as an NPM package, integrates via `npx cap sync`, and includes an `example-app` demonstrating usage on Android, iOS, and web.

##### File-Structure Analysis
- Top-level:
- `src/` – Typescript implementation (core JS façade, web fallback).
- `android/` – Android plugin module (Java code, resources, layouts).
- `ios/` – iOS plugin module (Swift code, WKWebView controller).
- `example-app/` – Sample Capacitor app integrating the plugin.
- Build/config: `rollup.config.mjs`, `package.json`, `tsconfig.json`, GitHub workflows (.github/workflows).
- Patterns: Bridge pattern (JS↔Native), Observer pattern (event listeners), Facade API (InAppBrowser.open/openWebView).

##### Key README Insights
- Defines plugin API surface (open, openWebView, postMessage, addListener, clearCookies, etc.).
- Describes platform-specific requirements (AndroidManifest.xml permissions, Info.plist entries).
- Mentions injected JS interface (`window.mobileApp`) for two-way communication.

A. Presentation / App Layer
- “Host App” (example-app or any Capacitor user app)
- Calls `InAppBrowser.open(...)`, `openWebView(...)`, `postMessage(...)`, `addListener(...)`.

B. JS Plugin Layer
- `@capgo/inappbrowser` NPM package:
- `src/index.ts` exports public API.
- `src/web.ts` fallback (calls `window.open`).
- `src/definitions.ts` provides TypeScript types.
- Uses Capacitor Bridge to dispatch calls to native.

C. Bridge Layer
- Capacitor Core (not in this repo but implied):
- Marshals JS calls to platform plugins.
- Serializes parameters/events across JS⇄Native boundary.

D. Native Platform Modules
1. Android Module (`android/src/main/java/…/InAppBrowserPlugin.java`)
- Reads `Options.java`, instantiates `WebViewDialog.java`, uses `WebViewCallbacks.java`.
- Underlying Custom Tabs or Android WebView.
2. iOS Module (`ios/Sources/InAppBrowserPlugin/InAppBrowserPlugin.swift`)
- Instantiates `WKWebViewController.swift`, uses `Enums.swift`.
- Wraps SFSafariViewController or WKWebView.

E. OS WebView / System APIs
- Android: Chrome Custom Tabs / WebView
- iOS: SFSafariViewController / WKWebView
- Web: Browser `window.open`

F. External Services / Permissions
- Camera & Microphone (via AndroidManifest and Info.plist)
- Cookie & Cache storage (system-managed)

G. Example-App
- Demonstrates integration: `capacitor.config.json`, platform folders, JS code in `example-app/src`.

##### Relationships & Interactions
- Host App → Plugin JS API → Capacitor Bridge → Native Module → System WebView
- WebView → Native Module event callbacks → Bridge → Plugin JS → Host App listeners
- Plugin JS → fallback to Web layer for unsupported platforms
- CI layer: GitHub Actions run lint, build, test (via `.github/workflows/*.yml`)

##### Architectural Patterns & Principles
- Bridge Pattern: decoupling JS API from multiple native implementations
- Observer Pattern: `addListener` for events from native/webview
- Facade Pattern: Single unified JS interface abstracts platform differences
- Separation of Concerns:
- Core API in TS
- Platform-specific code in respective directories
- Example app isolated under `example-app/`

## Diagram (Mermaid)

```mermaid
flowchart TD
    classDef js fill:#cfe2f3,stroke:#6fa8dc,color:#073763
    classDef android fill:#d9ead3,stroke:#6aa84f,color:#274e13
    classDef ios fill:#ead1dc,stroke:#a64d79,color:#741b47
    classDef external fill:#f2f2f2,stroke:#999999,color:#333333
    classDef example fill:#fff2cc,stroke:#bf9000,color:#663c00
    classDef build fill:#e6e0ec,stroke:#8e7cc3,color:#4a235a
    classDef bridge fill:#d9e1f2,stroke:#3c78d8,color:#073763
    classDef ci fill:#f4cccc,stroke:#cc0000,color:#660000

    subgraph "Presentation / App Layer"
        HostApp["Host App"]:::example
    end

    subgraph "JS Plugin Layer"
        PublicAPI["src/index.ts\n(Public API)"]:::js
        WebFallback["src/web.ts\n(Web Fallback)"]:::js
        Definitions["src/definitions.ts\n(Type Definitions)"]:::js
    end

    subgraph "Build & Config"
        Rollup["rollup.config.mjs"]:::build
        PackageJSON["package.json"]:::build
    end

    subgraph "Bridge Layer"
        CapacitorBridge["Capacitor Bridge"]:::bridge
    end

    subgraph "Native Platform Modules"
        subgraph "Android Module"
            APlugin["InAppBrowserPlugin.java"]:::android
            AOptions["Options.java"]:::android
            ADialog["WebViewDialog.java"]:::android
            ACallbacks["WebViewCallbacks.java"]:::android
            AndroidManifest["AndroidManifest.xml"]:::android
        end
        subgraph "iOS Module"
            IOSPlugin["InAppBrowserPlugin.swift"]:::ios
            IWController["WKWebViewController.swift"]:::ios
            IEnums["Enums.swift"]:::ios
            Podspec["CapgoInappbrowser.podspec"]:::ios
        end
    end

    subgraph "OS WebView / System APIs"
        AndroidWebView["Chrome Custom Tabs / WebView"]:::external
        iOSWebView["SFSafariViewController / WKWebView"]:::external
        Browser["window.open (Web)"]:::external
    end

    subgraph "Example-App Artifacts"
        ExampleConfig["capacitor.config.json"]:::example
        ExampleSrc["src/ (demo code)"]:::example
    end

    subgraph "Continuous Integration"
        CI[".github/workflows/"]:::ci
    end

    %% Relationships
    HostApp -->|"calls open(), postMessage()"| PublicAPI
    PublicAPI -->|"dispatch calls"| CapacitorBridge
    PublicAPI -->|"fallback"| WebFallback
    CapacitorBridge -->|"invoke"| APlugin
    CapacitorBridge -->|"invoke"| IOSPlugin
    APlugin -->|"uses"| AOptions
    APlugin -->|"opens"| ADialog
    APlugin -->|"registers"| ACallbacks
    APlugin -->|"loads"| AndroidWebView
    IOSPlugin -->|"uses"| IWController
    IOSPlugin -->|"uses"| IEnums
    IOSPlugin -->|"packages"| Podspec
    CapacitorBridge -->|"serializes events"| PublicAPI
    ACallbacks -.->|"event callbacks"| CapacitorBridge
    Browser -->|".postMessage events"| PublicAPI

    HostApp --> ExampleConfig
    HostApp --> ExampleSrc
    CI --> Rollup
    CI --> PackageJSON
    CI --> PublicAPI
    CI --> WebFallback

    %% Click Events
    click HostApp "https://github.com/cap-go/capacitor-inappbrowser/tree/main/example-app/"
    click PublicAPI "https://github.com/cap-go/capacitor-inappbrowser/blob/main/src/index.ts"
    click WebFallback "https://github.com/cap-go/capacitor-inappbrowser/blob/main/src/web.ts"
    click Definitions "https://github.com/cap-go/capacitor-inappbrowser/blob/main/src/definitions.ts"
    click Rollup "https://github.com/cap-go/capacitor-inappbrowser/blob/main/rollup.config.mjs"
    click PackageJSON "https://github.com/cap-go/capacitor-inappbrowser/blob/main/package.json"
    click APlugin "https://github.com/cap-go/capacitor-inappbrowser/blob/main/android/src/main/java/ee/forgr/capacitor_inappbrowser/InAppBrowserPlugin.java"
    click AOptions "https://github.com/cap-go/capacitor-inappbrowser/blob/main/android/src/main/java/ee/forgr/capacitor_inappbrowser/Options.java"
    click ADialog "https://github.com/cap-go/capacitor-inappbrowser/blob/main/android/src/main/java/ee/forgr/capacitor_inappbrowser/WebViewDialog.java"
    click ACallbacks "https://github.com/cap-go/capacitor-inappbrowser/blob/main/android/src/main/java/ee/forgr/capacitor_inappbrowser/WebViewCallbacks.java"
    click AndroidManifest "https://github.com/cap-go/capacitor-inappbrowser/blob/main/android/src/main/AndroidManifest.xml"
    click IOSPlugin "https://github.com/cap-go/capacitor-inappbrowser/blob/main/ios/Sources/InAppBrowserPlugin/InAppBrowserPlugin.swift"
    click IWController "https://github.com/cap-go/capacitor-inappbrowser/blob/main/ios/Sources/InAppBrowserPlugin/WKWebViewController.swift"
    click IEnums "https://github.com/cap-go/capacitor-inappbrowser/blob/main/ios/Sources/InAppBrowserPlugin/Enums.swift"
    click Podspec "https://github.com/cap-go/capacitor-inappbrowser/blob/main/CapgoInappbrowser.podspec"
    click ExampleConfig "https://github.com/cap-go/capacitor-inappbrowser/blob/main/example-app/capacitor.config.json"
    click ExampleSrc "https://github.com/cap-go/capacitor-inappbrowser/tree/main/example-app/src/"
    click CI "https://github.com/cap-go/capacitor-inappbrowser/tree/main/.github/workflows/"

    %% Styles
    class HostApp,ExampleConfig,ExampleSrc example
    class PublicAPI,WebFallback,Definitions js
    class Rollup,PackageJSON build
    class CapacitorBridge bridge
    class APlugin,AOptions,ADialog,ACallbacks,AndroidManifest android
    class IOSPlugin,IWController,IEnums,Podspec ios
    class AndroidWebView,iOSWebView,Browser external
    class CI ci
```
