Chromium Code Reviews| Index: ios/chrome/browser/voice/text_to_speech_listener.mm |
| diff --git a/ios/chrome/browser/voice/text_to_speech_listener.mm b/ios/chrome/browser/voice/text_to_speech_listener.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aa95344945425791d9791fa87d95576e36b7a595 |
| --- /dev/null |
| +++ b/ios/chrome/browser/voice/text_to_speech_listener.mm |
| @@ -0,0 +1,110 @@ |
| +// Copyright 2016 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. |
| + |
| +#import "ios/chrome/browser/voice/text_to_speech_listener.h" |
| + |
| +#include <memory> |
| + |
| +#include "base/ios/weak_nsobject.h" |
| +#include "base/logging.h" |
| +#include "base/mac/scoped_nsobject.h" |
| +#include "ios/web/public/navigation_manager.h" |
| +#include "ios/web/public/web_state/web_state.h" |
| +#include "ios/web/public/web_state/web_state_observer.h" |
| +#import "ios/chrome/browser/voice/text_to_speech_parser.h" |
| +#import "ios/chrome/browser/voice/voice_search_url_rewriter.h" |
| +#include "url/gurl.h" |
| + |
| +#pragma mark - TextToSpeechListener Private Interface |
| + |
| +class TextToSpeechWebStateObserver; |
| + |
| +@interface TextToSpeechListener () { |
|
sdefresne
2016/10/25 13:20:30
nit: move the ivar declaration to @implementation
rohitrao (ping after 24h)
2016/10/25 15:23:12
Done.
|
| + // Backing object for property of the same name. |
| + base::WeakNSProtocol<id<TextToSpeechListenerDelegate>> _delegate; |
| + // The TextToSpeechWebStateObserver that listens for Text-To-Speech data. |
| + std::unique_ptr<TextToSpeechWebStateObserver> _webStateObserver; |
|
sdefresne
2016/10/25 13:20:30
I think it could be just std::unique_ptr<web::WebS
rohitrao (ping after 24h)
2016/10/25 15:23:12
Protected destructor on web::WebStateObserver prev
|
| +} |
| + |
| +// The TextToSpeechListenerDelegate passed on initialization. |
| +@property(nonatomic, readonly) id<TextToSpeechListenerDelegate> delegate; |
| + |
| +@end |
| + |
| +#pragma mark - TextToSpeechWebStateObserver |
| + |
| +class TextToSpeechWebStateObserver : public web::WebStateObserver { |
| + public: |
| + TextToSpeechWebStateObserver(web::WebState* web_state, |
| + TextToSpeechListener* listener); |
| + ~TextToSpeechWebStateObserver() override; |
| + |
| + // web::WebStateObserver implementation: |
| + void PageLoaded( |
| + web::PageLoadCompletionStatus load_completion_status) override; |
| + void WebStateDestroyed() override; |
| + |
| + private: |
| + TextToSpeechListener* listener_; |
| +}; |
| + |
| +TextToSpeechWebStateObserver::TextToSpeechWebStateObserver( |
| + web::WebState* web_state, |
| + TextToSpeechListener* listener) |
| + : web::WebStateObserver(web_state), listener_(listener) { |
| + DCHECK(web_state); |
| + DCHECK(listener); |
| + // Rewrite the next loaded URL to have the voice search flags. |
| + web_state->GetNavigationManager()->AddTransientURLRewriter( |
| + &VoiceSearchURLRewriter); |
| +} |
| + |
| +TextToSpeechWebStateObserver::~TextToSpeechWebStateObserver() {} |
| + |
| +void TextToSpeechWebStateObserver::PageLoaded( |
| + web::PageLoadCompletionStatus load_completion_status) { |
| + GURL url = web_state()->GetLastCommittedURL(); |
|
sdefresne
2016/10/25 13:20:30
nit: const GURL& url = ...
rohitrao (ping after 24h)
2016/10/25 15:23:12
Done.
|
| + BOOL shouldParse = [listener_.delegate shouldTextToSpeechListener:listener_ |
| + parseDataFromURL:url]; |
| + if (shouldParse) { |
| + base::WeakNSObject<TextToSpeechListener> weakListener(listener_); |
| + ExtractVoiceSearchAudioDataFromWebState(web_state(), ^(NSData* audioData) { |
| + [[weakListener delegate] textToSpeechListener:weakListener |
| + didReceieveResult:audioData]; |
| + }); |
| + } else { |
| + [listener_.delegate textToSpeechListener:listener_ didReceieveResult:nil]; |
| + } |
| +} |
| + |
| +void TextToSpeechWebStateObserver::WebStateDestroyed() { |
| + [listener_.delegate textToSpeechListenerWebStateWasDestroyed:listener_]; |
| +} |
| + |
| +#pragma mark - TextToSpeechListener |
| + |
| +@implementation TextToSpeechListener |
| + |
| +- (instancetype)initWithWebState:(web::WebState*)webState |
| + delegate:(id<TextToSpeechListenerDelegate>)delegate { |
| + if ((self = [super init])) { |
| + DCHECK(webState); |
| + DCHECK(delegate); |
| + _webStateObserver.reset(new TextToSpeechWebStateObserver(webState, self)); |
| + _delegate.reset(delegate); |
| + } |
| + return self; |
| +} |
| + |
| +#pragma mark Accessors |
| + |
| +- (web::WebState*)webState { |
| + return _webStateObserver->web_state(); |
| +} |
| + |
| +- (id<TextToSpeechListenerDelegate>)delegate { |
| + return _delegate.get(); |
| +} |
| + |
| +@end |