[iOS] Cleanup Search Widget, use shared utils

There is now a shared class in ios/chrome/common (somewhere extensions
can access) that does the same thing as CreateSameConstraints. We should
use that instead.

Also, we can get rid of unnecessary @synthesize and method prototypes.

Bug: 938317
Change-Id: I54b7a3b855015c377ebdbb8c0be2d0e47698a811
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1503417
Commit-Queue: Robbie Gibson <rkgibson@google.com>
Reviewed-by: Olivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638064}
diff --git a/ios/chrome/search_widget_extension/BUILD.gn b/ios/chrome/search_widget_extension/BUILD.gn
index 946289a..cb65f7d 100644
--- a/ios/chrome/search_widget_extension/BUILD.gn
+++ b/ios/chrome/search_widget_extension/BUILD.gn
@@ -50,12 +50,12 @@
     "copied_content_view.mm",
     "search_action_view.h",
     "search_action_view.mm",
+    "search_widget_constants.h",
+    "search_widget_constants.mm",
     "search_widget_view.h",
     "search_widget_view.mm",
     "search_widget_view_controller.h",
     "search_widget_view_controller.mm",
-    "ui_util.h",
-    "ui_util.mm",
   ]
 
   deps = [
diff --git a/ios/chrome/search_widget_extension/copied_content_view.mm b/ios/chrome/search_widget_extension/copied_content_view.mm
index a8630c5..59b221e 100644
--- a/ios/chrome/search_widget_extension/copied_content_view.mm
+++ b/ios/chrome/search_widget_extension/copied_content_view.mm
@@ -7,7 +7,8 @@
 #import <NotificationCenter/NotificationCenter.h>
 
 #include "base/logging.h"
-#import "ios/chrome/search_widget_extension/ui_util.h"
+#import "ios/chrome/common/ui_util/constraints_ui_util.h"
+#import "ios/chrome/search_widget_extension/search_widget_constants.h"
 
 #if !defined(__has_feature) || !__has_feature(objc_arc)
 #error "This file requires ARC support."
@@ -41,12 +42,6 @@
 
 @implementation CopiedContentView
 
-@synthesize copiedContentLabel = _copiedContentLabel;
-@synthesize copiedText = _copiedText;
-@synthesize openCopiedContentTitleLabel = _openCopiedContentTitleLabel;
-@synthesize hairlineView = _hairlineView;
-@synthesize copiedButtonView = _copiedButtonView;
-
 - (instancetype)initWithActionTarget:(id)target
                       actionSelector:(SEL)actionSelector {
   DCHECK(target);
@@ -71,8 +66,7 @@
          @[ primaryEffectView, secondaryEffectView ]) {
       [self addSubview:effectView];
       effectView.translatesAutoresizingMaskIntoConstraints = NO;
-      [NSLayoutConstraint
-          activateConstraints:ui_util::CreateSameConstraints(self, effectView)];
+      AddSameConstraints(self, effectView);
       effectView.userInteractionEnabled = NO;
     }
 
@@ -109,26 +103,24 @@
 
       [_copiedButtonView.leadingAnchor
           constraintEqualToAnchor:self.leadingAnchor
-                         constant:ui_util::kContentMargin],
+                         constant:kContentMargin],
       [_copiedButtonView.trailingAnchor
           constraintEqualToAnchor:self.trailingAnchor
-                         constant:-ui_util::kContentMargin],
-      [_copiedButtonView.topAnchor
-          constraintEqualToAnchor:self.topAnchor
-                         constant:ui_util::kContentMargin],
-      [_copiedButtonView.bottomAnchor
-          constraintEqualToAnchor:self.bottomAnchor
-                         constant:-ui_util::kContentMargin],
+                         constant:-kContentMargin],
+      [_copiedButtonView.topAnchor constraintEqualToAnchor:self.topAnchor
+                                                  constant:kContentMargin],
+      [_copiedButtonView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor
+                                                     constant:-kContentMargin],
 
       [_openCopiedContentTitleLabel.topAnchor
           constraintEqualToAnchor:_copiedButtonView.topAnchor
                          constant:kURLButtonMargin],
       [_openCopiedContentTitleLabel.leadingAnchor
           constraintEqualToAnchor:_copiedButtonView.leadingAnchor
-                         constant:ui_util::kContentMargin],
+                         constant:kContentMargin],
       [_openCopiedContentTitleLabel.trailingAnchor
           constraintEqualToAnchor:_copiedButtonView.trailingAnchor
-                         constant:-ui_util::kContentMargin],
+                         constant:-kContentMargin],
 
       [_copiedContentLabel.topAnchor
           constraintEqualToAnchor:_openCopiedContentTitleLabel.bottomAnchor],
