How cap-go/capacitor-inappbrowser works
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/