Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Unified Diff: ios/chrome/browser/voice/text_to_speech_listener.mm

Issue 2449593002: [ios] Adds support for parsing Text-to-Speech search results. (Closed)
Patch Set: Disabled Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..f9a8468dec0486c4bb5f40853d5621110704505c
--- /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 ()
+
+// 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) {
+ const GURL& url = web_state()->GetLastCommittedURL();
+ BOOL shouldParse = [listener_.delegate shouldTextToSpeechListener:listener_
+ parseDataFromURL:url];
+ if (shouldParse) {
+ base::WeakNSObject<TextToSpeechListener> weakListener(listener_);
+ ExtractVoiceSearchAudioDataFromWebState(web_state(), ^(NSData* audioData) {
+ [[weakListener delegate] textToSpeechListener:weakListener
+ didReceiveResult:audioData];
+ });
+ } else {
+ [listener_.delegate textToSpeechListener:listener_ didReceiveResult:nil];
+ }
+}
+
+void TextToSpeechWebStateObserver::WebStateDestroyed() {
+ [listener_.delegate textToSpeechListenerWebStateWasDestroyed:listener_];
+}
+
+#pragma mark - TextToSpeechListener
+
+@implementation TextToSpeechListener {
+ // 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;
+}
+
+- (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
« no previous file with comments | « ios/chrome/browser/voice/text_to_speech_listener.h ('k') | ios/chrome/browser/voice/text_to_speech_listener_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698