Bug 1363168 - Add support for OSX Share feature. draft
authorDale Harvey <dale@arandomurl.com>
Mon, 12 Mar 2018 09:16:51 +0000
changeset 776636 6b7e15e2f62a442618c30b9e4ccfa5892ce510ca
parent 776635 a51cb0bc55c3dab5e250a3f3fd332bb57b8af112
push id104931
push userbmo:dharvey@mozilla.com
push dateTue, 03 Apr 2018 14:20:56 +0000
bugs1363168
milestone61.0a1
Bug 1363168 - Add support for OSX Share feature. MozReview-Commit-ID: sJXl2If9Ou
browser/base/content/browser-pageActions.js
browser/base/content/browser.xul
browser/locales/en-US/chrome/browser/browser.dtd
browser/modules/PageActions.jsm
browser/themes/shared/icons/share.svg
browser/themes/shared/jar.inc.mn
browser/themes/shared/urlbar-searchbar.inc.css
widget/cocoa/nsMacWebAppUtils.mm
widget/nsIMacWebAppUtils.idl
--- a/browser/base/content/browser-pageActions.js
+++ b/browser/base/content/browser-pageActions.js
@@ -1257,8 +1257,60 @@ BrowserPageActions.addSearchEngine = {
         let prompt = Services.prompt.getPrompt(gBrowser.contentWindow, Ci.nsIPrompt);
         prompt.QueryInterface(Ci.nsIWritablePropertyBag2);
         prompt.setPropertyAsBool("allowTabModal", true);
         prompt.alert(title, text);
       },
     });
   },
 };
+
+// share URL
+BrowserPageActions.shareURL = {
+  onShowingInPanel(buttonNode) {
+    this.redraw = true;
+  },
+
+  onPlacedInPanel(buttonNode) {
+    let action = PageActions.actionForID("shareURL");
+    BrowserPageActions.takeActionTitleFromPanel(action);
+  },
+
+  onShowingSubview(panelViewNode) {
+    // We cache the providers + the UI if the user selects the share
+    // panel multiple times while the panel is open.
+    if (this.redraw === false) {
+      return;
+    }
+
+    let mwaUtils = Cc["@mozilla.org/widget/mac-web-app-utils;1"]
+        .createInstance(Ci.nsIMacWebAppUtils);
+    let currentURI = gBrowser.selectedBrowser.currentURI.spec;
+    let shareProviders = mwaUtils.getSharingProviders(currentURI);
+    let fragment = document.createDocumentFragment();
+
+    shareProviders.forEach(function(share) {
+
+      let item = document.createElement("toolbarbutton");
+      item.setAttribute("label", share.menuItemTitle);
+      item.setAttribute("share-title", share.title);
+      item.setAttribute("image", share.image);
+      item.classList.add("subviewbutton", "subviewbutton-iconic");
+
+      item.addEventListener("command", event => {
+        let shareTitle = event.target.getAttribute("share-title");
+        if (shareTitle) {
+          mwaUtils.shareUrl(shareTitle, currentURI);
+          PanelMultiView.hidePopup(BrowserPageActions.panelNode);
+        }
+      });
+
+      fragment.appendChild(item);
+    });
+
+    let bodyNode = panelViewNode.querySelector(".panel-subview-body");
+    while (bodyNode.firstChild) {
+      bodyNode.firstChild.remove();
+    }
+    bodyNode.appendChild(fragment);
+    this.redraw = false;
+  }
+};
--- a/browser/base/content/browser.xul
+++ b/browser/base/content/browser.xul
@@ -437,17 +437,18 @@
            flip="slide"
            photon="true"
            position="bottomcenter topright"
            tabspecific="true"
            noautofocus="true"
            copyURL-title="&pageAction.copyLink.label;"
            emailLink-title="&emailPageCmd.label;"
            sendToDevice-title="&pageAction.sendTabToDevice.label;"