diff --git a/ios/chrome/search_widget_extension/search_action_view.mm b/ios/chrome/search_widget_extension/search_action_view.mm
index 2732b74..e5672dc0 100644
--- a/ios/chrome/search_widget_extension/search_action_view.mm
+++ b/ios/chrome/search_widget_extension/search_action_view.mm
@@ -7,7 +7,8 @@
 #import <NotificationCenter/NotificationCenter.h>
 
 #include "base/logging.h"
-#import "ios/chrome/search_widget_extension/ui_util.h"
+#import "ios/chrome/common/ui_util/constraints_ui_util.h"
+#import "ios/chrome/search_widget_extension/search_widget_constants.h"
 
 #if !defined(__has_feature) || !__has_feature(objc_arc)
 #error "This file requires ARC support."
@@ -45,8 +46,7 @@
       [self addSubview:effectView];
       effectView.translatesAutoresizingMaskIntoConstraints = NO;
       effectView.userInteractionEnabled = NO;
-      [NSLayoutConstraint
-          activateConstraints:ui_util::CreateSameConstraints(self, effectView)];
+      AddSameConstraints(self, effectView);
     }
 
     UIView* circleView = [[UIView alloc] initWithFrame:CGRectZero];
@@ -66,12 +66,11 @@
     UIStackView* stack = [[UIStackView alloc]
         initWithArrangedSubviews:@[ circleView, labelView ]];
     stack.axis = UILayoutConstraintAxisVertical;
-    stack.spacing = ui_util::kIconSpacing;
+    stack.spacing = kIconSpacing;
     stack.alignment = UIStackViewAlignmentCenter;
     stack.translatesAutoresizingMaskIntoConstraints = NO;
     [secondaryEffectView.contentView addSubview:stack];
