Skip to content

Commit

Permalink
expose canTakeSnapshot, adjust comments, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson committed Feb 27, 2025
1 parent 128c168 commit b3ed162
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 32 deletions.
3 changes: 3 additions & 0 deletions chromium_src/ios/web/public/browser_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef BRAVE_CHROMIUM_SRC_IOS_WEB_PUBLIC_BROWSER_STATE_H_
#define BRAVE_CHROMIUM_SRC_IOS_WEB_PUBLIC_BROWSER_STATE_H_

#include "base/notreached.h"

class PrefService;

// This override is tied to the replaced implementation of `//ios/web_view`'s
Expand All @@ -22,6 +24,7 @@ class PrefService;
#define GetURLLoaderFactory() \
GetURLLoaderFactory(); \
PrefService* GetPrefs() { \
NOTREACHED(); \
return nullptr; \
} \
void Unused()
Expand Down
13 changes: 6 additions & 7 deletions chromium_src/ios/web_view/internal/cwv_web_view_configuration.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#import <memory>

#import "base/memory/raw_ptr.h"
#import "base/notimplemented.h"
#import "base/notreached.h"
#import "base/threading/thread_restrictions.h"
#import "components/keyed_service/core/service_access_type.h"
#import "components/keyed_service/ios/browser_state_dependency_manager.h"
Expand All @@ -27,7 +27,6 @@
// we can nop certain services such as access to CWVWebView specific wrappers
// around autofill, sync, etc (since we would use Chrome's) and replace
// the exposed browser state to be a plain `web::BrowserState`

