diff options
author | erikchen <erikchen@chromium.org> | 2014-12-11 16:17:38 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-12-12 00:18:30 +0000 |
commit | 600f7962455cb8479168eb2c6678eda05365460a (patch) | |
tree | 0485cd68bf693af4ef9e925ee1e594da813d6463 /chrome/browser/app_controller_mac.mm | |
parent | d01af9d9c924bcca8e965a89eb21e6299b2eb3e0 (diff) | |
download | chromium_src-600f7962455cb8479168eb2c6678eda05365460a.zip chromium_src-600f7962455cb8479168eb2c6678eda05365460a.tar.gz chromium_src-600f7962455cb8479168eb2c6678eda05365460a.tar.bz2 |
Reland 1: "mac: Allow Chrome to hand off its active URL to other devices."
The original CL used instance variables in a class extension and automatic
generation of ivars for synthesized properties, features only available on
64-bit builds. The Mac Memory bots are still compiling Chromium in 32-bits.
This reland removes the usage of those features.
> This CL adds the class HandoffManager, which is responsible for interfacing
> with Apple's Handoff APIs. It takes a GURL, and exposes that GURL to Handoff.
>
> This CL adds the class ActiveWebContentsObserver, which is responsible for
> listening to changes to the active browser, the active tab, and the visible
> URL. It notifies its delegate when any of this state might have changed.
>
> AppControllerMac is the delegate of ActiveWebContentsObserver, as well as the
> owner of the HandoffManager. When it receives a delegate callback, it passes an
> updated GURL to the HandoffManager. There is some minimal logic in
> AppControllerMac that prevents URLs from incognito windows from being passed to
> the HandoffManager.
>
> BUG=431051, 438823
> Committed: https://crrev.com/708abc5b0abb5e0916d779bf6d1342fd472a2aa1
> Cr-Commit-Position: refs/heads/master@{#307846}
BUG=431051, 438823
TBR=sky, erikwright, mmenke, avi
Review URL: https://codereview.chromium.org/794853004
Cr-Commit-Position: refs/heads/master@{#308005}
Diffstat (limited to 'chrome/browser/app_controller_mac.mm')
-rw-r--r-- | chrome/browser/app_controller_mac.mm | 82 |
1 files changed, 80 insertions, 2 deletions
diff --git a/chrome/browser/app_controller_mac.mm b/chrome/browser/app_controller_mac.mm index 2e2dfe5..604f217 100644 --- a/chrome/browser/app_controller_mac.mm +++ b/chrome/browser/app_controller_mac.mm @@ -63,6 +63,7 @@ #import "chrome/browser/ui/cocoa/confirm_quit.h" #import "chrome/browser/ui/cocoa/confirm_quit_panel_controller.h" #import "chrome/browser/ui/cocoa/encoding_menu_controller_delegate_mac.h" +#include "chrome/browser/ui/cocoa/handoff_active_url_observer_bridge.h" #import "chrome/browser/ui/cocoa/history_menu_bridge.h" #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h" #import "chrome/browser/ui/cocoa/profiles/profile_menu_controller.h" @@ -83,6 +84,7 @@ #include "chrome/common/url_constants.h" #include "chrome/grit/chromium_strings.h" #include "chrome/grit/generated_resources.h" +#include "components/handoff/handoff_manager.h" #include "components/handoff/handoff_utility.h" #include "components/signin/core/browser/signin_manager.h" #include "components/signin/core/common/profile_management_switches.h" @@ -209,9 +211,10 @@ bool IsProfileSignedOut(Profile* profile) { return cache.ProfileIsSigninRequiredAtIndex(profile_index); } -} // anonymous namespace +} // namespace + +@interface AppController () <HandoffActiveURLObserverBridgeDelegate> -@interface AppController (Private) - (void)initMenuState; - (void)initProfileMenu; - (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item; @@ -240,6 +243,25 @@ bool IsProfileSignedOut(Profile* profile) { // this method is called, and that tab is the NTP, then this method closes the // NTP after all the |urls| have been opened. - (void)openUrlsReplacingNTP:(const std::vector<GURL>&)urls; + +// Whether instances of this class should use the Handoff feature. +- (BOOL)shouldUseHandoff; + +// This method passes |handoffURL| to |handoffManager_|. +- (void)passURLToHandoffManager:(const GURL&)handoffURL; + +// Lazily creates the Handoff Manager. Updates the state of the Handoff +// Manager. This method is idempotent. This should be called: +// - During initialization. +// - When the current tab navigates to a new URL. +// - When the active browser changes. +// - When the active browser's active tab switches. +// |webContents| should be the new, active WebContents. +- (void)updateHandoffManager:(content::WebContents*)webContents; + +// Given |webContents|, extracts a GURL to be used for Handoff. This may return +// the empty GURL. +- (GURL)handoffURLFromWebContents:(content::WebContents*)webContents; @end class AppControllerProfileObserver : public ProfileInfoCacheObserver { @@ -776,6 +798,12 @@ class AppControllerProfileObserver : public ProfileInfoCacheObserver { startupComplete_ = YES; + Browser* browser = + FindLastActiveWithHostDesktopType(chrome::HOST_DESKTOP_TYPE_NATIVE); + content::WebContents* activeWebContents = nullptr; + if (browser) + activeWebContents = browser->tab_strip_model()->GetActiveWebContents(); + [self updateHandoffManager:activeWebContents]; [self openStartupUrls]; PrefService* localState = g_browser_process->local_state(); @@ -786,6 +814,9 @@ class AppControllerProfileObserver : public ProfileInfoCacheObserver { base::Bind(&chrome::BrowserCommandController::UpdateOpenFileState, menuState_.get())); } + + handoff_active_url_observer_bridge_.reset( + new HandoffActiveURLObserverBridge(self)); } // This is called after profiles have been loaded and preferences registered. @@ -1638,6 +1669,53 @@ class AppControllerProfileObserver : public ProfileInfoCacheObserver { error:(NSError*)error { } +#pragma mark - Handoff Manager + +- (BOOL)shouldUseHandoff { + return base::mac::IsOSYosemiteOrLater(); +} + +- (void)passURLToHandoffManager:(const GURL&)handoffURL { + [handoffManager_ updateActiveURL:handoffURL]; +} + +- (void)updateHandoffManager:(content::WebContents*)webContents { + if (![self shouldUseHandoff]) + return; + + if (!handoffManager_) + handoffManager_.reset([[HandoffManager alloc] init]); + + GURL handoffURL = [self handoffURLFromWebContents:webContents]; + [self passURLToHandoffManager:handoffURL]; +} + +- (GURL)handoffURLFromWebContents:(content::WebContents*)webContents { + if (!webContents) + return GURL(); + + Profile* profile = + Profile::FromBrowserContext(webContents->GetBrowserContext()); + if (!profile) + return GURL(); + + // Handoff is not allowed from an incognito profile. To err on the safe side, + // also disallow Handoff from a guest profile. + if (profile->GetProfileType() != Profile::REGULAR_PROFILE) + return GURL(); + + if (!webContents) + return GURL(); + + return webContents->GetVisibleURL(); +} + +#pragma mark - HandoffActiveURLObserverBridgeDelegate + +- (void)handoffActiveURLChanged:(content::WebContents*)webContents { + [self updateHandoffManager:webContents]; +} + @end // @implementation AppController //--------------------------------------------------------------------------- |