Commit 1c9b983a authored by ccameron's avatar ccameron Committed by Commit bot

Mac: Make ScopedTypeRef require explicit constructors

Style guide requires explicit constructors.

Prior to this patch, a T* can be passed into a function that requires
a scoped_nsobject<T>, and the object will be released but not retained.

Also make the copy constructor that takes a different typename more
generic, allowing for different types (e.g, subclasses) in addition to
different traits.

Clean up all instances where we relied on this behavior.

BUG=

Review-Url: https://codereview.chromium.org/2028823003
Cr-Commit-Position: refs/heads/master@{#397847}
parent ce674bd8
......@@ -53,7 +53,7 @@ class ScopedTypeRef {
public:
typedef T element_type;
ScopedTypeRef(
explicit ScopedTypeRef(
T object = Traits::InvalidValue(),
base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME)
: object_(object) {
......@@ -67,12 +67,10 @@ class ScopedTypeRef {
object_ = Traits::Retain(object_);
}
// Without this, passing a ScopedTypeRef<A,TraitsX> to construct a
// ScopedTypeRef<A,TraitsY> would automatically cast down to an A, and then
// ASSUME ownership of A, when a retain is what was needed.
template<typename OtherTraits>
ScopedTypeRef(const ScopedTypeRef<T, OtherTraits>& that_with_other_traits)
: object_(that_with_other_traits.get()) {
// This allows passing an object to a function that takes its superclass.
template <typename R, typename RTraits>
explicit ScopedTypeRef(const ScopedTypeRef<R, RTraits>& that_as_subclass)
: object_(that_as_subclass.get()) {
if (object_)
object_ = Traits::Retain(object_);
}
......
......@@ -140,7 +140,7 @@ TEST_F(ServiceDiscoveryClientMacTest, ServiceResolver) {
ServiceResolverImplMac* resolver_impl =
static_cast<ServiceResolverImplMac*>(resolver.get());
resolver_impl->GetContainerForTesting()->SetServiceForTesting(
test_service.release());
base::scoped_nsobject<NSNetService>(test_service));
resolver->StartResolving();
resolver_impl->GetContainerForTesting()->OnResolveUpdate(
......
......@@ -42,14 +42,14 @@ NSString* const kNotificationIncognito = @"notificationIncognito";
- (instancetype)init {
if ((self = [super init])) {
notificationData_ = [[NSMutableDictionary alloc] init];
notificationData_.reset([[NSMutableDictionary alloc] init]);
}
return self;
}
- (instancetype)initWithDictionary:(NSDictionary*)data {
if ((self = [super init])) {
notificationData_ = [data copy];
notificationData_.reset([data copy]);
}
return self;
}
......
......@@ -473,7 +473,7 @@ void CardUnmaskPromptViewBridge::PerformClose() {
storageView_.reset([[NSView alloc] initWithFrame:NSZeroRect]);
[storageView_ setAutoresizingMask:NSViewWidthSizable];
base::scoped_nsobject<NSBox> box = [CardUnmaskPromptViewCocoa createPlainBox];
base::scoped_nsobject<NSBox> box([CardUnmaskPromptViewCocoa createPlainBox]);
[box setAutoresizingMask:NSViewWidthSizable];
[box setFillColor:skia::SkColorToCalibratedNSColor(kShadingColor)];
[box setContentViewMargins:NSMakeSize(chrome_style::kHorizontalPadding,
......@@ -506,8 +506,8 @@ void CardUnmaskPromptViewBridge::PerformClose() {
NSMakePoint(NSMaxX([storageCheckbox_ frame]) + kButtonGap, 0)];
// Add horizontal separator.
base::scoped_nsobject<NSBox> separator =
[CardUnmaskPromptViewCocoa createPlainBox];
base::scoped_nsobject<NSBox> separator(
[CardUnmaskPromptViewCocoa createPlainBox]);
[separator setAutoresizingMask:NSViewWidthSizable];
[separator setFillColor:skia::SkColorToCalibratedNSColor(kSubtleBorderColor)];
[storageView_ addSubview:separator];
......
......@@ -495,8 +495,8 @@ void ToolbarActionsBarBridge::ShowToolbarActionBubble(
- (void)removeViewForAction:(ToolbarActionViewController*)action {
// We're about to remove the button view from the container as well as from
// |buttons_|, so make sure we retain a reference.
base::scoped_nsobject<BrowserActionButton> button =
[[self buttonForId:action->GetId()] retain];
base::scoped_nsobject<BrowserActionButton> button(
[[self buttonForId:action->GetId()] retain]);
// Note: We remove the button from the view and the buttons list first because
// destroying it (or calling -onRemoved) can cause redraws, and we don't want
......
......@@ -45,8 +45,8 @@ class ChooserDialogCocoa : public ConstrainedWindowMacDelegate,
private:
friend class ChooserDialogCocoaControllerTest;
base::scoped_nsobject<ChooserDialogCocoaController>
chooser_dialog_cocoa_controller_ = nullptr;
std::unique_ptr<ConstrainedWindowMac> constrained_window_ = nullptr;
chooser_dialog_cocoa_controller_;
std::unique_ptr<ConstrainedWindowMac> constrained_window_;
content::WebContents* web_contents_; // Weak.
ChooserController* chooser_controller_; // Weak.
};
......
......@@ -44,7 +44,7 @@ class FullscreenLowPowerCoordinatorCocoa
void EnterOrExitLowPowerModeIfNeeded();
// The main fullscreen window.
base::scoped_nsobject<NSWindow> content_window_ = nil;
base::scoped_nsobject<NSWindow> content_window_;
// Weak, reset by WillLoseAcceleratedWidget before it goes away.
ui::AcceleratedWidgetMac* widget_ = nullptr;
......
......@@ -128,10 +128,10 @@ const SkColor kMaterialDarkVectorIconColor = 0xCCFFFFFF;
base::mac::GetSRGBColorSpace());
}
base::scoped_nsobject<LocationBarImageRep> imageRep =
base::scoped_nsobject<LocationBarImageRep> imageRep(
[[LocationBarImageRep alloc]
initWithDrawSelector:@selector(drawLocationBarIcon:)
delegate:[LocationBarImageRep class]];
delegate:[LocationBarImageRep class]]);
[imageRep setIconId:vectorIconId];
[imageRep setFillColor:skia::SkColorToSRGBNSColor(vectorIconColor)];
......
......@@ -359,10 +359,10 @@ CGFloat LineWidthFromContext(CGContextRef context) {
NOTREACHED();
}
base::scoped_nsobject<NewTabButtonCustomImageRep> imageRep =
base::scoped_nsobject<NewTabButtonCustomImageRep> imageRep(
[[NewTabButtonCustomImageRep alloc]
initWithDrawSelector:@selector(drawNewTabButtonImage:)
delegate:[NewTabButton class]];
delegate:[NewTabButton class]]);
[imageRep setDestView:self];
[imageRep setFillColor:fillColor];
[imageRep setPatternPhasePosition:
......
......@@ -128,8 +128,8 @@ constexpr CGFloat kMaxHeightAccounts = 3.5;
kDesiredBubbleWidth,
std::min<CGFloat>([credentialButtons_ count], kMaxHeightAccounts) *
kCredentialHeight);
base::scoped_nsobject<NSScrollView> scrollView = [[NSScrollView alloc]
initWithFrame:NSMakeRect(0, 0, buttonsSize.width, buttonsSize.height)];
base::scoped_nsobject<NSScrollView> scrollView([[NSScrollView alloc]
initWithFrame:NSMakeRect(0, 0, buttonsSize.width, buttonsSize.height)]);
[scrollView
setHasVerticalScroller:[credentialButtons_ count] > kMaxHeightAccounts
? YES
......
......@@ -91,10 +91,9 @@ NSImage* imageForResourceID(int resource_id) {
}
DCHECK(theSelector);
base::scoped_nsobject<NSCustomImageRep> imageRep =
[[NSCustomImageRep alloc]
initWithDrawSelector:theSelector
delegate:[TabView class]];
base::scoped_nsobject<NSCustomImageRep> imageRep([[NSCustomImageRep alloc]
initWithDrawSelector:theSelector
delegate:[TabView class]]);
NSImage* newTabButtonImage =
[[[NSImage alloc] initWithSize:NSMakeSize(imageWidth, 29)] autorelease];
......
......@@ -231,10 +231,10 @@ const NSSize kMDButtonIconSize = NSMakeSize(16, 16);
- (NSImage*)browserToolsIconForFillColor:(SkColor)fillColor {
// Create a |BrowserToolsImageRep| to draw the browser tools icon using
// the provided fill color.
base::scoped_nsobject<BrowserToolsImageRep> imageRep =
[[BrowserToolsImageRep alloc]
initWithDrawSelector:@selector(drawBrowserToolsIcon:)
delegate:[BrowserToolsImageRep class]];
base::scoped_nsobject<BrowserToolsImageRep> imageRep(
[[BrowserToolsImageRep alloc]
initWithDrawSelector:@selector(drawBrowserToolsIcon:)
delegate:[BrowserToolsImageRep class]]);
if (!ui::MaterialDesignController::IsModeMaterial()) {
[imageRep setFillColor:skia::SkColorToCalibratedNSColor(fillColor)];
} else {
......@@ -254,10 +254,10 @@ const NSSize kMDButtonIconSize = NSMakeSize(16, 16);
withBackgroundStyle:(ToolbarButtonImageBackgroundStyle)style {
// Create a |ToolbarButtonImageRep| to draw the button image using
// the provided icon and background style.
base::scoped_nsobject<ToolbarButtonImageRep> imageRep =
base::scoped_nsobject<ToolbarButtonImageRep> imageRep(
[[ToolbarButtonImageRep alloc]
initWithDrawSelector:@selector(drawImage:)
delegate:[ToolbarButtonImageRep class]];
delegate:[ToolbarButtonImageRep class]]);
[imageRep setIcon:iconImage];
[imageRep setStyle:style];
......
......@@ -228,7 +228,7 @@ base::scoped_nsobject<NSPasteboardItem> WriteSimplifiedBookmarkTypes(
}
if (!item) {
item = [[NSPasteboardItem alloc] init];
item.reset([[NSPasteboardItem alloc] init]);
}
[item setString:[toplevel_string_data componentsJoinedByString:@"\n"]
......
......@@ -41,18 +41,20 @@ class BluetoothTestMac::ScopedMockCentralManager {
namespace {
NSDictionary* CreateAdvertisementData(NSString* name, NSArray* uuids) {
NSMutableDictionary* advertisement_data =
scoped_nsobject<NSDictionary> CreateAdvertisementData(NSString* name,
NSArray* uuids) {
NSMutableDictionary* advertisement_data(
[NSMutableDictionary dictionaryWithDictionary:@{
CBAdvertisementDataLocalNameKey : name,
CBAdvertisementDataServiceDataKey : @{},
CBAdvertisementDataIsConnectable : @(YES),
}];
}]);
if (uuids) {
[advertisement_data setObject:uuids
forKey:CBAdvertisementDataServiceUUIDsKey];
}
return [advertisement_data retain];
return scoped_nsobject<NSDictionary>(advertisement_data,
base::scoped_policy::RETAIN);
}
} // namespace
......@@ -229,7 +231,7 @@ void BluetoothTestMac::SimulateGattServicesDiscovered(
static_cast<BluetoothLowEnergyDeviceMac*>(device);
CBPeripheral* peripheral = device_mac->GetPeripheral();
MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral);
scoped_nsobject<NSMutableArray> services = [[NSMutableArray alloc] init];
scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]);
for (auto uuid : uuids) {
CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())];
[services addObject:cb_service_uuid];
......
......@@ -76,7 +76,7 @@ using base::scoped_nsobject;
- (void)setState:(CBPeripheralState)state {
_state = state;
if (_state == CBPeripheralStateDisconnected) {
_services = nil;
_services.reset();
}
}
......
......@@ -72,10 +72,10 @@ TEST(PasswordControllerTest, SaveOnNonHTMLLandingPage) {
.WillByDefault(testing::Return(browser_state.get()));
auto client = base::WrapUnique(new MockPasswordManagerClient);
MockPasswordManagerClient* weak_client = client.get();
base::scoped_nsobject<PasswordController> passwordController =
base::scoped_nsobject<PasswordController> passwordController(
[[PasswordController alloc] initWithWebState:&web_state
passwordsUiDelegate:nil
client:std::move(client)];
client:std::move(client)]);
// Use a mock LogManager to detect that OnPasswordFormsRendered has been
// called. TODO(crbug.com/598672): this is a hack, we should modularize the
......
......@@ -53,7 +53,7 @@ TEST_F(WebStateDelegateBridgeTest, HandleContextMenu) {
context_menu_params.link_url = GURL("http://www.url.com");
context_menu_params.src_url = GURL("http://www.url.com/image.jpeg");
context_menu_params.referrer_policy = web::ReferrerPolicyOrigin;
context_menu_params.view = [UIView new];
context_menu_params.view.reset([UIView new]);
context_menu_params.location = CGPointMake(5.0, 5.0);
bridge_->HandleContextMenu(nullptr, context_menu_params);
web::ContextMenuParams* result_params = [delegate_ contextMenuParams];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment