Commit cacc3a58 authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Convert PrintObserver to a tab helper (PrintTabHelper).

Change PrintObserver to a WebStateUserData and move creation to
BrowserViewController to remove the need to create it from Tab
and a dependency of model on UI.

Introduce a separate protocol WebStatePrinter implemented by
BrowserViewController to decouple PrintTabHelper from UI.

Bug: 760556
Change-Id: I42ea68e70e84b52ed7c5f705ee01e74e749e09df
Reviewed-on: https://chromium-review.googlesource.com/660122
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarRohit Rao (ping after 24h) <rohitrao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#501257}
parent 31b1e91f
......@@ -96,7 +96,6 @@
#import "ios/chrome/browser/web/external_app_launcher.h"
#import "ios/chrome/browser/web/navigation_manager_util.h"
#import "ios/chrome/browser/web/passkit_dialog_provider.h"
#include "ios/chrome/browser/web/print_observer.h"
#import "ios/chrome/browser/web/tab_id_tab_helper.h"
#include "ios/chrome/grit/ios_strings.h"
#import "ios/web/navigation/navigation_manager_impl.h"
......@@ -215,9 +214,6 @@ class TabHistoryContext : public history::Context {
// web page.
WebControllerSnapshotHelper* _webControllerSnapshotHelper;
// Handles support for window.print JavaScript calls.
std::unique_ptr<PrintObserver> _printObserver;
// WebStateImpl for this tab.
web::WebStateImpl* _webStateImpl;
......@@ -402,13 +398,6 @@ void TabInfoBarObserver::OnInfoBarReplaced(infobars::InfoBar* old_infobar,
self, favicon::WebFaviconDriver::FromWebState(self.webState));
}
// Attach any tab helpers which are dependent on the dispatcher having been
// set on the tab.
- (void)attachDispatcherDependentTabHelpers {
_printObserver =
base::MakeUnique<PrintObserver>(self.webState, self.dispatcher);
}
- (id<FindInPageControllerDelegate>)findInPageControllerDelegate {
return self;
}
......@@ -638,12 +627,9 @@ void TabInfoBarObserver::OnInfoBarReplaced(infobars::InfoBar* old_infobar,
// should be nil, or the new value should be nil.
DCHECK(!_dispatcher || !dispatcher);
_dispatcher = dispatcher;
// Forward the new dispatcher to tab helpers.
PasswordTabHelper::FromWebState(self.webState)
->SetDispatcher(self.dispatcher);
// If the new dispatcher is nonnull, add tab helpers.
if (self.dispatcher)
[self attachDispatcherDependentTabHelpers];
PasswordTabHelper::FromWebState(self.webState)->SetDispatcher(_dispatcher);
}
- (void)saveTitleToHistoryDB {
......
......@@ -171,8 +171,10 @@
#import "ios/chrome/browser/web/blocked_popup_tab_helper.h"
#import "ios/chrome/browser/web/error_page_content.h"
#import "ios/chrome/browser/web/passkit_dialog_provider.h"
#include "ios/chrome/browser/web/print_tab_helper.h"
#import "ios/chrome/browser/web/repost_form_tab_helper.h"
#import "ios/chrome/browser/web/sad_tab_tab_helper.h"
#include "ios/chrome/browser/web/web_state_printer.h"
#import "ios/chrome/browser/web_state_list/web_state_list.h"
#import "ios/chrome/browser/web_state_list/web_state_opener.h"
#include "ios/chrome/grit/ios_chromium_strings.h"
......@@ -375,7 +377,8 @@ bool IsURLAllowedInIncognito(const GURL& url) {
UIGestureRecognizerDelegate,
UpgradeCenterClientProtocol,
VoiceSearchBarDelegate,
VoiceSearchBarOwner> {
VoiceSearchBarOwner,
WebStatePrinter> {
// The dependency factory passed on initialization. Used to vend objects used
// by the BVC.
BrowserViewControllerDependencyFactory* _dependencyFactory;
......@@ -2367,6 +2370,7 @@ bubblePresenterForFeature:(const base::Feature&)feature
// BrowserViewController owns the coordinator that displays the Sad Tab.
if (!SadTabTabHelper::FromWebState(tab.webState))
SadTabTabHelper::CreateForWebState(tab.webState, _sadTabCoordinator);
PrintTabHelper::CreateForWebState(tab.webState, self);
}
- (void)uninstallDelegatesForTab:(Tab*)tab {
......@@ -5106,4 +5110,11 @@ bubblePresenterForFeature:(const base::Feature&)feature
[_toolbarController cancelOmniboxEdit];
}
#pragma mark - WebStatePrinter
- (void)printWebState:(web::WebState*)webState {
if (webState == [_model currentTab].webState)
[self printTab];
}
@end
......@@ -147,8 +147,9 @@ source_set("web_internal") {
"external_app_launcher.h",
"external_app_launcher.mm",
"passkit_dialog_provider.h",
"print_observer.h",
"print_observer.mm",
"print_tab_helper.h",
"print_tab_helper.mm",
"web_state_printer.h",
]
deps = [
":chrome_bundle",
......@@ -187,7 +188,10 @@ source_set("web_internal") {
"//ui/gfx",
"//url",
]
libs = [ "UIKit.framework" ]
libs = [
"UIKit.framework",
"Foundation.framework",
]
}
source_set("test_support") {
......
......@@ -2,34 +2,43 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_WEB_PRINT_OBSERVER_H_
#define IOS_CHROME_BROWSER_WEB_PRINT_OBSERVER_H_
#ifndef IOS_CHROME_BROWSER_WEB_PRINT_TAB_HELPER_H_
#define IOS_CHROME_BROWSER_WEB_PRINT_TAB_HELPER_H_
#include "base/macros.h"
#include "ios/web/public/web_state/web_state_observer.h"
#include "ios/web/public/web_state/web_state_user_data.h"
@protocol WebStatePrinter;
class GURL;
@protocol BrowserCommands;
namespace base {
class DictionaryValue;
} // namespace base
class GURL;
// Handles print requests from JavaScript window.print.
class PrintObserver : public web::WebStateObserver {
class PrintTabHelper : public web::WebStateObserver,
public web::WebStateUserData<PrintTabHelper> {
public:
PrintObserver(web::WebState* web_state, id<BrowserCommands> dispatcher);
~PrintObserver() override;
~PrintTabHelper() override;
// Creates a PrintTabHelper and attaches it to |web_state|. The |printer|
// must be non-nil.
static void CreateForWebState(web::WebState* web_state,
id<WebStatePrinter> printer);
private:
PrintTabHelper(web::WebState* web_state, id<WebStatePrinter> printer);
// web::WebStateObserver overrides:
void WebStateDestroyed() override;
// Called when print message is sent by the web page.
bool OnPrintCommand(const base::DictionaryValue&, const GURL&, bool);
// Stops handling print requests from the web page.
void Detach();
__weak id<BrowserCommands> dispatcher_;
__weak id<WebStatePrinter> printer_;
DISALLOW_COPY_AND_ASSIGN(PrintTabHelper);
};
#endif // IOS_CHROME_BROWSER_WEB_PRINT_OBSERVER_H_
#endif // IOS_CHROME_BROWSER_WEB_PRINT_TAB_HELPER_H_
......@@ -2,12 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/chrome/browser/web/print_observer.h"
#include "ios/chrome/browser/web/print_tab_helper.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "ios/chrome/browser/ui/commands/browser_commands.h"
#include "ios/chrome/browser/web/web_state_printer.h"
#import "ios/web/public/web_state/web_state.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
......@@ -19,31 +20,40 @@ namespace {
const char kPrintCommandPrefix[] = "print";
}
PrintObserver::PrintObserver(web::WebState* web_state,
id<BrowserCommands> dispatcher)
: web::WebStateObserver(web_state), dispatcher_(dispatcher) {
web_state->AddScriptCommandCallback(
base::Bind(&PrintObserver::OnPrintCommand, base::Unretained(this)),
kPrintCommandPrefix);
}
DEFINE_WEB_STATE_USER_DATA_KEY(PrintTabHelper);
PrintObserver::~PrintObserver() {
Detach();
// static
void PrintTabHelper::CreateForWebState(web::WebState* web_state,
id<WebStatePrinter> printer) {
DCHECK(web_state);
if (!FromWebState(web_state)) {
web_state->SetUserData(UserDataKey(), base::WrapUnique(new PrintTabHelper(
web_state, printer)));
}
}
void PrintObserver::WebStateDestroyed() {
Detach();
}
PrintTabHelper::~PrintTabHelper() = default;
bool PrintObserver::OnPrintCommand(const base::DictionaryValue&,
const GURL&,
bool) {
[dispatcher_ printTab];
return true;
PrintTabHelper::PrintTabHelper(web::WebState* web_state,
id<WebStatePrinter> printer)
: web::WebStateObserver(web_state), printer_(printer) {
DCHECK(printer);
web_state->AddScriptCommandCallback(
base::Bind(&PrintTabHelper::OnPrintCommand, base::Unretained(this)),
kPrintCommandPrefix);
}
void PrintObserver::Detach() {
void PrintTabHelper::WebStateDestroyed() {
// Stops handling print requests from the web page.
if (web_state()) {
web_state()->RemoveScriptCommandCallback(kPrintCommandPrefix);
}
}
bool PrintTabHelper::OnPrintCommand(const base::DictionaryValue&,
const GURL&,
bool) {
DCHECK(web_state());
[printer_ printWebState:web_state()];
return true;
}
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_WEB_WEB_STATE_PRINTER_H_
#define IOS_CHROME_BROWSER_WEB_WEB_STATE_PRINTER_H_
#import <Foundation/Foundation.h>
namespace web {
class WebState;
}
// Protocol implemented to print a WebState. Used to implement the javascript
// command "window.print".
@protocol WebStatePrinter<NSObject>
- (void)printWebState:(web::WebState*)webState;
@end
#endif // IOS_CHROME_BROWSER_WEB_WEB_STATE_PRINTER_H_
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