# Global Metrics

path: .metrics.halstead.volume
old: 4240.382734446322
new: 10057.188777899692
path: .metrics.halstead.purity_ratio
old: 1.0081656759131996
new: 2.1851667156548285
path: .metrics.halstead.effort
old: 185331.22788739452
new: 153201.2542963555
path: .metrics.halstead.n2
old: 80.0
new: 309.0
path: .metrics.halstead.bugs
old: 1.0835388331289195
new: 0.9543777209948452
path: .metrics.halstead.N1
old: 370.0
new: 681.0
path: .metrics.halstead.estimated_program_length
old: 634.1362101494026
new: 2630.9407256484133
path: .metrics.halstead.n1
old: 27.0
new: 18.0
path: .metrics.halstead.N2
old: 259.0
new: 523.0
path: .metrics.halstead.length
old: 629.0
new: 1204.0
path: .metrics.halstead.level
old: 0.02288002288002288
new: 0.06564690885914595
path: .metrics.halstead.difficulty
old: 43.70625
new: 15.233009708737864
path: .metrics.halstead.time
old: 10296.179327077472
new: 8511.180794241973
path: .metrics.halstead.vocabulary
old: 107.0
new: 327.0
path: .metrics.mi.mi_sei
old: 9.017686929901846
new: -14.117334372839268
path: .metrics.mi.mi_visual_studio
old: 23.912637275670843
new: 11.298768794213826
path: .metrics.mi.mi_original
old: 40.89060974139714
new: 19.32089463810564
path: .metrics.nargs.sum
old: 7.0
new: 17.0
path: .metrics.nargs.average
old: 2.3333333333333335
new: 1.2142857142857142
path: .metrics.cyclomatic.average
old: 5.75
new: 1.0869565217391304
path: .metrics.cyclomatic.sum
old: 23.0
new: 25.0
path: .metrics.loc.sloc
old: 152.0
new: 424.0
path: .metrics.loc.blank
old: 25.0
new: 78.0
path: .metrics.loc.cloc
old: 15.0
new: 80.0
path: .metrics.loc.lloc
old: 60.0
new: 19.0
path: .metrics.loc.ploc
old: 112.0
new: 266.0
path: .metrics.nexits.sum
old: 2.0
new: 9.0
path: .metrics.nexits.average
old: 0.6666666666666666
new: 0.6428571428571429
path: .metrics.cognitive.sum
old: 21.0
new: 1.0
path: .metrics.cognitive.average
old: 7.0
new: 0.07142857142857142
path: .metrics.nom.total
old: 3.0
new: 14.0
path: .metrics.nom.functions
old: 3.0
new: 14.0

# Spaces Data

## Minimal test - lines (31, 422)

path: .spaces[0].metrics.loc.ploc
old: 68.0
new: 251.0
path: .spaces[0].metrics.loc.sloc
old: 84.0
new: 392.0
path: .spaces[0].metrics.loc.lloc
old: 48.0
new: 19.0
path: .spaces[0].metrics.loc.cloc
old: 0.0
new: 66.0
path: .spaces[0].metrics.loc.blank
old: 16.0
new: 75.0
path: .spaces[0].metrics.halstead.purity_ratio
old: 0.7409649500302142
new: 2.1050897867664964
path: .spaces[0].metrics.halstead.N2
old: 171.0
new: 509.0
path: .spaces[0].metrics.halstead.n1
old: 15.0
new: 18.0
path: .spaces[0].metrics.halstead.N1
old: 251.0
new: 681.0
path: .spaces[0].metrics.halstead.bugs
old: 0.5649706246734154
new: 0.952547838491272
path: .spaces[0].metrics.halstead.estimated_program_length
old: 312.6872089127504
new: 2505.0568462521305
path: .spaces[0].metrics.halstead.effort
old: 69778.34800324125
new: 152760.85339290652
path: .spaces[0].metrics.halstead.level
old: 0.03586744639376219
new: 0.06461471294477189
path: .spaces[0].metrics.halstead.difficulty
old: 27.880434782608695
new: 15.47635135135135
path: .spaces[0].metrics.halstead.time
old: 3876.5748890689583
new: 8486.714077383695
path: .spaces[0].metrics.halstead.length
old: 422.0
new: 1190.0
path: .spaces[0].metrics.halstead.vocabulary
old: 61.0
new: 314.0
path: .spaces[0].metrics.halstead.n2
old: 46.0
new: 296.0
path: .spaces[0].metrics.halstead.volume
old: 2502.771156451538
new: 9870.598691181038
path: .spaces[0].metrics.cyclomatic.average
old: 16.0
new: 1.0909090909090908
path: .spaces[0].metrics.cyclomatic.sum
old: 16.0
new: 24.0
path: .spaces[0].metrics.nom.total
old: 1.0
new: 14.0
path: .spaces[0].metrics.nom.functions
old: 1.0
new: 14.0
path: .spaces[0].metrics.mi.mi_original
old: 54.84996778909979
new: 20.919516096464832
path: .spaces[0].metrics.mi.mi_sei
old: 5.060042280712295
new: -13.39068706297039
path: .spaces[0].metrics.mi.mi_visual_studio
old: 32.076004555029115
new: 12.23363514413148
path: .spaces[0].metrics.nexits.average
old: 0.0
new: 0.6428571428571429
path: .spaces[0].metrics.nexits.sum
old: 0.0
new: 9.0
path: .spaces[0].metrics.cognitive.sum
old: 15.0
new: 1.0
path: .spaces[0].metrics.cognitive.average
old: 15.0
new: 0.07142857142857142
path: .spaces[0].metrics.nargs.average
old: 2.0
new: 1.2142857142857142
path: .spaces[0].metrics.nargs.sum
old: 2.0
new: 17.0

