1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_MEDIA_FEEDS_MEDIA_FEEDS_CONTENTS_OBSERVER_H_
6 #define CHROME_BROWSER_MEDIA_FEEDS_MEDIA_FEEDS_CONTENTS_OBSERVER_H_
7 
8 #include "base/callback_forward.h"
9 #include "chrome/common/chrome_render_frame.mojom.h"
10 #include "content/public/browser/web_contents_observer.h"
11 #include "content/public/browser/web_contents_user_data.h"
12 #include "mojo/public/cpp/bindings/associated_remote.h"
13 
14 namespace media_feeds {
15 class MediaFeedsService;
16 }  // namespace media_feeds
17 
18 namespace url {
19 class Origin;
20 }  // namespace url
21 
22 class MediaFeedsContentsObserver
23     : public content::WebContentsObserver,
24       public content::WebContentsUserData<MediaFeedsContentsObserver> {
25  public:
26   ~MediaFeedsContentsObserver() override;
27   MediaFeedsContentsObserver(const MediaFeedsContentsObserver&) = delete;
28   MediaFeedsContentsObserver& operator=(const MediaFeedsContentsObserver&) =
29       delete;
30 
31   // content::WebContentsObserver:
32   void DidFinishNavigation(content::NavigationHandle* handle) override;
33   void DidFinishLoad(content::RenderFrameHost* render_frame_host,
34                      const GURL& validated_url) override;
35   void WebContentsDestroyed() override;
36 
SetClosureForTest(base::RepeatingClosure callback)37   void SetClosureForTest(base::RepeatingClosure callback) {
38     test_closure_ = std::move(callback);
39   }
40 
41  private:
42   friend class content::WebContentsUserData<MediaFeedsContentsObserver>;
43 
44   explicit MediaFeedsContentsObserver(content::WebContents* web_contents);
45 
46   void DidFindMediaFeed(const url::Origin& origin,
47                         const base::Optional<GURL>& url);
48 
49   media_feeds::MediaFeedsService* GetService();
50 
51   void ResetFeed();
52 
53   // The last origin that the web contents navigated too. Used for resetting
54   // feeds based on user navigation.
55   base::Optional<url::Origin> last_origin_;
56 
57   // The test closure will be called once we have checked the page for a media
58   // feed.
59   base::RepeatingClosure test_closure_;
60 
61   mojo::AssociatedRemote<chrome::mojom::ChromeRenderFrame> render_frame_;
62 
63   WEB_CONTENTS_USER_DATA_KEY_DECL();
64 };
65 
66 #endif  // CHROME_BROWSER_MEDIA_FEEDS_MEDIA_FEEDS_CONTENTS_OBSERVER_H_
67