-           sendToDevice-notReadyTitle="&sendToDevice.syncNotReady.label;">
+           sendToDevice-notReadyTitle="&sendToDevice.syncNotReady.label;"
+           shareURL-title="&pageAction.shareUrl.label;">
       <panelmultiview id="pageActionPanelMultiView"
                       mainViewId="pageActionPanelMainView"
                       viewCacheId="appMenu-viewCache">
         <panelview id="pageActionPanelMainView"
                    context="pageActionContextMenu"
                    class="PanelUI-subView">
           <vbox class="panel-subview-body"/>
         </panelview>
--- a/browser/locales/en-US/chrome/browser/browser.dtd
+++ b/browser/locales/en-US/chrome/browser/browser.dtd
@@ -985,16 +985,18 @@ you can use these alternative items. Oth
 <!ENTITY pageAction.removeFromUrlbar.label "Remove from Address Bar">
 <!ENTITY pageAction.allowInUrlbar.label "Show in Address Bar">
 <!ENTITY pageAction.disallowInUrlbar.label "Don’t Show in Address Bar">
 <!ENTITY pageAction.manageExtension.label "Manage Extension…">
 
 <!ENTITY pageAction.sendTabToDevice.label "Send Tab to Device">
 <!ENTITY sendToDevice.syncNotReady.label "Syncing Devices…">
 
+<!ENTITY pageAction.shareUrl.label "Share">
+
 <!ENTITY libraryButton.tooltip "View history, saved bookmarks, and more">
 
 <!-- LOCALIZATION NOTE: (accessibilityIndicator.tooltip): This is used to
      display a tooltip for accessibility indicator in toolbar/tabbar. It is also
      used as a textual label for the indicator used by assistive technology
      users. -->
 <!ENTITY accessibilityIndicator.tooltip "Accessibility Features Enabled">
 
--- a/browser/modules/PageActions.jsm
+++ b/browser/modules/PageActions.jsm
@@ -1162,26 +1162,48 @@ if (Services.prefs.getBoolPref("identity
     },
     onSubviewShowing(panelViewNode) {
       browserPageActions(panelViewNode).sendToDevice
         .onShowingSubview(panelViewNode);
     },
   });
 }
 
