Commit fc901203 authored by Fredrik Söderqvist's avatar Fredrik Söderqvist Committed by Commit Bot

Refactor ImageDocument::UpdateImageStyle()

This moves the determination of the mouse cursor to a separate method,
dropping the associated state in the process.
Drop the ShouldShrinkToFit() check in ImageLoaded(). This will have no
direct effect at the moment, but will aid a future CL.

Bug: 844743
Change-Id: Iae464e999668f7f77b13e03018e801c3885fb6f9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2461268
Commit-Queue: Fredrik Söderquist <fs@opera.com>
Reviewed-by: default avatarPhilip Rogers <pdr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#815246}
parent dd9d84a6
......@@ -204,7 +204,6 @@ ImageDocument::ImageDocument(const DocumentInit& initializer)
did_shrink_image_(false),
should_shrink_image_(ShouldShrinkToFit()),
image_is_loaded_(false),
style_mouse_cursor_mode_(kDefault),
shrink_to_fit_mode_(GetFrame()->GetSettings()->GetViewportEnabled()
? kViewport
: kDesktop) {
......@@ -394,11 +393,17 @@ void ImageDocument::ImageClicked(int x, int y) {
void ImageDocument::ImageLoaded() {
image_is_loaded_ = true;
UpdateImageStyle();
}
if (ShouldShrinkToFit()) {
// The checkerboard background needs to be inserted.
UpdateImageStyle();
}
ImageDocument::MouseCursorMode ImageDocument::ComputeMouseCursorMode() const {
if (!image_is_loaded_)
return kDefault;
if (shrink_to_fit_mode_ != kDesktop || !ShouldShrinkToFit())
return kDefault;
if (ImageFitsInWindow())
return kDefault;
return should_shrink_image_ ? kZoomIn : kZoomOut;
}
void ImageDocument::UpdateImageStyle() {
......@@ -409,36 +414,14 @@ void ImageDocument::UpdateImageStyle() {
if (shrink_to_fit_mode_ == kViewport)
image_style.Append("max-width: 100%;");
image_style.Append("margin: auto;");
if (image_is_loaded_) {
MouseCursorMode new_cursor_mode = kDefault;
if (shrink_to_fit_mode_ != kViewport) {
// In desktop mode, the user can click on the image to zoom in or out.
DCHECK_EQ(shrink_to_fit_mode_, kDesktop);
if (ImageFitsInWindow()) {
new_cursor_mode = kDefault;
} else {
new_cursor_mode = should_shrink_image_ ? kZoomIn : kZoomOut;
}
}
// The only thing that can differ between updates is
// the type of cursor being displayed.
if (new_cursor_mode == style_mouse_cursor_mode_) {
return;
}
style_mouse_cursor_mode_ = new_cursor_mode;
if (shrink_to_fit_mode_ == kDesktop) {
if (style_mouse_cursor_mode_ == kZoomIn)
image_style.Append("cursor: zoom-in;");
else if (style_mouse_cursor_mode_ == kZoomOut)
image_style.Append("cursor: zoom-out;");
}
}
}
MouseCursorMode cursor_mode = ComputeMouseCursorMode();
if (cursor_mode == kZoomIn)
image_style.Append("cursor: zoom-in;");
else if (cursor_mode == kZoomOut)
image_style.Append("cursor: zoom-out;");
image_element_->setAttribute(html_names::kStyleAttr,
image_style.ToAtomicString());
}
......
......@@ -57,6 +57,10 @@ class CORE_EXPORT ImageDocument final : public HTMLDocument {
private:
DocumentParser* CreateParser() override;
enum MouseCursorMode { kDefault, kZoomIn, kZoomOut };
// Compute the state of the mouse cursor in the image style.
MouseCursorMode ComputeMouseCursorMode() const;
// Calculates how large the div needs to be to properly center the image.
int CalculateDivWidth();
......@@ -83,10 +87,6 @@ class CORE_EXPORT ImageDocument final : public HTMLDocument {
// Whether the image has finished loading or not
bool image_is_loaded_;
// Desktop: State of the mouse cursor in the image style
enum MouseCursorMode { kDefault, kZoomIn, kZoomOut };
MouseCursorMode style_mouse_cursor_mode_;
enum ShrinkToFitMode { kViewport, kDesktop };
ShrinkToFitMode shrink_to_fit_mode_;
......
......@@ -2,4 +2,6 @@ CONSOLE ERROR: Refused to apply inline style because it violates the following C
CONSOLE ERROR: Refused to apply inline style because it violates the following Content Security Policy directive: "default-src 'none'". Either the 'unsafe-inline' keyword, a hash ('sha256-VPZ2mdsWWlqXOFgt1tAllbbJhG8t9bh6emP1o9GwJxY='), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'style-src' was not explicitly set, so 'default-src' is used as a fallback.
CONSOLE ERROR: Refused to apply inline style because it violates the following Content Security Policy directive: "default-src 'none'". Either the 'unsafe-inline' keyword, a hash ('sha256-VPZ2mdsWWlqXOFgt1tAllbbJhG8t9bh6emP1o9GwJxY='), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'style-src' was not explicitly set, so 'default-src' is used as a fallback.
Ensure that we don't crash when loading an ImageDocument that sets CSP headers
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment