OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/chrome/browser/web/print_observer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #import "base/mac/scoped_nsobject.h" |
| 10 #include "base/values.h" |
| 11 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" |
| 12 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 13 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 14 #import "ios/web/public/web_state/web_state.h" |
| 15 |
| 16 namespace { |
| 17 // Prefix for print JavaScript command. |
| 18 const char kPrintCommandPrefix[] = "print"; |
| 19 } |
| 20 |
| 21 PrintObserver::PrintObserver(web::WebState* web_state) |
| 22 : web::WebStateObserver(web_state) { |
| 23 web_state->AddScriptCommandCallback( |
| 24 base::Bind(&PrintObserver::OnPrintCommand, base::Unretained(this)), |
| 25 kPrintCommandPrefix); |
| 26 } |
| 27 |
| 28 PrintObserver::~PrintObserver() { |
| 29 Detach(); |
| 30 } |
| 31 |
| 32 void PrintObserver::WebStateDestroyed() { |
| 33 Detach(); |
| 34 } |
| 35 |
| 36 bool PrintObserver::OnPrintCommand(const base::DictionaryValue&, |
| 37 const GURL&, |
| 38 bool) { |
| 39 base::scoped_nsobject<GenericChromeCommand> print_command( |
| 40 [[GenericChromeCommand alloc] initWithTag:IDC_PRINT]); |
| 41 [web_state()->GetView() chromeExecuteCommand:print_command]; |
| 42 return true; |
| 43 } |
| 44 |
| 45 void PrintObserver::Detach() { |
| 46 if (web_state()) { |
| 47 web_state()->RemoveScriptCommandCallback(kPrintCommandPrefix); |
| 48 } |
| 49 } |
OLD | NEW |