Bug 157062 - Purge PassRefPtr in Modules/battery
Summary: Purge PassRefPtr in Modules/battery
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Gyuyoung Kim
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-04-27 00:05 PDT by Gyuyoung Kim
Modified: 2016-05-26 16:52 PDT (History)
3 users (show)

See Also:


Attachments
Patch (10.12 KB, patch)
2016-04-27 00:06 PDT, Gyuyoung Kim
no flags Details | Formatted Diff | Diff
Patch (11.22 KB, patch)
2016-05-23 22:10 PDT, Gyuyoung Kim
no flags Details | Formatted Diff | Diff
Patch (13.75 KB, patch)
2016-05-24 18:15 PDT, Gyuyoung Kim
no flags Details | Formatted Diff | Diff
Patch (13.75 KB, patch)
2016-05-25 02:21 PDT, Gyuyoung Kim
no flags Details | Formatted Diff | Diff
Patch (13.20 KB, patch)
2016-05-25 18:46 PDT, Gyuyoung Kim
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Gyuyoung Kim 2016-04-27 00:05:06 PDT
SSIA
Comment 1 Gyuyoung Kim 2016-04-27 00:06:43 PDT
Created attachment 277452 [details]
Patch
Comment 2 Alex Christensen 2016-04-27 22:53:15 PDT
Comment on attachment 277452 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=277452&action=review

This isn't compiled on mac, right?

> Source/WebCore/Modules/battery/BatteryController.cpp:84
> -void BatteryController::didChangeBatteryStatus(const AtomicString& eventType, PassRefPtr<BatteryStatus> batteryStatus)
> +void BatteryController::didChangeBatteryStatus(const AtomicString& eventType, RefPtr<BatteryStatus>&& batteryStatus)

That's a lot of copyRef.  If you made this a RefPtr<BatteryStatus>&, it could probably eliminate a lot of them.

> Source/WebKit2/WebProcess/Battery/WebBatteryManager.cpp:78
> -    RefPtr<BatteryStatus> status = BatteryStatus::create(data.isCharging, data.chargingTime, data.dischargingTime, data.level);
> +    Ref<BatteryStatus> status = BatteryStatus::create(data.isCharging, data.chargingTime, data.dischargingTime, data.level);

auto

> Source/WebKit2/WebProcess/Battery/WebBatteryManager.cpp:88
> -    RefPtr<BatteryStatus> status = BatteryStatus::create(data.isCharging, data.chargingTime, data.dischargingTime, data.level);
> +    Ref<BatteryStatus> status = BatteryStatus::create(data.isCharging, data.chargingTime, data.dischargingTime, data.level);

auto
Comment 3 Darin Adler 2016-04-28 23:50:11 PDT
Comment on attachment 277452 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=277452&action=review

> Source/WebCore/Modules/battery/BatteryController.cpp:51
> -        batteryManager->updateBatteryStatus(m_batteryStatus);
> +        batteryManager->updateBatteryStatus(WTFMove(m_batteryStatus));

This looks wrong. The old code would not null out m_batteryStatus, but this new code will.
Comment 4 Gyuyoung Kim 2016-05-23 22:10:34 PDT
Created attachment 279624 [details]
Patch
Comment 5 Gyuyoung Kim 2016-05-23 23:27:33 PDT
Sorry for too late update. I'm busy at work lately.
Comment 6 Darin Adler 2016-05-24 08:40:08 PDT
Comment on attachment 279624 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=279624&action=review

> Source/WebCore/Modules/battery/BatteryClient.h:38
> +void provideBatteryTo(Page*, BatteryClient&);

Should be Page& also.

> Source/WebCore/Modules/battery/BatteryController.cpp:65
>      RefPtr<BatteryStatus> status = batteryStatus;

No need for a local variable now that we are no longer using PassRefPtr.

> Source/WebCore/Modules/battery/BatteryController.cpp:81
>      m_batteryStatus = status.release();

This should be WTFMove instead of release(). The release() function creates a PassRefPtr.

> Source/WebCore/Modules/battery/BatteryController.cpp:87
>      RefPtr<BatteryStatus> battery = batteryStatus;

No need for this local variable once we are no longer using PassRefPtr.

> Source/WebCore/Modules/battery/BatteryController.h:40
> +    void updateBatteryStatus(RefPtr<BatteryStatus>&);

This is wrong; should be RefPtr<BatteryStatus>&&.

> Source/WebCore/Modules/battery/BatteryController.h:41
> +    void didChangeBatteryStatus(const AtomicString& eventType, RefPtr<BatteryStatus>&);