+if (AppConstants.platform == "macosx") {
+  gBuiltInActions.push(
+  // Share URL
+  {
+    id: "shareURL",
+    title: "shareURL-title",
+    onShowingInPanel(buttonNode) {
+      browserPageActions(buttonNode).shareURL.onShowingInPanel(buttonNode);
+    },
+    onPlacedInPanel(buttonNode) {
+      browserPageActions(buttonNode).shareURL.onPlacedInPanel(buttonNode);
+    },
+    wantsSubview: true,
+    onSubviewShowing(panelViewNode) {
+        browserPageActions(panelViewNode).shareURL
+          .onShowingSubview(panelViewNode);
+    },
+  });
+}
 
 /**
  * Gets a BrowserPageActions object in a browser window.
  *
  * @param  obj
  *         Either a DOM node or a browser window.
  * @return The BrowserPageActions object in the browser window related to the
  *         given object.
  */
 function browserPageActions(obj) {
+  if (!obj) {
+    console.trace();
+  }
   if (obj.BrowserPageActions) {
     return obj.BrowserPageActions;
   }
   return obj.ownerGlobal.BrowserPageActions;
 }
 
 /**
  * A generator function for all open browser windows.
new file mode 100644
--- /dev/null
+++ b/browser/themes/shared/icons/share.svg
@@ -0,0 +1,7 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+   - License, v. 2.0. If a copy of the MPL was not distributed with this
+   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
+  <path fill="context-fill" d="M12.707 4.294l-4-4A1 1 0 0 0 8.38.077a.984.984 0 0 0-.246-.05A.938.938 0 0 0 8 0a.938.938 0 0 0-.134.027.984.984 0 0 0-.246.05A1 1 0 0 0 7.291.3l-4 4a1 1 0 0 0 1.416 1.408L7 3.415V11a1 1 0 0 0 2 0V3.415l2.293 2.293a1 1 0 0 0 1.414-1.414z"></path>
+  <path fill="context-fill" d="M14 9a1 1 0 0 0-1 1v3a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1v-3a1 1 0 0 0-2 0v3a3 3 0 0 0 3 3h8a3 3 0 0 0 3-3v-3a1 1 0 0 0-1-1z"></path>
+</svg>
--- a/browser/themes/shared/jar.inc.mn
+++ b/browser/themes/shared/jar.inc.mn
@@ -157,16 +157,17 @@
   skin/classic/browser/restore-session.svg            (../shared/icons/restore-session.svg)
   skin/classic/browser/quit.svg                       (../shared/icons/quit.svg)
   skin/classic/browser/reload.svg                     (../shared/icons/reload.svg)
   skin/classic/browser/reload-to-stop.svg             (../shared/icons/reload-to-stop.svg)
   skin/classic/browser/save.svg                       (../shared/icons/save.svg)
   skin/classic/browser/search-glass.svg               (../shared/icons/search-glass.svg)
   skin/classic/browser/send-to-device.svg             (../shared/icons/send-to-device.svg)
   skin/classic/browser/settings.svg                   (../shared/icons/settings.svg)
+  skin/classic/browser/share.svg                      (../shared/icons/share.svg)
   skin/classic/browser/sidebars.svg                   (../shared/icons/sidebars.svg)
   skin/classic/browser/sidebars-right.svg             (../shared/icons/sidebars-right.svg)
   skin/classic/browser/stop.svg                       (../shared/icons/stop.svg)
   skin/classic/browser/stop-to-reload.svg             (../shared/icons/stop-to-reload.svg)
   skin/classic/browser/sync.svg                       (../shared/icons/sync.svg)
   skin/classic/browser/tab.svg                        (../shared/icons/tab.svg)
   skin/classic/browser/bookmarks-toolbar.svg          (../shared/icons/bookmarks-toolbar.svg)
   skin/classic/browser/webIDE.svg                     (../shared/icons/webIDE.svg)
--- a/browser/themes/shared/urlbar-searchbar.inc.css
+++ b/browser/themes/shared/urlbar-searchbar.inc.css
@@ -164,16 +164,20 @@
 }
 
 .pageAction-sendToDevice-device.signintosync,
 #pageAction-panel-sendToDevice-fxa,
 #pageAction-urlbar-sendToDevice-fxa {
   list-style-image: url("chrome://browser/skin/sync.svg");
 }
 
+#pageAction-panel-shareURL {
+  list-style-image: url("chrome://browser/skin/share.svg");
+}
+
 #pageAction-panel-addSearchEngine > .toolbarbutton-badge-stack > .toolbarbutton-icon {
   width: 16px;
   height: 16px;
 }
 #pageAction-panel-addSearchEngine > .toolbarbutton-badge-stack > .toolbarbutton-badge {
   display: -moz-box;
   background: url(chrome://browser/skin/search-indicator-badge-add.svg) no-repeat center;
   box-shadow: none;
--- a/widget/cocoa/nsMacWebAppUtils.mm
+++ b/widget/cocoa/nsMacWebAppUtils.mm
@@ -4,16 +4,18 @@
 
 #import <Cocoa/Cocoa.h>
 
 #include "nsMacWebAppUtils.h"
 #include "nsCOMPtr.h"
 #include "nsCocoaUtils.h"
 #include "nsString.h"
 
+#include "mozilla/MacStringHelpers.h"
+
 // This must be included last:
 #include "nsObjCExceptions.h"
 
 // Find the path to the app with the given bundleIdentifier, if any.
 // Note that the OS will return the path to the newest binary, if there is more than one.
 // The determination of 'newest' is complex and beyond the scope of this comment.
 
 NS_IMPL_ISUPPORTS(nsMacWebAppUtils, nsIMacWebAppUtils)
@@ -75,8 +77,77 @@ NS_IMETHODIMP nsMacWebAppUtils::TrashApp
       nsresult rv = (error == nil) ? NS_OK : NS_ERROR_FAILURE;
       callback->TrashAppFinished(rv);
     }];
 
   return NS_OK;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
 }
+
+NSString* NSImageToBase64(const NSImage* image) {
+  [image lockFocus];
+  NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, image.size.width, image.size.height)];
+  [image unlockFocus];
+  NSData *imageData = [bitmapRep representationUsingType:NSPNGFileType properties:@{}];
+  NSString *base64Encoded = [imageData base64EncodedStringWithOptions:0];
+  return [NSString stringWithFormat: @"data:image/png;base64,%@", base64Encoded];
+}
+
+NSString* nsAtoNSString(const nsAString& str) {
+  return [NSString stringWithCharacters:reinterpret_cast<const unichar*>(str.BeginReading())
+                                 length:str.Length()];
+}
+
+void setStrAttribute(JSContext* aCx, JS::Rooted<JSObject*>& obj, const char* key, NSString* val) {
+  nsAutoString strVal;
+  mozilla::CopyCocoaStringToXPCOMString(val, strVal);
+  JS::Rooted<JSString*> title(aCx, JS_NewUCStringCopyZ(aCx, strVal.get()));
+  JS::Rooted<JS::Value> attVal(aCx, JS::StringValue(title));
+  JS_SetProperty(aCx, obj, key, attVal);
+}
+
+nsresult nsMacWebAppUtils::GetSharingProviders(const nsAString& urlToShare, JSContext* aCx, JS::MutableHandleValue aResult) {
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  JS::Rooted<JSObject*> array(aCx, JS_NewArrayObject(aCx, 0));
+  NSURL *url = [NSURL URLWithString:nsAtoNSString(urlToShare)];
+
+  NSArray *sharingService = [NSSharingService sharingServicesForItems:[NSArray arrayWithObject:url]];
+  int32_t serviceCount = 0;
+
+  for (NSSharingService *currentService in sharingService) {
+
+    JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
+
+    setStrAttribute(aCx, obj, "title", currentService.title);
+    setStrAttribute(aCx, obj, "menuItemTitle", currentService.menuItemTitle);
+    setStrAttribute(aCx, obj, "image", NSImageToBase64(currentService.image));
+
+    JS::Rooted<JS::Value> element(aCx, JS::ObjectValue(*obj));
+    JS_SetElement(aCx, array, serviceCount++, element);
+  }
+
+  aResult.setObject(*array);
+
+  return NS_OK;
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
+
+NS_IMETHODIMP nsMacWebAppUtils::ShareUrl(const nsAString& shareTitle, const nsAString& urlToShare) {
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
+  NSString* titleString = nsAtoNSString(shareTitle);
+  NSURL *url = [NSURL URLWithString:nsAtoNSString(urlToShare)];
+
+  NSArray *sharingService = [NSSharingService sharingServicesForItems:[NSArray arrayWithObject:url]];
+
+  for (NSSharingService *currentService in sharingService) {
+    if ([currentService.title isEqualToString:titleString]) {
+      [currentService performWithItems:@[url]];
+      break;
+    }
+  }
+
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
--- a/widget/nsIMacWebAppUtils.idl
+++ b/widget/nsIMacWebAppUtils.idl
@@ -27,9 +27,19 @@ interface nsIMacWebAppUtils : nsISupport
    * Launch the app with the given identifier, if it exists.
    */
   void launchAppWithIdentifier(in AString bundleIdentifier);
 
   /**
    * Move the app from the given directory to the Trash.
    */
   void trashApp(in AString path, in nsITrashAppCallback callback);
+
+  /**
+   * Get list of sharing providers
+   */
+  [implicit_jscontext] jsval getSharingProviders(in AString urlToShare);
+
+  /**
+   * Launch service with shareTitle with given url
+   */
+  void shareUrl(in AString shareTitle, in AString urlToShare);
 };