OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #import "ios/chrome/browser/voice/text_to_speech_listener.h" | 5 #import "ios/chrome/browser/voice/text_to_speech_listener.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/ios/weak_nsobject.h" | |
10 #include "base/logging.h" | 9 #include "base/logging.h" |
11 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/memory/ptr_util.h" |
12 #include "ios/web/public/navigation_manager.h" | 12 #include "ios/web/public/navigation_manager.h" |
13 #include "ios/web/public/web_state/web_state.h" | 13 #include "ios/web/public/web_state/web_state.h" |
14 #include "ios/web/public/web_state/web_state_observer.h" | 14 #include "ios/web/public/web_state/web_state_observer.h" |
15 #import "ios/chrome/browser/voice/text_to_speech_parser.h" | 15 #import "ios/chrome/browser/voice/text_to_speech_parser.h" |
16 #import "ios/chrome/browser/voice/voice_search_url_rewriter.h" | 16 #import "ios/chrome/browser/voice/voice_search_url_rewriter.h" |
17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
18 | 18 |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 20 #error "This file requires ARC support." |
| 21 #endif |
| 22 |
19 #pragma mark - TextToSpeechListener Private Interface | 23 #pragma mark - TextToSpeechListener Private Interface |
20 | 24 |
21 class TextToSpeechWebStateObserver; | 25 class TextToSpeechWebStateObserver; |
22 | 26 |
23 @interface TextToSpeechListener () | 27 @interface TextToSpeechListener () |
24 | 28 |
25 // The TextToSpeechListenerDelegate passed on initialization. | 29 // The TextToSpeechListenerDelegate passed on initialization. |
26 @property(nonatomic, readonly) id<TextToSpeechListenerDelegate> delegate; | 30 @property(weak, nonatomic, readonly) id<TextToSpeechListenerDelegate> delegate; |
27 | 31 |
28 @end | 32 @end |
29 | 33 |
30 #pragma mark - TextToSpeechWebStateObserver | 34 #pragma mark - TextToSpeechWebStateObserver |
31 | 35 |
32 class TextToSpeechWebStateObserver : public web::WebStateObserver { | 36 class TextToSpeechWebStateObserver : public web::WebStateObserver { |
33 public: | 37 public: |
34 TextToSpeechWebStateObserver(web::WebState* web_state, | 38 TextToSpeechWebStateObserver(web::WebState* web_state, |
35 TextToSpeechListener* listener); | 39 TextToSpeechListener* listener); |
36 ~TextToSpeechWebStateObserver() override; | 40 ~TextToSpeechWebStateObserver() override; |
(...skipping 19 matching lines...) Expand all Loading... |
56 } | 60 } |
57 | 61 |
58 TextToSpeechWebStateObserver::~TextToSpeechWebStateObserver() {} | 62 TextToSpeechWebStateObserver::~TextToSpeechWebStateObserver() {} |
59 | 63 |
60 void TextToSpeechWebStateObserver::PageLoaded( | 64 void TextToSpeechWebStateObserver::PageLoaded( |
61 web::PageLoadCompletionStatus load_completion_status) { | 65 web::PageLoadCompletionStatus load_completion_status) { |
62 const GURL& url = web_state()->GetLastCommittedURL(); | 66 const GURL& url = web_state()->GetLastCommittedURL(); |
63 BOOL shouldParse = [listener_.delegate shouldTextToSpeechListener:listener_ | 67 BOOL shouldParse = [listener_.delegate shouldTextToSpeechListener:listener_ |
64 parseDataFromURL:url]; | 68 parseDataFromURL:url]; |
65 if (shouldParse) { | 69 if (shouldParse) { |
66 base::WeakNSObject<TextToSpeechListener> weakListener(listener_); | 70 __weak TextToSpeechListener* weakListener = listener_; |
67 ExtractVoiceSearchAudioDataFromWebState(web_state(), ^(NSData* audioData) { | 71 ExtractVoiceSearchAudioDataFromWebState(web_state(), ^(NSData* audioData) { |
68 [[weakListener delegate] textToSpeechListener:weakListener | 72 [[weakListener delegate] textToSpeechListener:weakListener |
69 didReceiveResult:audioData]; | 73 didReceiveResult:audioData]; |
70 }); | 74 }); |
71 } else { | 75 } else { |
72 [listener_.delegate textToSpeechListener:listener_ didReceiveResult:nil]; | 76 [listener_.delegate textToSpeechListener:listener_ didReceiveResult:nil]; |
73 } | 77 } |
74 } | 78 } |
75 | 79 |
76 void TextToSpeechWebStateObserver::WebStateDestroyed() { | 80 void TextToSpeechWebStateObserver::WebStateDestroyed() { |
77 [listener_.delegate textToSpeechListenerWebStateWasDestroyed:listener_]; | 81 [listener_.delegate textToSpeechListenerWebStateWasDestroyed:listener_]; |
78 } | 82 } |
79 | 83 |
80 #pragma mark - TextToSpeechListener | 84 #pragma mark - TextToSpeechListener |
81 | |
82 @implementation TextToSpeechListener { | 85 @implementation TextToSpeechListener { |
83 // Backing object for property of the same name. | |
84 base::WeakNSProtocol<id<TextToSpeechListenerDelegate>> _delegate; | |
85 // The TextToSpeechWebStateObserver that listens for Text-To-Speech data. | 86 // The TextToSpeechWebStateObserver that listens for Text-To-Speech data. |
86 std::unique_ptr<TextToSpeechWebStateObserver> _webStateObserver; | 87 std::unique_ptr<TextToSpeechWebStateObserver> _webStateObserver; |
87 } | 88 } |
| 89 @synthesize delegate = _delegate; |
88 | 90 |
89 - (instancetype)initWithWebState:(web::WebState*)webState | 91 - (instancetype)initWithWebState:(web::WebState*)webState |
90 delegate:(id<TextToSpeechListenerDelegate>)delegate { | 92 delegate:(id<TextToSpeechListenerDelegate>)delegate { |
91 if ((self = [super init])) { | 93 if ((self = [super init])) { |
92 DCHECK(webState); | 94 DCHECK(webState); |
93 DCHECK(delegate); | 95 DCHECK(delegate); |
94 _webStateObserver.reset(new TextToSpeechWebStateObserver(webState, self)); | 96 _webStateObserver = |
95 _delegate.reset(delegate); | 97 base::MakeUnique<TextToSpeechWebStateObserver>(webState, self); |
| 98 _delegate = delegate; |
96 } | 99 } |
97 return self; | 100 return self; |
98 } | 101 } |
99 | 102 |
100 #pragma mark Accessors | 103 #pragma mark Accessors |
101 | 104 |
102 - (web::WebState*)webState { | 105 - (web::WebState*)webState { |
103 return _webStateObserver->web_state(); | 106 return _webStateObserver->web_state(); |
104 } | 107 } |
105 | 108 |
106 - (id<TextToSpeechListenerDelegate>)delegate { | |
107 return _delegate.get(); | |
108 } | |
109 | |
110 @end | 109 @end |
OLD | NEW |