This is wrong; should be BatteryStatus*, not RefPtr.

> Source/WebCore/Modules/battery/BatteryManager.cpp:88
>      m_batteryStatus = batteryStatus;

Needs WTFMove.

> Source/WebCore/Modules/battery/BatteryManager.h:49
> +    void didChangeBatteryStatus(Event&, RefPtr<BatteryStatus>&);

This is wrong; should be RefPtr<BatteryStatus>&&.

> Source/WebCore/Modules/battery/BatteryManager.h:50
> +    void updateBatteryStatus(RefPtr<BatteryStatus>&);

This is wrong; should be BatteryStatus*, not RefPtr.

> Source/WebCore/testing/Internals.cpp:1564
> -    BatteryController::from(document->page())->didChangeBatteryStatus(eventType, BatteryStatus::create(charging, chargingTime, dischargingTime, level));
> +    RefPtr<BatteryStatus> status = BatteryStatus::create(charging, chargingTime, dischargingTime, level);
> +    BatteryController::from(document->page())->didChangeBatteryStatus(eventType, status);

Why the change? The code reads better on a single line, and the use of RefPtr is incorrect here; should be Ref, I think. Normally we’d use auto. But it should just work all on one line.
Comment 7 Gyuyoung Kim 2016-05-24 18:15:09 PDT
Created attachment 279735 [details]
Patch
Comment 8 Alex Christensen 2016-05-24 23:19:31 PDT
Comment on attachment 279735 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=279735&action=review

> Source/WebKit2/WebProcess/Battery/WebBatteryManager.cpp:92
>      for (auto* page : m_pageSet) {
>          if (page->corePage())
> -            BatteryController::from(page->corePage())->updateBatteryStatus(status.get());
> +            BatteryController::from(page->corePage())->updateBatteryStatus(WTFMove(status));

Because this could happen more than once, this needs to use copyRef.  It will not have the intended behavior after the first page.
Comment 9 Gyuyoung Kim 2016-05-25 02:21:59 PDT
Created attachment 279751 [details]
Patch
Comment 10 Gyuyoung Kim 2016-05-25 02:25:09 PDT
(In reply to comment #8)
> Comment on attachment 279735 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=279735&action=review
> 
> > Source/WebKit2/WebProcess/Battery/WebBatteryManager.cpp:92
> >      for (auto* page : m_pageSet) {
> >          if (page->corePage())
> > -            BatteryController::from(page->corePage())->updateBatteryStatus(status.get());
> > +            BatteryController::from(page->corePage())->updateBatteryStatus(WTFMove(status));
> 
> Because this could happen more than once, this needs to use copyRef.  It
> will not have the intended behavior after the first page.

yes, it can cause a crash. Fixed.
Comment 11 Darin Adler 2016-05-25 03:29:54 PDT
Comment on attachment 279751 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=279751&action=review

> Source/WebKit2/WebProcess/Battery/WebBatteryManager.cpp:78
> +    auto status = BatteryStatus::create(data.isCharging, data.chargingTime, data.dischargingTime, data.level);

The storage model of this code seems quite peculiar. It’s so strange that we create a temporary BatteryStatus and call didChangeBatteryStatus and then throw it away. It’s not stored anywhere?
Comment 12 Gyuyoung Kim 2016-05-25 18:44:52 PDT
Comment on attachment 279751 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=279751&action=review

>> Source/WebKit2/WebProcess/Battery/WebBatteryManager.cpp:78
>> +    auto status = BatteryStatus::create(data.isCharging, data.chargingTime, data.dischargingTime, data.level);
> 
> The storage model of this code seems quite peculiar. It’s so strange that we create a temporary BatteryStatus and call didChangeBatteryStatus and then throw it away. It’s not stored anywhere?

Previous implementation had been passed a pointer from here to BatteryManager class. Then BatteryManager manages it using a RefPtr. I thought that this is fine because BatteryManager will maintain the *BatteryStatus*. But now, as your question, it would be better to pass an ownership of the *BatteryStatus* to BatteryManager.
Comment 13 Gyuyoung Kim 2016-05-25 18:46:26 PDT
Created attachment 279848 [details]
Patch
Comment 14 WebKit Commit Bot 2016-05-26 16:52:15 PDT
Comment on attachment 279848 [details]
Patch

Clearing flags on attachment: 279848

Committed r201440: <http://trac.webkit.org/changeset/201440>
Comment 15 WebKit Commit Bot 2016-05-26 16:52:19 PDT
All reviewed patches have been landed.  Closing bug.