OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ios/clean/chrome/browser/ui/web_contents/web_coordinator.h" | 5 #import "ios/clean/chrome/browser/ui/web_contents/web_coordinator.h" |
6 | 6 |
| 7 #include "base/mac/foundation_util.h" |
7 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #import "ios/clean/chrome/browser/ui/commands/context_menu_commands.h" |
| 10 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_context_impl.h" |
8 #import "ios/clean/chrome/browser/ui/context_menu/web_context_menu_coordinator.h
" | 11 #import "ios/clean/chrome/browser/ui/context_menu/web_context_menu_coordinator.h
" |
9 #import "ios/clean/chrome/browser/ui/web_contents/web_contents_mediator.h" | 12 #import "ios/clean/chrome/browser/ui/web_contents/web_contents_mediator.h" |
10 #import "ios/clean/chrome/browser/ui/web_contents/web_contents_view_controller.h
" | 13 #import "ios/clean/chrome/browser/ui/web_contents/web_contents_view_controller.h
" |
11 #import "ios/shared/chrome/browser/ui/browser_list/browser.h" | 14 #import "ios/shared/chrome/browser/ui/browser_list/browser.h" |
| 15 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" |
12 #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator+internal.
h" | 16 #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator+internal.
h" |
| 17 #include "ios/web/public/navigation_manager.h" |
13 #include "ios/web/public/web_state/web_state.h" | 18 #include "ios/web/public/web_state/web_state.h" |
14 #import "ios/web/public/web_state/web_state_delegate_bridge.h" | 19 #import "ios/web/public/web_state/web_state_delegate_bridge.h" |
15 | 20 |
16 #if !defined(__has_feature) || !__has_feature(objc_arc) | 21 #if !defined(__has_feature) || !__has_feature(objc_arc) |
17 #error "This file requires ARC support." | 22 #error "This file requires ARC support." |
18 #endif | 23 #endif |
19 | 24 |
20 @interface WebCoordinator ()<CRWWebStateDelegate> { | 25 @interface WebCoordinator ()<ContextMenuCommands, CRWWebStateDelegate> { |
21 std::unique_ptr<web::WebStateDelegateBridge> _webStateDelegate; | 26 std::unique_ptr<web::WebStateDelegateBridge> _webStateDelegate; |
22 } | 27 } |
23 @property(nonatomic, strong) WebContentsViewController* viewController; | 28 @property(nonatomic, strong) WebContentsViewController* viewController; |
24 @property(nonatomic, strong) WebContentsMediator* mediator; | 29 @property(nonatomic, strong) WebContentsMediator* mediator; |
25 @end | 30 @end |
26 | 31 |
27 @implementation WebCoordinator | 32 @implementation WebCoordinator |
28 @synthesize webState = _webState; | 33 @synthesize webState = _webState; |
29 @synthesize viewController = _viewController; | 34 @synthesize viewController = _viewController; |
30 @synthesize mediator = _mediator; | 35 @synthesize mediator = _mediator; |
(...skipping 19 matching lines...) Expand all Loading... |
50 self.mediator.webStateList = &self.browser->web_state_list(); | 55 self.mediator.webStateList = &self.browser->web_state_list(); |
51 [super start]; | 56 [super start]; |
52 } | 57 } |
53 | 58 |
54 - (void)stop { | 59 - (void)stop { |
55 [super stop]; | 60 [super stop]; |
56 [self.mediator disconnect]; | 61 [self.mediator disconnect]; |
57 } | 62 } |
58 | 63 |
59 - (void)childCoordinatorDidStart:(BrowserCoordinator*)childCoordinator { | 64 - (void)childCoordinatorDidStart:(BrowserCoordinator*)childCoordinator { |
60 DCHECK([childCoordinator isKindOfClass:[WebContextMenuCoordinator class]]); | 65 // Register to receive relevant ContextMenuCommands. |
| 66 if ([childCoordinator isKindOfClass:[WebContextMenuCoordinator class]]) { |
| 67 [self.browser->dispatcher() |
| 68 startDispatchingToTarget:self |
| 69 forSelector:@selector(executeContextMenuScript:)]; |
| 70 [self.browser->dispatcher() |
| 71 startDispatchingToTarget:self |
| 72 forSelector:@selector(openContextMenuImage:)]; |
| 73 } |
61 [self.viewController presentViewController:childCoordinator.viewController | 74 [self.viewController presentViewController:childCoordinator.viewController |
62 animated:YES | 75 animated:YES |
63 completion:nil]; | 76 completion:nil]; |
64 } | 77 } |
65 | 78 |
| 79 - (void)childCoordinatorWillStop:(BrowserCoordinator*)childCoordinator { |
| 80 // Unregister ContextMenuCommands once the UI has been dismissed. |
| 81 if ([childCoordinator isKindOfClass:[WebContextMenuCoordinator class]]) { |
| 82 [self.browser->dispatcher() |
| 83 stopDispatchingForSelector:@selector(executeContextMenuScript:)]; |
| 84 [self.browser->dispatcher() |
| 85 stopDispatchingForSelector:@selector(openContextMenuImage:)]; |
| 86 } |
| 87 } |
| 88 |
| 89 #pragma mark - ContextMenuCommand |
| 90 |
| 91 - (void)executeContextMenuScript:(ContextMenuContext*)context { |
| 92 ContextMenuContextImpl* contextImpl = |
| 93 base::mac::ObjCCastStrict<ContextMenuContextImpl>(context); |
| 94 self.webState->ExecuteJavaScript(contextImpl.script); |
| 95 } |
| 96 |
| 97 - (void)openContextMenuImage:(ContextMenuContext*)context { |
| 98 ContextMenuContextImpl* contextImpl = |
| 99 base::mac::ObjCCastStrict<ContextMenuContextImpl>(context); |
| 100 web::NavigationManager::WebLoadParams loadParams(contextImpl.imageURL); |
| 101 self.webState->GetNavigationManager()->LoadURLWithParams(loadParams); |
| 102 } |
| 103 |
66 #pragma mark - CRWWebStateDelegate | 104 #pragma mark - CRWWebStateDelegate |
67 | 105 |
68 - (BOOL)webState:(web::WebState*)webState | 106 - (BOOL)webState:(web::WebState*)webState |
69 handleContextMenu:(const web::ContextMenuParams&)params { | 107 handleContextMenu:(const web::ContextMenuParams&)params { |
| 108 ContextMenuContextImpl* context = |
| 109 [[ContextMenuContextImpl alloc] initWithParams:params]; |
70 WebContextMenuCoordinator* contextMenu = | 110 WebContextMenuCoordinator* contextMenu = |
71 [[WebContextMenuCoordinator alloc] init]; | 111 [[WebContextMenuCoordinator alloc] initWithContext:context]; |
72 [self addChildCoordinator:contextMenu]; | 112 [self addChildCoordinator:contextMenu]; |
73 [contextMenu start]; | 113 [contextMenu start]; |
74 return YES; | 114 return YES; |
75 } | 115 } |
76 | 116 |
77 @end | 117 @end |
OLD | NEW |