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