@interface CWVWebViewConfiguration () {
// The BrowserState for this configuration.
raw_ptr<web::BrowserState> _browserState;
Expand All @@ -43,17 +42,17 @@ + (void)shutDown {
}

+ (instancetype)defaultConfiguration {
NOTIMPLEMENTED();
NOTREACHED();
return nil;
}

+ (instancetype)incognitoConfiguration {
NOTIMPLEMENTED();
NOTREACHED();
return nil;
}

+ (CWVWebViewConfiguration*)nonPersistentConfiguration {
NOTIMPLEMENTED();
NOTREACHED();
return nil;
}

Expand All @@ -71,14 +70,14 @@ - (instancetype)initWithBrowserState:(web::BrowserState*)browserState {
- (CWVPreferences*)preferences {
// This property is not nullable, so will crash anyways on the Swift side if
// accessed.
NOTIMPLEMENTED();
NOTREACHED();
return nil;
}

- (CWVUserContentController*)userContentController {
// This property is not nullable, so will crash anyways on the Swift side if
// accessed.
NOTIMPLEMENTED();
NOTREACHED();
return nil;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
#ifndef BRAVE_CHROMIUM_SRC_IOS_WEB_VIEW_INTERNAL_CWV_WEB_VIEW_CONFIGURATION_INTERNAL_H_
#define BRAVE_CHROMIUM_SRC_IOS_WEB_VIEW_INTERNAL_CWV_WEB_VIEW_CONFIGURATION_INTERNAL_H_

// We completely replace the CWVWebViewConfiguration implementation so that
// we can nop certain services such as access to CWVWebView specific wrappers
// around autofill, sync, etc (since we would use Chrome's) and replace
// the exposed browser state to be a plain `web::BrowserState`

#import "ios/web_view/public/cwv_web_view_configuration.h"

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -21,6 +16,10 @@ class BrowserState;

@class CWVWebView;

// We completely replace the CWVWebViewConfiguration implementation so that
// we can nop certain services such as access to CWVWebView specific wrappers
// around autofill, sync, etc (since we would use Chrome's) and replace
// the exposed browser state to be a plain `web::BrowserState`
@interface CWVWebViewConfiguration ()

// The browser state associated with this configuration.
Expand Down
1 change: 0 additions & 1 deletion chromium_src/ios/web_view/internal/cwv_web_view_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Value;
// Expose the underlying web::WebState and a few other properties that are not
// public so that cwv_web_view_extras.mm can access the private property and
// implement additional functionality

@interface CWVWebView (Internal)
@property(readonly) web::WebState* webState;
+ (BOOL)_isRestoreDataValid:(NSData*)data;
Expand Down
6 changes: 3 additions & 3 deletions ios/browser/api/web_view/brave_web_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "brave/ios/browser/api/web_view/brave_web_view.h"

#include "base/notimplemented.h"
#include "base/notreached.h"
#include "ios/chrome/browser/tabs/model/tab_helper_util.h"
#include "ios/web/public/web_state.h"
#include "ios/web_view/internal/cwv_web_view_internal.h"
Expand All @@ -25,12 +25,12 @@ - (void)attachSecurityInterstitialHelpersToWebStateIfNecessary {
}

- (CWVAutofillController*)autofillController {
NOTIMPLEMENTED();
NOTREACHED();
return nil;
}

- (CWVTranslationController*)translationController {
NOTIMPLEMENTED();
NOTREACHED();
return nil;
}

Expand Down
7 changes: 1 addition & 6 deletions ios/browser/web/brave_web_client.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
if (!legacy_user_agent_.empty()) {
return legacy_user_agent_;
}
// FIXME: Decide on whether or not to adjust the product type (defaults to
// CriOS/<version>)
return ChromeWebClient::GetUserAgent(type);
}

Expand Down Expand Up @@ -69,13 +67,11 @@ bool WillHandleBraveURLRedirect(GURL* url, web::BrowserState* browser_state) {

std::vector<web::JavaScriptFeature*> BraveWebClient::GetJavaScriptFeatures(
web::BrowserState* browser_state) const {
// Disable majority of ChromeWebClient JS features
// Add any JavaScriptFeature's from Chromium or Brave as needed
std::vector<web::JavaScriptFeature*> features;
// FIXME: Add any JavaScriptFeature's from Chromium as needed
features.push_back(
security_interstitials::IOSSecurityInterstitialJavaScriptFeature::
GetInstance());
features.push_back(translate::TranslateJavaScriptFeature::GetInstance());
return features;
}

Expand All @@ -91,7 +87,6 @@ bool WillHandleBraveURLRedirect(GURL* url, web::BrowserState* browser_state) {

bool BraveWebClient::EnableWebInspector(
web::BrowserState* browser_state) const {
// FIXME: Probably better to just use prefs::kWebInspectorEnabled
return CWVWebView.webInspectorEnabled;
}

Expand Down
8 changes: 6 additions & 2 deletions ios/web_view/internal/cwv_web_view_extras.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ - (void)updateScripts {
config_provider.UpdateScripts();
}

- (void)createPDF:(void (^)(NSData* _Nullable))completionHandler {
- (void)createFullPagePDF:(void (^)(NSData* _Nullable))completionHandler {
self.webState->CreateFullPagePdf(base::BindOnce(completionHandler));
}

- (BOOL)canTakeSnapshot {
return self.webState->CanTakeSnapshot();
}

- (void)takeSnapshotWithRect:(CGRect)rect
completionHandler:(void (^)(UIImage* _Nullable))completionHandler {
self.webState->TakeSnapshot(rect, base::BindRepeating(completionHandler));
Expand Down Expand Up @@ -111,7 +115,7 @@ - (void)evaluateJavaScript:(NSString*)javaScriptString
}));
}

- (WKWebView*)underlyingWebView {
- (WKWebView*)internalWebView {
CRWWebController* web_controller =
web::WebStateImpl::FromWebState(self.webState)->GetWebController();
return web_controller.webView;
Expand Down
18 changes: 12 additions & 6 deletions ios/web_view/internal/web_view_global_state_util.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
namespace ios_web_view {

void InitializeGlobalState() {
// We already provide global initialization with BraveCoreMain so this is a
// stub of a required function.
// We already provide global initialization with -[BraveCoreMain init] so this
// is a stub of a required function.
//
// Typically the implementation of this method
// is in `//ios/web_view/internal/web_view_global_state_util.mm` but this
// file is not part of the web_view_sources target and only part of the
// framework target which we don't use
// This function when implemented by the main `//ios/web_view` target would
// typically setup the required Chromium web infrastructure required to
// support CWVWebView: `WebMain`, `WebMainDelegate` and `WebClient`
//
// We already set this up in a similar fashion to Chrome iOS however so there
// is no need to do it here.
//
// Note: This method is removed in M135 and replaced with a new type that
// handles things similarily: `CWVGlobalState`. This new type will need a
// chromium_src override to remove said initialization
}

} // namespace ios_web_view
13 changes: 11 additions & 2 deletions ios/web_view/public/cwv_web_view_extras.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ CWV_EXPORT
/// Creates a PDF of the current page
///
/// Equivalent of -[WKWebView createPDFWithConfiguration:completionHandler:]
- (void)createPDF:(void (^)(NSData* _Nullable))completionHandler;
- (void)createFullPagePDF:(void (^)(NSData* _Nullable))completionHandler;

/// Whether or not you can create a snapshot using `takeSnapshotWithRect`
- (BOOL)canTakeSnapshot;

/// Creates an image from the current rendered page for a given CGRect
///
Expand Down Expand Up @@ -67,9 +70,15 @@ CWV_EXPORT
- (void)updateScripts;

/// The underlying WKWebView if one has been created already.
@property(readonly, nullable) WKWebView* underlyingWebView;
///
/// This is only available for `use_blink=false` builds and be used for WebKit
/// specific paths.
@property(readonly, nullable) WKWebView* internalWebView;

/// The underlying WKWebViewConfiguration for this CWVWebView
///
/// This is only available for `use_blink=false` builds and be used for WebKit
/// specific paths.
@property(readonly) WKWebViewConfiguration* WKConfiguration;

@end
Expand Down

0 comments on commit b3ed162

Please sign in to comment.