OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/context_menu/web_context_menu_coordinator.h " | 5 #import "ios/clean/chrome/browser/ui/context_menu/web_context_menu_coordinator.h " |
6 | 6 |
7 #import <MobileCoreServices/MobileCoreServices.h> | |
8 | |
9 #import "base/strings/sys_string_conversions.h" | |
10 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | |
11 #import "ios/chrome/browser/web_state_list/web_state_list.h" | |
12 #import "ios/chrome/browser/web_state_list/web_state_opener.h" | |
13 #import "ios/clean/chrome/browser/ui/commands/context_menu_commands.h" | |
7 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_mediator.h" | 14 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_mediator.h" |
8 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_view_controller.h " | 15 #import "ios/clean/chrome/browser/ui/context_menu/context_menu_view_controller.h " |
9 #import "ios/shared/chrome/browser/ui/browser_list/browser.h" | 16 #import "ios/shared/chrome/browser/ui/browser_list/browser.h" |
10 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" | 17 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" |
18 #import "ios/web/public/navigation_manager.h" | |
19 #import "ios/web/public/web_state/context_menu_params.h" | |
20 #import "ios/web/public/web_state/web_state.h" | |
21 #import "net/base/mac/url_conversions.h" | |
11 | 22 |
12 #if !defined(__has_feature) || !__has_feature(objc_arc) | 23 #if !defined(__has_feature) || !__has_feature(objc_arc) |
13 #error "This file requires ARC support." | 24 #error "This file requires ARC support." |
14 #endif | 25 #endif |
15 | 26 |
16 @interface WebContextMenuCoordinator () | 27 @interface WebContextMenuCoordinator ()<ContextMenuCommands> |
28 @property(nonatomic) web::ContextMenuParams params; | |
17 @property(nonatomic, strong) ContextMenuMediator* mediator; | 29 @property(nonatomic, strong) ContextMenuMediator* mediator; |
18 @property(nonatomic, strong) ContextMenuViewController* viewController; | 30 @property(nonatomic, strong) ContextMenuViewController* viewController; |
31 | |
32 // Opens |URL| in either the current or a new WebState. | |
33 - (void)openURL:(const GURL&)URL inNewWebState:(BOOL)inNewWebState; | |
34 | |
19 @end | 35 @end |
20 | 36 |
21 @implementation WebContextMenuCoordinator | 37 @implementation WebContextMenuCoordinator |
38 @synthesize params = _params; | |
22 @synthesize viewController = _viewController; | 39 @synthesize viewController = _viewController; |
23 @synthesize mediator = _mediator; | 40 @synthesize mediator = _mediator; |
24 | 41 |
42 - (instancetype)initWithParams:(const web::ContextMenuParams&)params { | |
43 if ((self = [super init])) | |
44 _params = params; | |
45 return self; | |
46 } | |
47 | |
48 #pragma mark - BrowserCoordinator | |
49 | |
25 - (void)start { | 50 - (void)start { |
26 self.viewController = [[ContextMenuViewController alloc] | 51 self.viewController = [[ContextMenuViewController alloc] |
27 initWithDispatcher:static_cast<id>(self.browser->dispatcher())]; | 52 initWithDispatcher:static_cast<id>(self.browser->dispatcher())]; |
53 bool isIncognito = self.browser->browser_state()->IsOffTheRecord(); | |
28 self.mediator = | 54 self.mediator = |
29 [[ContextMenuMediator alloc] initWithConsumer:self.viewController]; | 55 [[ContextMenuMediator alloc] initWithConsumer:self.viewController |
56 contextMenuParams:_params | |
57 isIncognito:isIncognito]; | |
58 [self.browser->dispatcher() | |
59 startDispatchingToTarget:self | |
60 forProtocol:@protocol(ContextMenuCommands)]; | |
30 [super start]; | 61 [super start]; |
31 } | 62 } |
32 | 63 |
64 - (void)stop { | |
65 [self.browser->dispatcher() stopDispatchingToTarget:self]; | |
66 [super stop]; | |
67 } | |
68 | |
69 #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
| |
70 | |
71 - (void)openJavaScriptURL:(const GURL*)URL { | |
72 DCHECK(URL->is_valid()); | |
73 web::WebState* webState = self.browser->web_state_list().GetActiveWebState(); | |
74 DCHECK(webState); | |
75 NSString* scriptString = base::SysUTF8ToNSString(URL->GetContent()); | |
76 base::string16 strippedScriptString = | |
77 base::SysNSStringToUTF16([scriptString stringByRemovingPercentEncoding]); | |
78 webState->ExecuteJavaScript(strippedScriptString); | |
79 [self stop]; | |
80 } | |
81 | |
82 - (void)openURLInNewTab:(const GURL*)URL { | |
83 DCHECK(URL); | |
84 [self openURL:*URL inNewWebState:YES]; | |
85 [self stop]; | |
86 } | |
87 | |
88 - (void)openURLInNewIncognitoTab:(const GURL*)URL { | |
89 // TODO: Implement Incognito mode. | |
90 [self stop]; | |
91 } | |
92 | |
93 - (void)copyURL:(const GURL*)URL { | |
94 DCHECK(URL->is_valid()); | |
95 NSData* plainText = [base::SysUTF8ToNSString(URL->spec()) | |
96 dataUsingEncoding:NSUTF8StringEncoding]; | |
97 NSDictionary* copiedItem = @{ | |
98 (NSString*)kUTTypeURL : net::NSURLWithGURL(*URL), | |
99 (NSString*)kUTTypeUTF8PlainText : plainText, | |
100 }; | |
101 [[UIPasteboard generalPasteboard] setItems:@[ copiedItem ]]; | |
102 [self stop]; | |
103 } | |
104 | |
105 - (void)saveImageAtURL:(const GURL*)imageURL { | |
106 // TODO: Implement save image. | |
107 [self stop]; | |
108 } | |
109 | |
110 - (void)openImageAtURL:(const GURL*)imageURL { | |
111 DCHECK(imageURL); | |
112 [self openURL:*imageURL inNewWebState:NO]; | |
113 [self stop]; | |
114 } | |
115 | |
116 - (void)openImageAtURLInNewTab:(const GURL*)imageURL { | |
117 DCHECK(imageURL); | |
118 [self openURL:*imageURL inNewWebState:YES]; | |
119 [self stop]; | |
120 } | |
121 | |
122 - (void)cancelContextMenu { | |
123 [self stop]; | |
124 } | |
125 | |
126 #pragma mark - | |
127 | |
128 - (void)openURL:(const GURL&)URL inNewWebState:(BOOL)inNewWebState { | |
129 // Create the load parameters. | |
130 DCHECK(URL.is_valid()); | |
131 web::NavigationManager::WebLoadParams loadParams(URL); | |
132 loadParams.transition_type = ui::PageTransition::PAGE_TRANSITION_LINK; | |
133 // Get the WebState in which to perform the load. | |
134 WebStateList& webStateList = self.browser->web_state_list(); | |
135 web::WebState* webState = webStateList.GetActiveWebState(); | |
136 if (inNewWebState) { | |
137 web::WebState::CreateParams params(self.browser->browser_state()); | |
138 std::unique_ptr<web::WebState> newWebState = web::WebState::Create(params); | |
139 WebStateOpener opener(webState, webStateList.active_index()); | |
140 webState = newWebState.get(); | |
141 webStateList.AppendWebState(loadParams.transition_type, | |
142 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
| |
143 } | |
144 webState->GetNavigationManager()->LoadURLWithParams(loadParams); | |
145 } | |
146 | |
33 @end | 147 @end |
OLD | NEW |