Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: chrome/browser/app_controller_mac.mm

Issue 794853004: Reland 1: "mac: Allow Chrome to hand off its active URL to other devices." (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from avi. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/app_controller_mac.h ('k') | chrome/browser/app_controller_mac_browsertest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "chrome/browser/app_controller_mac.h" 5 #import "chrome/browser/app_controller_mac.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 #include "chrome/browser/ui/browser_window.h" 56 #include "chrome/browser/ui/browser_window.h"
57 #include "chrome/browser/ui/chrome_pages.h" 57 #include "chrome/browser/ui/chrome_pages.h"
58 #import "chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.h" 58 #import "chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.h"
59 #include "chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.h" 59 #include "chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.h"
60 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h" 60 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
61 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h" 61 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
62 #import "chrome/browser/ui/cocoa/browser_window_controller.h" 62 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
63 #import "chrome/browser/ui/cocoa/confirm_quit.h" 63 #import "chrome/browser/ui/cocoa/confirm_quit.h"
64 #import "chrome/browser/ui/cocoa/confirm_quit_panel_controller.h" 64 #import "chrome/browser/ui/cocoa/confirm_quit_panel_controller.h"
65 #import "chrome/browser/ui/cocoa/encoding_menu_controller_delegate_mac.h" 65 #import "chrome/browser/ui/cocoa/encoding_menu_controller_delegate_mac.h"
66 #include "chrome/browser/ui/cocoa/handoff_active_url_observer_bridge.h"
66 #import "chrome/browser/ui/cocoa/history_menu_bridge.h" 67 #import "chrome/browser/ui/cocoa/history_menu_bridge.h"
67 #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h" 68 #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h"
68 #import "chrome/browser/ui/cocoa/profiles/profile_menu_controller.h" 69 #import "chrome/browser/ui/cocoa/profiles/profile_menu_controller.h"
69 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" 70 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
70 #import "chrome/browser/ui/cocoa/tabs/tab_window_controller.h" 71 #import "chrome/browser/ui/cocoa/tabs/tab_window_controller.h"
71 #include "chrome/browser/ui/cocoa/task_manager_mac.h" 72 #include "chrome/browser/ui/cocoa/task_manager_mac.h"
72 #include "chrome/browser/ui/extensions/application_launch.h" 73 #include "chrome/browser/ui/extensions/application_launch.h"
73 #include "chrome/browser/ui/host_desktop.h" 74 #include "chrome/browser/ui/host_desktop.h"
74 #include "chrome/browser/ui/startup/startup_browser_creator.h" 75 #include "chrome/browser/ui/startup/startup_browser_creator.h"
75 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h" 76 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
76 #include "chrome/browser/ui/user_manager.h" 77 #include "chrome/browser/ui/user_manager.h"
77 #include "chrome/common/chrome_paths_internal.h" 78 #include "chrome/common/chrome_paths_internal.h"
78 #include "chrome/common/chrome_switches.h" 79 #include "chrome/common/chrome_switches.h"
79 #include "chrome/common/cloud_print/cloud_print_class_mac.h" 80 #include "chrome/common/cloud_print/cloud_print_class_mac.h"
80 #include "chrome/common/extensions/extension_constants.h" 81 #include "chrome/common/extensions/extension_constants.h"
81 #include "chrome/common/mac/app_mode_common.h" 82 #include "chrome/common/mac/app_mode_common.h"
82 #include "chrome/common/pref_names.h" 83 #include "chrome/common/pref_names.h"
83 #include "chrome/common/url_constants.h" 84 #include "chrome/common/url_constants.h"
84 #include "chrome/grit/chromium_strings.h" 85 #include "chrome/grit/chromium_strings.h"
85 #include "chrome/grit/generated_resources.h" 86 #include "chrome/grit/generated_resources.h"
87 #include "components/handoff/handoff_manager.h"
86 #include "components/handoff/handoff_utility.h" 88 #include "components/handoff/handoff_utility.h"
87 #include "components/signin/core/browser/signin_manager.h" 89 #include "components/signin/core/browser/signin_manager.h"
88 #include "components/signin/core/common/profile_management_switches.h" 90 #include "components/signin/core/common/profile_management_switches.h"
89 #include "content/public/browser/browser_thread.h" 91 #include "content/public/browser/browser_thread.h"
90 #include "content/public/browser/download_manager.h" 92 #include "content/public/browser/download_manager.h"
91 #include "content/public/browser/notification_service.h" 93 #include "content/public/browser/notification_service.h"
92 #include "content/public/browser/notification_types.h" 94 #include "content/public/browser/notification_types.h"
93 #include "content/public/browser/plugin_service.h" 95 #include "content/public/browser/plugin_service.h"
94 #include "content/public/browser/user_metrics.h" 96 #include "content/public/browser/user_metrics.h"
95 #include "extensions/browser/extension_system.h" 97 #include "extensions/browser/extension_system.h"
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 if (!switches::IsNewProfileManagement()) 204 if (!switches::IsNewProfileManagement())
203 return false; 205 return false;
204 ProfileInfoCache& cache = 206 ProfileInfoCache& cache =
205 g_browser_process->profile_manager()->GetProfileInfoCache(); 207 g_browser_process->profile_manager()->GetProfileInfoCache();
206 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath()); 208 size_t profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
207 if (profile_index == std::string::npos) 209 if (profile_index == std::string::npos)
208 return false; 210 return false;
209 return cache.ProfileIsSigninRequiredAtIndex(profile_index); 211 return cache.ProfileIsSigninRequiredAtIndex(profile_index);
210 } 212 }
211 213
212 } // anonymous namespace 214 } // namespace
213 215
214 @interface AppController (Private) 216 @interface AppController () <HandoffActiveURLObserverBridgeDelegate>
217
215 - (void)initMenuState; 218 - (void)initMenuState;
216 - (void)initProfileMenu; 219 - (void)initProfileMenu;
217 - (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item; 220 - (void)updateConfirmToQuitPrefMenuItem:(NSMenuItem*)item;
218 - (void)updateDisplayMessageCenterPrefMenuItem:(NSMenuItem*)item; 221 - (void)updateDisplayMessageCenterPrefMenuItem:(NSMenuItem*)item;
219 - (void)registerServicesMenuTypesTo:(NSApplication*)app; 222 - (void)registerServicesMenuTypesTo:(NSApplication*)app;
220 - (void)getUrl:(NSAppleEventDescriptor*)event 223 - (void)getUrl:(NSAppleEventDescriptor*)event
221 withReply:(NSAppleEventDescriptor*)reply; 224 withReply:(NSAppleEventDescriptor*)reply;
222 - (void)windowLayeringDidChange:(NSNotification*)inNotification; 225 - (void)windowLayeringDidChange:(NSNotification*)inNotification;
223 - (void)activeSpaceDidChange:(NSNotification*)inNotification; 226 - (void)activeSpaceDidChange:(NSNotification*)inNotification;
224 - (void)checkForAnyKeyWindows; 227 - (void)checkForAnyKeyWindows;
225 - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount; 228 - (BOOL)userWillWaitForInProgressDownloads:(int)downloadCount;
226 - (BOOL)shouldQuitWithInProgressDownloads; 229 - (BOOL)shouldQuitWithInProgressDownloads;
227 - (void)executeApplication:(id)sender; 230 - (void)executeApplication:(id)sender;
228 - (void)profileWasRemoved:(const base::FilePath&)profilePath; 231 - (void)profileWasRemoved:(const base::FilePath&)profilePath;
229 232
230 // Opens a tab for each GURL in |urls|. 233 // Opens a tab for each GURL in |urls|.
231 - (void)openUrls:(const std::vector<GURL>&)urls; 234 - (void)openUrls:(const std::vector<GURL>&)urls;
232 235
233 // This class cannot open urls until startup has finished. The urls that cannot 236 // This class cannot open urls until startup has finished. The urls that cannot
234 // be opened are cached in |startupUrls_|. This method must be called exactly 237 // be opened are cached in |startupUrls_|. This method must be called exactly
235 // once after startup has completed. It opens the urls in |startupUrls_|, and 238 // once after startup has completed. It opens the urls in |startupUrls_|, and
236 // clears |startupUrls_|. 239 // clears |startupUrls_|.
237 - (void)openStartupUrls; 240 - (void)openStartupUrls;
238 241
239 // Opens a tab for each GURL in |urls|. If there is exactly one tab open before 242 // Opens a tab for each GURL in |urls|. If there is exactly one tab open before
240 // this method is called, and that tab is the NTP, then this method closes the 243 // this method is called, and that tab is the NTP, then this method closes the
241 // NTP after all the |urls| have been opened. 244 // NTP after all the |urls| have been opened.
242 - (void)openUrlsReplacingNTP:(const std::vector<GURL>&)urls; 245 - (void)openUrlsReplacingNTP:(const std::vector<GURL>&)urls;
246
247 // Whether instances of this class should use the Handoff feature.
248 - (BOOL)shouldUseHandoff;
249
250 // This method passes |handoffURL| to |handoffManager_|.
251 - (void)passURLToHandoffManager:(const GURL&)handoffURL;
252
253 // Lazily creates the Handoff Manager. Updates the state of the Handoff
254 // Manager. This method is idempotent. This should be called:
255 // - During initialization.
256 // - When the current tab navigates to a new URL.
257 // - When the active browser changes.
258 // - When the active browser's active tab switches.
259 // |webContents| should be the new, active WebContents.
260 - (void)updateHandoffManager:(content::WebContents*)webContents;
261
262 // Given |webContents|, extracts a GURL to be used for Handoff. This may return
263 // the empty GURL.
264 - (GURL)handoffURLFromWebContents:(content::WebContents*)webContents;
243 @end 265 @end
244 266
245 class AppControllerProfileObserver : public ProfileInfoCacheObserver { 267 class AppControllerProfileObserver : public ProfileInfoCacheObserver {
246 public: 268 public:
247 AppControllerProfileObserver( 269 AppControllerProfileObserver(
248 ProfileManager* profile_manager, AppController* app_controller) 270 ProfileManager* profile_manager, AppController* app_controller)
249 : profile_manager_(profile_manager), 271 : profile_manager_(profile_manager),
250 app_controller_(app_controller) { 272 app_controller_(app_controller) {
251 DCHECK(profile_manager_); 273 DCHECK(profile_manager_);
252 DCHECK(app_controller_); 274 DCHECK(app_controller_);
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 // I/O. 791 // I/O.
770 BrowserThread::PostTask( 792 BrowserThread::PostTask(
771 BrowserThread::FILE, FROM_HERE, 793 BrowserThread::FILE, FROM_HERE,
772 base::Bind(&RecordLastRunAppBundlePath)); 794 base::Bind(&RecordLastRunAppBundlePath));
773 795
774 // Makes "Services" menu items available. 796 // Makes "Services" menu items available.
775 [self registerServicesMenuTypesTo:[notify object]]; 797 [self registerServicesMenuTypesTo:[notify object]];
776 798
777 startupComplete_ = YES; 799 startupComplete_ = YES;
778 800
801 Browser* browser =
802 FindLastActiveWithHostDesktopType(chrome::HOST_DESKTOP_TYPE_NATIVE);
803 content::WebContents* activeWebContents = nullptr;
804 if (browser)
805 activeWebContents = browser->tab_strip_model()->GetActiveWebContents();
806 [self updateHandoffManager:activeWebContents];
779 [self openStartupUrls]; 807 [self openStartupUrls];
780 808
781 PrefService* localState = g_browser_process->local_state(); 809 PrefService* localState = g_browser_process->local_state();
782 if (localState) { 810 if (localState) {
783 localPrefRegistrar_.Init(localState); 811 localPrefRegistrar_.Init(localState);
784 localPrefRegistrar_.Add( 812 localPrefRegistrar_.Add(
785 prefs::kAllowFileSelectionDialogs, 813 prefs::kAllowFileSelectionDialogs,
786 base::Bind(&chrome::BrowserCommandController::UpdateOpenFileState, 814 base::Bind(&chrome::BrowserCommandController::UpdateOpenFileState,
787 menuState_.get())); 815 menuState_.get()));
788 } 816 }
817
818 handoff_active_url_observer_bridge_.reset(
819 new HandoffActiveURLObserverBridge(self));
789 } 820 }
790 821
791 // This is called after profiles have been loaded and preferences registered. 822 // This is called after profiles have been loaded and preferences registered.
792 // It is safe to access the default profile here. 823 // It is safe to access the default profile here.
793 - (void)applicationDidBecomeActive:(NSNotification*)notify { 824 - (void)applicationDidBecomeActive:(NSNotification*)notify {
794 content::PluginService::GetInstance()->AppActivated(); 825 content::PluginService::GetInstance()->AppActivated();
795 } 826 }
796 827
797 // Helper function for populating and displaying the in progress downloads at 828 // Helper function for populating and displaying the in progress downloads at
798 // exit alert panel. 829 // exit alert panel.
(...skipping 832 matching lines...) Expand 10 before | Expand all | Expand 10 after
1631 1662
1632 [self openUrlsReplacingNTP:gurlVector]; 1663 [self openUrlsReplacingNTP:gurlVector];
1633 return YES; 1664 return YES;
1634 } 1665 }
1635 1666
1636 - (void)application:(NSApplication*)application 1667 - (void)application:(NSApplication*)application
1637 didFailToContinueUserActivityWithType:(NSString*)userActivityType 1668 didFailToContinueUserActivityWithType:(NSString*)userActivityType
1638 error:(NSError*)error { 1669 error:(NSError*)error {
1639 } 1670 }
1640 1671
1672 #pragma mark - Handoff Manager
1673
1674 - (BOOL)shouldUseHandoff {
1675 return base::mac::IsOSYosemiteOrLater();
1676 }
1677
1678 - (void)passURLToHandoffManager:(const GURL&)handoffURL {
1679 [handoffManager_ updateActiveURL:handoffURL];
1680 }
1681
1682 - (void)updateHandoffManager:(content::WebContents*)webContents {
1683 if (![self shouldUseHandoff])
1684 return;
1685
1686 if (!handoffManager_)
1687 handoffManager_.reset([[HandoffManager alloc] init]);
1688
1689 GURL handoffURL = [self handoffURLFromWebContents:webContents];
1690 [self passURLToHandoffManager:handoffURL];
1691 }
1692
1693 - (GURL)handoffURLFromWebContents:(content::WebContents*)webContents {
1694 if (!webContents)
1695 return GURL();
1696
1697 Profile* profile =
1698 Profile::FromBrowserContext(webContents->GetBrowserContext());
1699 if (!profile)
1700 return GURL();
1701
1702 // Handoff is not allowed from an incognito profile. To err on the safe side,
1703 // also disallow Handoff from a guest profile.
1704 if (profile->GetProfileType() != Profile::REGULAR_PROFILE)
1705 return GURL();
1706
1707 if (!webContents)
1708 return GURL();
1709
1710 return webContents->GetVisibleURL();
1711 }
1712
1713 #pragma mark - HandoffActiveURLObserverBridgeDelegate
1714
1715 - (void)handoffActiveURLChanged:(content::WebContents*)webContents {
1716 [self updateHandoffManager:webContents];
1717 }
1718
1641 @end // @implementation AppController 1719 @end // @implementation AppController
1642 1720
1643 //--------------------------------------------------------------------------- 1721 //---------------------------------------------------------------------------
1644 1722
1645 namespace app_controller_mac { 1723 namespace app_controller_mac {
1646 1724
1647 bool IsOpeningNewWindow() { 1725 bool IsOpeningNewWindow() {
1648 return g_is_opening_new_window; 1726 return g_is_opening_new_window;
1649 } 1727 }
1650 1728
1651 } // namespace app_controller_mac 1729 } // namespace app_controller_mac
OLDNEW
« no previous file with comments | « chrome/browser/app_controller_mac.h ('k') | chrome/browser/app_controller_mac_browsertest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698