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