Index: ios/clean/chrome/browser/ui/context_menu/web_context_menu_coordinator.mm |
diff --git a/ios/clean/chrome/browser/ui/context_menu/web_context_menu_coordinator.mm b/ios/clean/chrome/browser/ui/context_menu/web_context_menu_coordinator.mm |
index 097c80552457c81497fffa2837335a10dfb56982..34fc5ae3a1d6271b995594f132c4da7ddee5a31b 100644 |
--- a/ios/clean/chrome/browser/ui/context_menu/web_context_menu_coordinator.mm |
+++ b/ios/clean/chrome/browser/ui/context_menu/web_context_menu_coordinator.mm |
@@ -4,30 +4,144 @@ |
#import "ios/clean/chrome/browser/ui/context_menu/web_context_menu_coordinator.h" |
+#import <MobileCoreServices/MobileCoreServices.h> |
+ |
+#import "base/strings/sys_string_conversions.h" |
+#include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
+#import "ios/chrome/browser/web_state_list/web_state_list.h" |
+#import "ios/chrome/browser/web_state_list/web_state_opener.h" |
+#import "ios/clean/chrome/browser/ui/commands/context_menu_commands.h" |
#import "ios/clean/chrome/browser/ui/context_menu/context_menu_mediator.h" |
#import "ios/clean/chrome/browser/ui/context_menu/context_menu_view_controller.h" |
#import "ios/shared/chrome/browser/ui/browser_list/browser.h" |
#import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" |
+#import "ios/web/public/navigation_manager.h" |
+#import "ios/web/public/web_state/context_menu_params.h" |
+#import "ios/web/public/web_state/web_state.h" |
+#import "net/base/mac/url_conversions.h" |
#if !defined(__has_feature) || !__has_feature(objc_arc) |
#error "This file requires ARC support." |
#endif |
-@interface WebContextMenuCoordinator () |
+@interface WebContextMenuCoordinator ()<ContextMenuCommands> |
+@property(nonatomic) web::ContextMenuParams params; |
@property(nonatomic, strong) ContextMenuMediator* mediator; |
@property(nonatomic, strong) ContextMenuViewController* viewController; |
+ |
+// Opens |URL| in either the current or a new WebState. |
+- (void)openURL:(const GURL&)URL inNewWebState:(BOOL)inNewWebState; |
+ |
@end |
@implementation WebContextMenuCoordinator |
+@synthesize params = _params; |
@synthesize viewController = _viewController; |
@synthesize mediator = _mediator; |
+- (instancetype)initWithParams:(const web::ContextMenuParams&)params { |
+ if ((self = [super init])) |
+ _params = params; |
+ return self; |
+} |
+ |
+#pragma mark - BrowserCoordinator |
+ |
- (void)start { |
self.viewController = [[ContextMenuViewController alloc] |
initWithDispatcher:static_cast<id>(self.browser->dispatcher())]; |
+ bool isIncognito = self.browser->browser_state()->IsOffTheRecord(); |
self.mediator = |
- [[ContextMenuMediator alloc] initWithConsumer:self.viewController]; |
+ [[ContextMenuMediator alloc] initWithConsumer:self.viewController |
+ contextMenuParams:_params |
+ isIncognito:isIncognito]; |
+ [self.browser->dispatcher() |
+ startDispatchingToTarget:self |
+ forProtocol:@protocol(ContextMenuCommands)]; |
[super start]; |
} |
+- (void)stop { |
+ [self.browser->dispatcher() stopDispatchingToTarget:self]; |
+ [super stop]; |
+} |
+ |
+#pragma mark - ContextMenuCommands |
marq (ping after 24h)
2017/05/04 09:41:28
Almost all of these aren't the job of the context
kkhorimoto
2017/05/05 05:17:54
I've revamped my approach. Now there's a datasourc
|
+ |
+- (void)openJavaScriptURL:(const GURL*)URL { |
+ DCHECK(URL->is_valid()); |
+ web::WebState* webState = self.browser->web_state_list().GetActiveWebState(); |
+ DCHECK(webState); |
+ NSString* scriptString = base::SysUTF8ToNSString(URL->GetContent()); |
+ base::string16 strippedScriptString = |
+ base::SysNSStringToUTF16([scriptString stringByRemovingPercentEncoding]); |
+ webState->ExecuteJavaScript(strippedScriptString); |
+ [self stop]; |
+} |
+ |
+- (void)openURLInNewTab:(const GURL*)URL { |
+ DCHECK(URL); |
+ [self openURL:*URL inNewWebState:YES]; |
+ [self stop]; |
+} |
+ |
+- (void)openURLInNewIncognitoTab:(const GURL*)URL { |
+ // TODO: Implement Incognito mode. |
+ [self stop]; |
+} |
+ |
+- (void)copyURL:(const GURL*)URL { |
+ DCHECK(URL->is_valid()); |
+ NSData* plainText = [base::SysUTF8ToNSString(URL->spec()) |
+ dataUsingEncoding:NSUTF8StringEncoding]; |
+ NSDictionary* copiedItem = @{ |
+ (NSString*)kUTTypeURL : net::NSURLWithGURL(*URL), |
+ (NSString*)kUTTypeUTF8PlainText : plainText, |
+ }; |
+ [[UIPasteboard generalPasteboard] setItems:@[ copiedItem ]]; |
+ [self stop]; |
+} |
+ |
+- (void)saveImageAtURL:(const GURL*)imageURL { |
+ // TODO: Implement save image. |
+ [self stop]; |
+} |
+ |
+- (void)openImageAtURL:(const GURL*)imageURL { |
+ DCHECK(imageURL); |
+ [self openURL:*imageURL inNewWebState:NO]; |
+ [self stop]; |
+} |
+ |
+- (void)openImageAtURLInNewTab:(const GURL*)imageURL { |
+ DCHECK(imageURL); |
+ [self openURL:*imageURL inNewWebState:YES]; |
+ [self stop]; |
+} |
+ |
+- (void)cancelContextMenu { |
+ [self stop]; |
+} |
+ |
+#pragma mark - |
+ |
+- (void)openURL:(const GURL&)URL inNewWebState:(BOOL)inNewWebState { |
+ // Create the load parameters. |
+ DCHECK(URL.is_valid()); |
+ web::NavigationManager::WebLoadParams loadParams(URL); |
+ loadParams.transition_type = ui::PageTransition::PAGE_TRANSITION_LINK; |
+ // Get the WebState in which to perform the load. |
+ WebStateList& webStateList = self.browser->web_state_list(); |
+ web::WebState* webState = webStateList.GetActiveWebState(); |
+ if (inNewWebState) { |
+ web::WebState::CreateParams params(self.browser->browser_state()); |
+ std::unique_ptr<web::WebState> newWebState = web::WebState::Create(params); |
+ WebStateOpener opener(webState, webStateList.active_index()); |
+ webState = newWebState.get(); |
+ webStateList.AppendWebState(loadParams.transition_type, |
+ std::move(newWebState), opener); |
kkhorimoto
2017/05/04 05:38:01
How do we intend to handle showing links that are
marq (ping after 24h)
2017/05/04 09:41:28
The new tab animation needs to be designed; it's s
kkhorimoto
2017/05/05 05:17:54
Right. In that case, I think my ultimate approach
|
+ } |
+ webState->GetNavigationManager()->LoadURLWithParams(loadParams); |
+} |
+ |
@end |