// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/web/public/test/crw_fake_web_state_delegate.h"

#import "ios/web/public/ui/context_menu_params.h"
#import "ios/web/public/web_state.h"

@implementation CRWFakeWebStateDelegate {
  // Backs up the property with the same name.
  std::unique_ptr<web::WebState::OpenURLParams> _openURLParams;
  // Backs up the property with the same name.
  BOOL _javaScriptDialogPresenterRequested;
}

@synthesize webState = _webState;
@synthesize webStateCreationRequested = _webStateCreationRequested;
@synthesize webStateClosingRequested = _webStateClosingRequested;
@synthesize repostFormWarningRequested = _repostFormWarningRequested;
@synthesize copyAllowedRequested = _copyAllowedRequested;
@synthesize pasteAllowedRequested = _pasteAllowedRequested;
@synthesize cutAllowedRequested = _cutAllowedRequested;
@synthesize didFinishClipboardReadRequested = _didFinishClipboardReadRequested;
@synthesize permissionsRequestHandled = _permissionsRequestHandled;
@synthesize httpAuthenticationRequested = _httpAuthenticationRequested;
@synthesize clientCertAuthenticationRequested =
    _clientCertAuthenticationRequested;
@synthesize isAppLaunchingAllowedForWebStateReturnValue =
    _isAppLaunchingAllowedForWebStateReturnValue;

- (web::WebState*)webState:(web::WebState*)webState
    createNewWebStateForURL:(const GURL&)URL
                  openerURL:(const GURL&)openerURL
            initiatedByUser:(BOOL)initiatedByUser {
  _webState = webState;
  _webStateCreationRequested = YES;
  return nil;
}

- (void)closeWebState:(web::WebState*)webState {
  _webState = webState;
  _webStateClosingRequested = YES;
}

- (web::WebState*)webState:(web::WebState*)webState
         openURLWithParams:(const web::WebState::OpenURLParams&)params {
  _webState = webState;
  _openURLParams.reset(new web::WebState::OpenURLParams(params));
  return webState;
}

- (void)webState:(web::WebState*)webState
    runRepostFormDialogWithCompletionHandler:(void (^)(BOOL))handler {
  _webState = webState;
  _repostFormWarningRequested = YES;
}

- (void)webState:(web::WebState*)webState
    shouldAllowCopyWithDecisionHandler:(void (^)(BOOL))handler {
  _webState = webState;
  _copyAllowedRequested = YES;
  handler(YES);
}

- (void)webState:(web::WebState*)webState
    shouldAllowPasteWithDecisionHandler:(void (^)(BOOL))handler {
  _webState = webState;
  _pasteAllowedRequested = YES;
  handler(YES);
}

- (void)webState:(web::WebState*)webState
    shouldAllowCutWithDecisionHandler:(void (^)(BOOL))handler {
  _webState = webState;
  _cutAllowedRequested = YES;
  handler(YES);
}

- (void)webStateDidFinishClipboardRead:(web::WebState*)webState {
  _webState = webState;
  _didFinishClipboardReadRequested = YES;
}

- (web::JavaScriptDialogPresenter*)javaScriptDialogPresenterForWebState:
    (web::WebState*)webState {
  _webState = webState;
  _javaScriptDialogPresenterRequested = YES;
  return nil;
}

- (void)webState:(web::WebState*)webState
    handlePermissions:(NSArray<NSNumber*>*)permissions
      decisionHandler:(web::WebStatePermissionDecisionHandler)decisionHandler {
  _webState = webState;
  _permissionsRequestHandled = YES;
  decisionHandler(web::PermissionDecisionGrant);
}

- (void)webState:(web::WebState*)webState
    didRequestHTTPAuthForProtectionSpace:(NSURLProtectionSpace*)protectionSpace
                      proposedCredential:(NSURLCredential*)proposedCredential
                       completionHandler:(void (^)(NSString* username,
                                                   NSString* password))handler {
  _webState = webState;
  _httpAuthenticationRequested = YES;
}

- (void)webState:(web::WebState*)webState
    didRequestClientCertAuthForProtectionSpace:
        (NSURLProtectionSpace*)protectionSpace
                             completionHandler:
                                 (void (^)(SecIdentityRef))handler {
  _webState = webState;
  _clientCertAuthenticationRequested = YES;
}

- (const web::WebState::OpenURLParams*)openURLParams {
  return _openURLParams.get();
}

- (BOOL)javaScriptDialogPresenterRequested {
  return _javaScriptDialogPresenterRequested;
}

- (BOOL)isAppLaunchingAllowedForWebState:(web::WebState*)webState {
  _webState = webState;
  return _isAppLaunchingAllowedForWebStateReturnValue;
}

@end