### Code

    namespace mozilla {

    namespace dom {
    class BrowserChild;
    }  // namespace dom

    namespace widget {

    struct AutoCacheNativeKeyCommands;

    class PuppetWidget : public nsBaseWidget,
                         public TextEventDispatcherListener,
                         public layers::MemoryPressureListener {
      typedef mozilla::CSSRect CSSRect;
      typedef mozilla::dom::BrowserChild BrowserChild;
      typedef mozilla::gfx::DrawTarget DrawTarget;

      // Avoiding to make compiler confused between mozilla::widget and nsIWidget.
      typedef mozilla::widget::TextEventDispatcher TextEventDispatcher;
      typedef mozilla::widget::TextEventDispatcherListener
          TextEventDispatcherListener;

      typedef nsBaseWidget Base;

      // The width and height of the "widget" are clamped to this.
      static const size_t kMaxDimension;

     public:
      explicit PuppetWidget(BrowserChild* aBrowserChild);

     protected:
      virtual ~PuppetWidget();

     public:
      NS_DECL_ISUPPORTS_INHERITED

      // PuppetWidget creation is infallible, hence InfallibleCreate(), which
      // Create() calls.
      using nsBaseWidget::Create;  // for Create signature not overridden here
      virtual nsresult Create(nsIWidget* aParent, nsNativeWidget aNativeParent,
                              const LayoutDeviceIntRect& aRect,
                              nsWidgetInitData* aInitData = nullptr) override;
      void InfallibleCreate(nsIWidget* aParent, nsNativeWidget aNativeParent,
                            const LayoutDeviceIntRect& aRect,
                            nsWidgetInitData* aInitData = nullptr);

      void InitIMEState();

      virtual already_AddRefed CreateChild(
          const LayoutDeviceIntRect& aRect, nsWidgetInitData* aInitData = nullptr,
          bool aForceUseIWidgetParent = false) override;

      virtual void Destroy() override;

      virtual void Show(bool aState) override;

      virtual bool IsVisible() const override { return mVisible; }

      virtual void ConstrainPosition(bool /*ignored aAllowSlop*/, int32_t* aX,
                                     int32_t* aY) override {
        *aX = kMaxDimension;
        *aY = kMaxDimension;
      }

      // Widget position is controlled by the parent process via BrowserChild.
      virtual void Move(double aX, double aY) override {}

      virtual void Resize(double aWidth, double aHeight, bool aRepaint) override;
      virtual void Resize(double aX, double aY, double aWidth, double aHeight,
                          bool aRepaint) override {
        if (!mBounds.IsEqualXY(aX, aY)) {
          NotifyWindowMoved(aX, aY);
        }
        mBounds.MoveTo(aX, aY);
        return Resize(aWidth, aHeight, aRepaint);
      }

      // XXX/cjones: copying gtk behavior here; unclear what disabling a
      // widget is supposed to entail
      virtual void Enable(bool aState) override { mEnabled = aState; }
      virtual bool IsEnabled() const override { return mEnabled; }

      virtual void SetFocus(Raise, mozilla::dom::CallerType aCallerType) override;

      virtual nsresult ConfigureChildren(
          const nsTArray& aConfigurations) override;

      virtual void Invalidate(const LayoutDeviceIntRect& aRect) override;

      // PuppetWidgets don't have native data, as they're purely nonnative.
      virtual void* GetNativeData(uint32_t aDataType) override;
    #if defined(XP_WIN)
      void SetNativeData(uint32_t aDataType, uintptr_t aVal) override;
    #endif

      // PuppetWidgets don't have any concept of titles.
      virtual nsresult SetTitle(const nsAString& aTitle) override {
        return NS_ERROR_UNEXPECTED;
      }

      virtual mozilla::LayoutDeviceToLayoutDeviceMatrix4x4
      WidgetToTopLevelWidgetTransform() override;

      virtual LayoutDeviceIntPoint WidgetToScreenOffset() override;

      virtual LayoutDeviceIntPoint TopLevelWidgetToScreenOffset() override {
        return GetWindowPosition();
      }

      int32_t RoundsWidgetCoordinatesTo() override;

      void InitEvent(WidgetGUIEvent& aEvent,
                     LayoutDeviceIntPoint* aPoint = nullptr);

      virtual nsresult DispatchEvent(WidgetGUIEvent* aEvent,
                                     nsEventStatus& aStatus) override;
      nsEventStatus DispatchInputEvent(WidgetInputEvent* aEvent) override;
      void SetConfirmedTargetAPZC(
          uint64_t aInputBlockId,
          const nsTArray& aTargets) const override;
      void UpdateZoomConstraints(
          const uint32_t& aPresShellId, const ScrollableLayerGuid::ViewID& aViewId,
          const mozilla::Maybe& aConstraints) override;
      bool AsyncPanZoomEnabled() const override;

      MOZ_CAN_RUN_SCRIPT virtual bool GetEditCommands(
          NativeKeyBindingsType aType, const mozilla::WidgetKeyboardEvent& aEvent,
          nsTArray& aCommands) override;

      friend struct AutoCacheNativeKeyCommands;

      //
      // nsBaseWidget methods we override
      //

      // Documents loaded in child processes are always subdocuments of
      // other docs in an ancestor process.  To ensure that the
      // backgrounds of those documents are painted like those of
      // same-process subdocuments, we force the widget here to be
      // transparent, which in turn will cause layout to use a transparent
      // backstop background color.
      virtual nsTransparencyMode GetTransparencyMode() override {
        return eTransparencyTransparent;
      }

      virtual LayerManager* GetLayerManager(
          PLayerTransactionChild* aShadowManager = nullptr,
          LayersBackend aBackendHint = mozilla::layers::LayersBackend::LAYERS_NONE,
          LayerManagerPersistence aPersistence = LAYER_MANAGER_CURRENT) override;

      // This is used for creating remote layer managers and for re-creating
      // them after a compositor reset. The lambda aInitializeFunc is used to
      // perform any caller-required initialization for the newly created layer
      // manager; in the event of a failure, return false and it will destroy the
      // new layer manager without changing the state of the widget.
      bool CreateRemoteLayerManager(
          const std::function& aInitializeFunc);

      bool HasLayerManager() { return !!mLayerManager; }

      virtual void SetInputContext(const InputContext& aContext,
                                   const InputContextAction& aAction) override;
      virtual InputContext GetInputContext() override;
      virtual NativeIMEContext GetNativeIMEContext() override;
      TextEventDispatcherListener* GetNativeTextEventDispatcherListener() override {
        return mNativeTextEventDispatcherListener
                   ? mNativeTextEventDispatcherListener.get()
                   : this;
      }
      void SetNativeTextEventDispatcherListener(
          TextEventDispatcherListener* aListener) {
        mNativeTextEventDispatcherListener = aListener;
      }

      virtual void SetCursor(nsCursor aDefaultCursor, imgIContainer* aCustomCursor,
                             uint32_t aHotspotX, uint32_t aHotspotY) override;

      virtual void ClearCachedCursor() override;

      // Gets the DPI of the screen corresponding to this widget.
      // Contacts the parent process which gets the DPI from the
      // proper widget there. TODO: Handle DPI changes that happen
      // later on.
      virtual float GetDPI() override;
      virtual double GetDefaultScaleInternal() override;

      virtual bool NeedsPaint() override;

      // Paint the widget immediately if any paints are queued up.
      void PaintNowIfNeeded();

      virtual BrowserChild* GetOwningBrowserChild() override {
        return mBrowserChild;
      }

      void UpdateBackingScaleCache(float aDpi, int32_t aRounding, double aScale) {
        mDPI = aDpi;
        mRounding = aRounding;
        mDefaultScale = aScale;
      }

      nsIntSize GetScreenDimensions();

      // safe area insets support
      virtual ScreenIntMargin GetSafeAreaInsets() const override;
      void UpdateSafeAreaInsets(const ScreenIntMargin& aSafeAreaInsets);

      // Get the offset to the chrome of the window that this tab belongs to.
      //
      // NOTE: In OOP iframes this value is zero. You should use
      // WidgetToTopLevelWidgetTransform instead which is already including the
      // chrome offset.
      LayoutDeviceIntPoint GetChromeOffset();

      // Get the screen position of the application window.
      LayoutDeviceIntPoint GetWindowPosition();

      virtual LayoutDeviceIntRect GetScreenBounds() override;

      virtual nsresult SynthesizeNativeKeyEvent(
          int32_t aNativeKeyboardLayout, int32_t aNativeKeyCode,
          uint32_t aModifierFlags, const nsAString& aCharacters,
          const nsAString& aUnmodifiedCharacters, nsIObserver* aObserver) override;
      virtual nsresult SynthesizeNativeMouseEvent(
          LayoutDeviceIntPoint aPoint, NativeMouseMessage aNativeMessage,
          MouseButton aButton, nsIWidget::Modifiers aModifierFlags,
          nsIObserver* aObserver) override;
      virtual nsresult SynthesizeNativeMouseMove(LayoutDeviceIntPoint aPoint,
                                                 nsIObserver* aObserver) override;
      virtual nsresult SynthesizeNativeMouseScrollEvent(
          LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
          double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
          uint32_t aAdditionalFlags, nsIObserver* aObserver) override;
      virtual nsresult SynthesizeNativeTouchPoint(uint32_t aPointerId,
                                                  TouchPointerState aPointerState,
                                                  LayoutDeviceIntPoint aPoint,
                                                  double aPointerPressure,
                                                  uint32_t aPointerOrientation,
                                                  nsIObserver* aObserver) override;
      virtual nsresult SynthesizeNativeTouchPadPinch(
          TouchpadPinchPhase aEventPhase, float aScale, LayoutDeviceIntPoint aPoint,
          int32_t aModifierFlags) override;
      virtual nsresult SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint,
                                                bool aLongTap,
                                                nsIObserver* aObserver) override;
      virtual nsresult ClearNativeTouchSequence(nsIObserver* aObserver) override;
      virtual uint32_t GetMaxTouchPoints() const override;
      virtual nsresult SynthesizeNativePenInput(
          uint32_t aPointerId, TouchPointerState aPointerState,
          LayoutDeviceIntPoint aPoint, double aPressure, uint32_t aRotation,
          int32_t aTiltX, int32_t aTiltY, nsIObserver* aObserver) override;

      virtual void StartAsyncScrollbarDrag(
          const AsyncDragMetrics& aDragMetrics) override;

      virtual void ZoomToRect(const uint32_t& aPresShellId,
                              const ScrollableLayerGuid::ViewID& aViewId,
                              const CSSRect& aRect,
                              const uint32_t& aFlags) override;

      virtual bool HasPendingInputEvent() override;

      virtual void LookUpDictionary(
          const nsAString& aText,
          const nsTArray& aFontRangeArray,
          const bool aIsVertical, const LayoutDeviceIntPoint& aPoint) override;

      nsresult SetSystemFont(const nsCString& aFontName) override;
      nsresult GetSystemFont(nsCString& aFontName) override;

      // TextEventDispatcherListener
      using nsBaseWidget::NotifyIME;
      NS_IMETHOD NotifyIME(TextEventDispatcher* aTextEventDispatcher,
                           const IMENotification& aNotification) override;
      NS_IMETHOD_(IMENotificationRequests) GetIMENotificationRequests() override;
      NS_IMETHOD_(void)
      OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher) override;
      NS_IMETHOD_(void)
      WillDispatchKeyboardEvent(TextEventDispatcher* aTextEventDispatcher,
                                WidgetKeyboardEvent& aKeyboardEvent,
                                uint32_t aIndexOfKeypress, void* aData) override;

      virtual void OnMemoryPressure(layers::MemoryPressureReason aWhy) override;

     private:
      void SetChild(PuppetWidget* aChild);

      nsresult RequestIMEToCommitComposition(bool aCancel);
      nsresult NotifyIMEOfFocusChange(const IMENotification& aIMENotification);
      nsresult NotifyIMEOfSelectionChange(const IMENotification& aIMENotification);
      nsresult NotifyIMEOfCompositionUpdate(
          const IMENotification& aIMENotification);
      nsresult NotifyIMEOfTextChange(const IMENotification& aIMENotification);
      nsresult NotifyIMEOfMouseButtonEvent(const IMENotification& aIMENotification);
      nsresult NotifyIMEOfPositionChange(const IMENotification& aIMENotification);

      bool CacheEditorRect();
      bool CacheCompositionRects(uint32_t& aStartOffset,
                                 nsTArray& aRectArray,
                                 uint32_t& aTargetCauseOffset);
      bool GetCaretRect(LayoutDeviceIntRect& aCaretRect, uint32_t aCaretOffset);
      uint32_t GetCaretOffset();

      nsIWidgetListener* GetCurrentWidgetListener();

      // When this widget caches input context and currently managed by
      // IMEStateManager, the cache is valid.
      bool HaveValidInputContextCache() const;

      nsRefreshDriver* GetTopLevelRefreshDriver() const;

      // BrowserChild normally holds a strong reference to this PuppetWidget
      // or its root ancestor, but each PuppetWidget also needs a
      // reference back to BrowserChild (e.g. to delegate nsIWidget IME calls
      // to chrome) So we hold a weak reference to BrowserChild here.  Since
      // it's possible for BrowserChild to outlive the PuppetWidget, we clear
      // this weak reference in Destroy()
      BrowserChild* mBrowserChild;
      // The "widget" to which we delegate events if we don't have an
      // event handler.
      RefPtr mChild;
      RefPtr mMemoryPressureObserver;
      // XXX/cjones: keeping this around until we teach LayerManager to do
      // retained-content-only transactions
      RefPtr mDrawTarget;
      // IME
      IMENotificationRequests mIMENotificationRequestsOfParent;
      InputContext mInputContext;
      // mNativeIMEContext is initialized when this dispatches every composition
      // event both from parent process's widget and TextEventDispatcher in same
      // process.  If it hasn't been started composition yet, this isn't necessary
      // for XP code since there is no TextComposition instance which is caused by
      // the PuppetWidget instance.
      NativeIMEContext mNativeIMEContext;
      ContentCacheInChild mContentCache;

      // The DPI of the screen corresponding to this widget
      float mDPI;
      int32_t mRounding;
      double mDefaultScale;

      nsCOMPtr mCustomCursor;
      uint32_t mCursorHotspotX, mCursorHotspotY;

      ScreenIntMargin mSafeAreaInsets;

      RefPtr mNativeTextEventDispatcherListener;

     protected:
      bool mEnabled;
      bool mVisible;

     private:
      bool mNeedIMEStateInit;
      // When remote process requests to commit/cancel a composition, the
      // composition may have already been committed in the main process.  In such
      // case, this will receive remaining composition events for the old
      // composition even after requesting to commit/cancel the old composition
      // but the TextComposition for the old composition has already been
      // destroyed. So, until this meets new eCompositionStart, following
      // composition events should be ignored if this is set to true.
      bool mIgnoreCompositionEvents;
    };

    class PuppetScreen : public nsBaseScreen {
     public:
      explicit PuppetScreen(void* nativeScreen);
      ~PuppetScreen();

      NS_IMETHOD GetRect(int32_t* aLeft, int32_t* aTop, int32_t* aWidth,
                         int32_t* aHeight) override;
      NS_IMETHOD GetAvailRect(int32_t* aLeft, int32_t* aTop, int32_t* aWidth,
                              int32_t* aHeight) override;
      NS_IMETHOD GetPixelDepth(int32_t* aPixelDepth) override;
      NS_IMETHOD GetColorDepth(int32_t* aColorDepth) override;
    };

    class PuppetScreenManager final : public nsIScreenManager {
      ~PuppetScreenManager();

     public:
      PuppetScreenManager();

      NS_DECL_ISUPPORTS
      NS_DECL_NSISCREENMANAGER

     protected:
      nsCOMPtr mOneScreen;
    };

    }  // namespace widget
    }  // namespace mozilla