-    [NSLayoutConstraint activateConstraints:ui_util::CreateSameConstraints(
-                                                secondaryEffectView, stack)];
+    AddSameConstraints(secondaryEffectView, stack);
     UIImage* iconImage = [UIImage imageNamed:imageName];
     iconImage =
         [iconImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
diff --git a/ios/chrome/search_widget_extension/search_widget_constants.h b/ios/chrome/search_widget_extension/search_widget_constants.h
new file mode 100644
index 0000000..44db73f0
--- /dev/null
+++ b/ios/chrome/search_widget_extension/search_widget_constants.h
@@ -0,0 +1,14 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef IOS_CHROME_SEARCH_WIDGET_EXTENSION_SEARCH_WIDGET_CONSTANTS_H_
+#define IOS_CHROME_SEARCH_WIDGET_EXTENSION_SEARCH_WIDGET_CONSTANTS_H_
+
+// The spacing to use between action icons.
+extern CGFloat const kIconSpacing;
+
+// The spacing between content and edges.
+extern CGFloat const kContentMargin;
+
+#endif  // IOS_CHROME_SEARCH_WIDGET_EXTENSION_SEARCH_WIDGET_CONSTANTS_H_
diff --git a/ios/chrome/search_widget_extension/search_widget_constants.mm b/ios/chrome/search_widget_extension/search_widget_constants.mm
new file mode 100644
index 0000000..26f301b
--- /dev/null
+++ b/ios/chrome/search_widget_extension/search_widget_constants.mm
@@ -0,0 +1,14 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import <UIKit/UIKit.h>
+
+#include "ios/chrome/search_widget_extension/search_widget_constants.h"
+
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+#error "This file requires ARC support."
+#endif
+
+CGFloat const kIconSpacing = 5;
+CGFloat const kContentMargin = 12;
diff --git a/ios/chrome/search_widget_extension/search_widget_view.mm b/ios/chrome/search_widget_extension/search_widget_view.mm
index ca8c427..f6ad453 100644
--- a/ios/chrome/search_widget_extension/search_widget_view.mm
+++ b/ios/chrome/search_widget_extension/search_widget_view.mm
@@ -6,7 +6,7 @@
 #include "base/logging.h"
 #import "ios/chrome/search_widget_extension/copied_content_view.h"
 #import "ios/chrome/search_widget_extension/search_action_view.h"
-#import "ios/chrome/search_widget_extension/ui_util.h"
+#import "ios/chrome/search_widget_extension/search_widget_constants.h"
 
 #if !defined(__has_feature) || !__has_feature(objc_arc)
 #error "This file requires ARC support."
@@ -124,7 +124,7 @@
       [self.actionsContent
           systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]
           .height;
-  return height + 2 * ui_util::kContentMargin;
+  return height + 2 * kContentMargin;
 }
 
 - (CGFloat)copiedURLSectionHeight {
@@ -169,7 +169,7 @@
   actionsContentStack.axis = UILayoutConstraintAxisHorizontal;
   actionsContentStack.alignment = UIStackViewAlignmentTop;
   actionsContentStack.distribution = UIStackViewDistributionFillEqually;
-  actionsContentStack.spacing = ui_util::kIconSpacing;
+  actionsContentStack.spacing = kIconSpacing;
   actionsContentStack.layoutMargins = UIEdgeInsetsZero;
   actionsContentStack.layoutMarginsRelativeArrangement = YES;
   actionsContentStack.translatesAutoresizingMaskIntoConstraints = NO;
@@ -186,12 +186,12 @@
   NSLayoutConstraint* actionsLeadingConstraint =
       [self.actionsContent.leadingAnchor
           constraintEqualToAnchor:self.actionsSection.leadingAnchor
-                         constant:ui_util::kContentMargin];
+                         constant:kContentMargin];
   actionsLeadingConstraint.priority = UILayoutPriorityDefaultHigh;
   NSLayoutConstraint* actionsTrailingConstraint =
       [self.actionsContent.trailingAnchor
           constraintEqualToAnchor:self.actionsSection.trailingAnchor
-                         constant:-ui_util::kContentMargin];
+                         constant:-kContentMargin];
   actionsTrailingConstraint.priority = UILayoutPriorityDefaultHigh;
 
   [NSLayoutConstraint activateConstraints:@[
diff --git a/ios/chrome/search_widget_extension/search_widget_view_controller.mm b/ios/chrome/search_widget_extension/search_widget_view_controller.mm
index 9e080ed..548eaf3 100644
--- a/ios/chrome/search_widget_extension/search_widget_view_controller.mm
+++ b/ios/chrome/search_widget_extension/search_widget_view_controller.mm
@@ -39,42 +39,10 @@
 @property(nonatomic, assign) BOOL supportsSearchByImage;
 @property(nonatomic, readonly) BOOL copiedContentBehaviorEnabled;
 
-// Updates the widget with latest data from the clipboard. Returns whether any
-// visual updates occurred.
-- (BOOL)updateWidget;
-// Opens the main application with the given |command|.
-- (void)openAppWithCommand:(NSString*)command;
-// Opens the main application with the given |command|, |text|, and |image|.
-- (void)openAppWithCommand:(NSString*)command
-                      text:(NSString*)text
-                 imageData:(NSData*)imageData;
-// Returns the dictionary of commands to pass via user defaults to open the main
-// application for a given |command| and optional |text| and |image|.
-+ (NSDictionary*)dictForCommand:(NSString*)command
-                           text:(NSString*)text
-                      imageData:(NSData*)imageData;
-// Register a display of the widget in the app_group NSUserDefaults.
-// Metrics on the widget usage will be sent (if enabled) on the next Chrome
-// startup.
-- (void)registerWidgetDisplay;
-// Sets the copied content type. |copiedText| should be provided if the content
-// type requires textual data, otherwise it should be nil. Likewise,
-// |copiedImage| should be provided if the content type requires image data.
-// Also saves the data and returns YES if the screen needs updating and NO
-// otherwise.
-- (BOOL)setCopiedContentType:(CopiedContentType)type
-                  copiedText:(NSString*)copiedText
-                 copiedImage:(UIImage*)copiedImage;
-
 @end
 
 @implementation SearchWidgetViewController
 
-@synthesize widgetView = _widgetView;
-@synthesize copiedText = _copiedText;
-@synthesize copiedContentType = _copiedContentType;
-@synthesize clipboardRecentContent = _clipboardRecentContent;
-
 - (instancetype)init {
   self = [super init];
   if (self) {
@@ -132,6 +100,9 @@
   completionHandler([self updateWidget] ? NCUpdateResultNewData
                                         : NCUpdateResultNoData);
 }
+
+// Updates the widget with latest data from the clipboard. Returns whether any
+// visual updates occurred.
 - (BOOL)updateWidget {
   NSUserDefaults* sharedDefaults = app_group::GetGroupUserDefaults();
   NSString* fieldTrialKey =
@@ -271,10 +242,14 @@
 
 #pragma mark - internal
 
+// Opens the main application with the given |command|.
 - (void)openAppWithCommand:(NSString*)command {
   return [self openAppWithCommand:command text:nil imageData:nil];
 }
 
+// Register a display of the widget in the app_group NSUserDefaults.
+// Metrics on the widget usage will be sent (if enabled) on the next Chrome
+// startup.
 - (void)registerWidgetDisplay {
   NSUserDefaults* sharedDefaults = app_group::GetGroupUserDefaults();
   NSInteger numberOfDisplay =
@@ -283,6 +258,7 @@
                       forKey:app_group::kSearchExtensionDisplayCount];
 }
 
+// Opens the main application with the given |command|, |text|, and |image|.
 - (void)openAppWithCommand:(NSString*)command
                       text:(NSString*)text
                  imageData:(NSData*)imageData {
@@ -312,6 +288,8 @@
   [self.extensionContext openURL:openURL completionHandler:nil];
 }
 
+// Returns the dictionary of commands to pass via user defaults to open the main
+// application for a given |command| and optional |text| and |image|.
 + (NSDictionary*)dictForCommand:(NSString*)command
                            text:(NSString*)text
                       imageData:(NSData*)imageData {
@@ -343,6 +321,11 @@
   return baseKeys;
 }
 
+// Sets the copied content type. |copiedText| should be provided if the content
+// type requires textual data, otherwise it should be nil. Likewise,
+// |copiedImage| should be provided if the content type requires image data.
+// Also saves the data and returns YES if the screen needs updating and NO
+// otherwise.
 - (BOOL)setCopiedContentType:(CopiedContentType)type
                   copiedText:(NSString*)copiedText
                  copiedImage:(UIImage*)copiedImage {
diff --git a/ios/chrome/search_widget_extension/ui_util.h b/ios/chrome/search_widget_extension/ui_util.h
deleted file mode 100644
index 9897552..0000000
--- a/ios/chrome/search_widget_extension/ui_util.h
+++ /dev/null
@@ -1,23 +0,0 @@
-// Copyright 2017 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef IOS_CHROME_SEARCH_WIDGET_EXTENSION_UI_UTIL_H_
-#define IOS_CHROME_SEARCH_WIDGET_EXTENSION_UI_UTIL_H_
-
-namespace ui_util {
-
-// The spacing to use between action icons.
-extern CGFloat const kIconSpacing;
-
-// The spacing between content and edges.
-extern CGFloat const kContentMargin;
-
-// Returns constraints to make two views' size and center equal by pinning
-// leading, trailing, top and bottom anchors.
-NSArray<NSLayoutConstraint*>* CreateSameConstraints(UIView* view1,
-                                                    UIView* view2);
-
-}  // namespace ui_util
-
-#endif  // IOS_CHROME_SEARCH_WIDGET_EXTENSION_UI_UTIL_H_
diff --git a/ios/chrome/search_widget_extension/ui_util.mm b/ios/chrome/search_widget_extension/ui_util.mm
deleted file mode 100644
index 1b4dae7..0000000
--- a/ios/chrome/search_widget_extension/ui_util.mm
+++ /dev/null
@@ -1,28 +0,0 @@
-// Copyright 2017 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#import <UIKit/UIKit.h>
-
-#include "ios/chrome/search_widget_extension/ui_util.h"
-
-#if !defined(__has_feature) || !__has_feature(objc_arc)
-#error "This file requires ARC support."
-#endif
-
-namespace ui_util {
-
-CGFloat const kIconSpacing = 5;
-CGFloat const kContentMargin = 12;
-
-NSArray<NSLayoutConstraint*>* CreateSameConstraints(UIView* view1,
-                                                    UIView* view2) {
-  return @[
-    [view1.leadingAnchor constraintEqualToAnchor:view2.leadingAnchor],
-    [view1.trailingAnchor constraintEqualToAnchor:view2.trailingAnchor],
-    [view1.topAnchor constraintEqualToAnchor:view2.topAnchor],
-    [view1.bottomAnchor constraintEqualToAnchor:view2.bottomAnchor]
-  ];
-}
-
-}  // namespace ui_util