Commit 7375ccb2 authored by edchin's avatar edchin Committed by Commit Bot

[ios] Add utils for drag and drop

This CL adds utils for creating UIDragItems.
Two types of items can be dragged from Chrome:
1) A tab (from tab grid, tab strip, and omnibox)
2) An URL (from bookmarks, history, recent tabs, and
reading list)

This CL is one in a series to add drag and drop
functionality in a multi-window world.

A tab drag item has enough information to locate a
tab and move it from one window to another.

These util methods will be used in future CLs to
fulfill drag delegate protocol methods to create
drag items.

Bug: 1087844
Change-Id: Ib56c287e6d8b631d275528430cd7ae1102044642
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2246178
Commit-Queue: edchin <edchin@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#778980}
parent 9aace80c
......@@ -100,7 +100,6 @@ source_set("browser") {
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/complex_tasks",
"//ios/chrome/browser/download",
"//ios/chrome/browser/drag_and_drop",
"//ios/chrome/browser/itunes_urls",
"//ios/chrome/browser/signin:feature_flags",
"//ios/chrome/browser/ssl:feature_flags",
......
......@@ -7,14 +7,19 @@ source_set("drag_and_drop") {
sources = [
"drag_and_drop_flag.h",
"drag_and_drop_flag.mm",
"drag_item_util.h",
"drag_item_util.mm",
"drop_and_navigate_delegate.h",
"drop_and_navigate_interaction.h",
"drop_and_navigate_interaction.mm",
]
deps = [
"//base",
"//net:net",
"//url:url",
"//ios/chrome/browser/web:tab_id_tab_helper",
"//ios/chrome/browser/window_activities",
"//ios/web/public",
"//net",
"//url",
]
libs = [ "UIKit.framework" ]
}
......
// Copyright 2020 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_BROWSER_DRAG_AND_DROP_DRAG_ITEM_UTIL_H_
#define IOS_CHROME_BROWSER_DRAG_AND_DROP_DRAG_ITEM_UTIL_H_
#import <UIKit/UIKit.h>
#import "ios/chrome/browser/window_activities/window_activity_helpers.h"
class GURL;
namespace web {
class WebState;
}
// Information that allows the receiver to locate a tab and also to decide
// whether to allow a drop.
@interface TabInfo : NSObject
// The unique identifier of the tab.
@property(nonatomic, strong, readonly) NSString* tabID;
// If YES, the tab is currently in an incognito profile.
@property(nonatomic, assign, readonly) BOOL incognito;
// Default initializer.
- (instancetype)initWithTabID:(NSString*)tabID incognito:(BOOL)incognito;
- (instancetype)init NS_UNAVAILABLE;
@end
// Creates a drag item that encapsulates a tab and a user activity to move the
// tab to a new window.
UIDragItem* CreateTabDragItem(web::WebState* web_state);
// Creates a drag item that encapsulates an URL and a user activity to open the
// URL in a new Chrome window.
UIDragItem* CreateURLDragItem(const GURL& url, WindowActivityOrigin origin);
#endif // IOS_CHROME_BROWSER_DRAG_AND_DROP_DRAG_ITEM_UTIL_H_
// Copyright 2020 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 "ios/chrome/browser/drag_and_drop/drag_item_util.h"
#include "base/check_op.h"
#import "ios/chrome/browser/web/tab_id_tab_helper.h"
#import "ios/chrome/browser/window_activities/window_activity_helpers.h"
#include "ios/web/public/browser_state.h"
#import "ios/web/public/web_state.h"
#include "net/base/mac/url_conversions.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation TabInfo
- (instancetype)initWithTabID:(NSString*)tabID incognito:(BOOL)incognito {
self = [super init];
if (self) {
_tabID = tabID;
_incognito = incognito;
}
return self;
}
@end
UIDragItem* CreateTabDragItem(web::WebState* web_state) {
DCHECK(web_state);
NSURL* url = net::NSURLWithGURL(web_state->GetLastCommittedURL());
NSItemProvider* item_provider = [[NSItemProvider alloc] initWithObject:url];
UIDragItem* drag_item =
[[UIDragItem alloc] initWithItemProvider:item_provider];
NSString* tab_id = TabIdTabHelper::FromWebState(web_state)->tab_id();
// Visibility "all" is required to allow the OS to recognize this activity for
// creating a new window.
[item_provider registerObject:ActivityToMoveTab(tab_id)
visibility:NSItemProviderRepresentationVisibilityAll];
TabInfo* tab_info = [[TabInfo alloc]
initWithTabID:tab_id
incognito:web_state->GetBrowserState()->IsOffTheRecord()];
// Local objects allow synchronous drops, whereas NSItemProvider only allows
// asynchronous drops.
drag_item.localObject = tab_info;
return drag_item;
}
UIDragItem* CreateURLDragItem(const GURL& url, WindowActivityOrigin origin) {
DCHECK(url.is_valid());
NSURL* url_object = net::NSURLWithGURL(url);
NSItemProvider* item_provider =
[[NSItemProvider alloc] initWithObject:url_object];
// Visibility "all" is required to allow the OS to recognize this activity for
// creating a new window.
[item_provider registerObject:ActivityToLoadURL(origin, url)
visibility:NSItemProviderRepresentationVisibilityAll];
UIDragItem* drag_item =
[[UIDragItem alloc] initWithItemProvider:item_provider];
// Local objects allow synchronous drops, whereas NSItemProvider only allows
// asynchronous drops.
drag_item.localObject = url_object;
return drag_item;
}
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