| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #ifndef COMPONENTS_PAYMENTS_PAYMENT_REQUEST_WEB_CONTENTS_MANAGER_H_ | 5 #ifndef COMPONENTS_PAYMENTS_PAYMENT_REQUEST_WEB_CONTENTS_MANAGER_H_ |
| 6 #define COMPONENTS_PAYMENTS_PAYMENT_REQUEST_WEB_CONTENTS_MANAGER_H_ | 6 #define COMPONENTS_PAYMENTS_PAYMENT_REQUEST_WEB_CONTENTS_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "components/payments/content/payment_request.h" | 12 #include "components/payments/content/payment_request.h" |
| 13 #include "components/payments/mojom/payment_request.mojom.h" | 13 #include "components/payments/mojom/payment_request.mojom.h" |
| 14 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "content/public/browser/web_contents_user_data.h" | 15 #include "content/public/browser/web_contents_user_data.h" |
| 15 #include "mojo/public/cpp/bindings/binding.h" | 16 #include "mojo/public/cpp/bindings/binding.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 class RenderFrameHost; | 19 class RenderFrameHost; |
| 20 class NavigationHandle; |
| 19 class WebContents; | 21 class WebContents; |
| 20 } | 22 } // namespace content |
| 21 | 23 |
| 22 namespace payments { | 24 namespace payments { |
| 23 | 25 |
| 24 class PaymentRequestDelegate; | 26 class PaymentRequestDelegate; |
| 25 | 27 |
| 26 // This class owns the PaymentRequest associated with a given WebContents. | 28 // This class owns the PaymentRequest associated with a given WebContents. |
| 27 // | 29 // |
| 28 // Responsible for creating PaymentRequest's and retaining ownership. No request | 30 // Responsible for creating PaymentRequest's and retaining ownership. No request |
| 29 // pointers are currently available because the request manages its interactions | 31 // pointers are currently available because the request manages its interactions |
| 30 // with UI and renderer. The PaymentRequest may call DestroyRequest() to signal | 32 // with UI and renderer. The PaymentRequest may call DestroyRequest() to signal |
| 31 // it is ready to die. Otherwise it gets destroyed when the WebContents (thus | 33 // it is ready to die. Otherwise it gets destroyed when the WebContents (thus |
| 32 // this class) goes away. | 34 // this class) goes away. |
| 33 class PaymentRequestWebContentsManager | 35 class PaymentRequestWebContentsManager |
| 34 : public content::WebContentsUserData<PaymentRequestWebContentsManager> { | 36 : public content::WebContentsObserver, |
| 37 public content::WebContentsUserData<PaymentRequestWebContentsManager> { |
| 35 public: | 38 public: |
| 36 ~PaymentRequestWebContentsManager() override; | 39 ~PaymentRequestWebContentsManager() override; |
| 37 | 40 |
| 38 // Retrieves the instance of PaymentRequestWebContentsManager that was | 41 // Retrieves the instance of PaymentRequestWebContentsManager that was |
| 39 // attached to the specified WebContents. If no instance was attached, | 42 // attached to the specified WebContents. If no instance was attached, |
| 40 // creates one, and attaches it to the specified WebContents. | 43 // creates one, and attaches it to the specified WebContents. |
| 41 static PaymentRequestWebContentsManager* GetOrCreateForWebContents( | 44 static PaymentRequestWebContentsManager* GetOrCreateForWebContents( |
| 42 content::WebContents* web_contents); | 45 content::WebContents* web_contents); |
| 43 | 46 |
| 44 // Creates the PaymentRequest that will interact with this |render_frame_host| | 47 // Creates the PaymentRequest that will interact with this |render_frame_host| |
| 45 // and the associated |web_contents|. | 48 // and the associated |web_contents|. |
| 46 void CreatePaymentRequest( | 49 void CreatePaymentRequest( |
| 47 content::RenderFrameHost* render_frame_host, | 50 content::RenderFrameHost* render_frame_host, |
| 48 content::WebContents* web_contents, | 51 content::WebContents* web_contents, |
| 49 std::unique_ptr<PaymentRequestDelegate> delegate, | 52 std::unique_ptr<PaymentRequestDelegate> delegate, |
| 50 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request, | 53 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request, |
| 51 PaymentRequest::ObserverForTest* observer_for_testing); | 54 PaymentRequest::ObserverForTest* observer_for_testing); |
| 52 | 55 |
| 53 // Destroys the given |request|. | 56 // Destroys the given |request|. |
| 54 void DestroyRequest(PaymentRequest* request); | 57 void DestroyRequest(PaymentRequest* request); |
| 55 | 58 |
| 56 // Called when |request| has received the show() call. If the |request| can be | 59 // Called when |request| has received the show() call. If the |request| can be |
| 57 // shown, then returns true and assumes that |request| is now showing until | 60 // shown, then returns true and assumes that |request| is now showing until |
| 58 // DestroyRequest(|request|) is called with the same pointer. (Only one | 61 // DestroyRequest(|request|) is called with the same pointer. (Only one |
| 59 // request at a time can be shown per tab.) | 62 // request at a time can be shown per tab.) |
| 60 bool CanShow(PaymentRequest* request); | 63 bool CanShow(PaymentRequest* request); |
| 61 | 64 |
| 65 // WebContentsObserver:: |
| 66 void DidStartNavigation( |
| 67 content::NavigationHandle* navigation_handle) override; |
| 68 |
| 62 private: | 69 private: |
| 63 explicit PaymentRequestWebContentsManager(content::WebContents* web_contents); | 70 explicit PaymentRequestWebContentsManager(content::WebContents* web_contents); |
| 64 friend class content::WebContentsUserData<PaymentRequestWebContentsManager>; | 71 friend class content::WebContentsUserData<PaymentRequestWebContentsManager>; |
| 65 friend class PaymentRequestBrowserTestBase; | 72 friend class PaymentRequestBrowserTestBase; |
| 66 | 73 |
| 67 // Owns all the PaymentRequest for this WebContents. Since the | 74 // Owns all the PaymentRequest for this WebContents. Since the |
| 68 // PaymentRequestWebContentsManager's lifetime is tied to the WebContents, | 75 // PaymentRequestWebContentsManager's lifetime is tied to the WebContents, |
| 69 // these requests only get destroyed when the WebContents goes away, or when | 76 // these requests only get destroyed when the WebContents goes away, or when |
| 70 // the requests themselves call DestroyRequest(). | 77 // the requests themselves call DestroyRequest(). |
| 71 std::map<PaymentRequest*, std::unique_ptr<PaymentRequest>> payment_requests_; | 78 std::map<PaymentRequest*, std::unique_ptr<PaymentRequest>> payment_requests_; |
| 72 | 79 |
| 73 // The currently displayed instance of PaymentRequest. Points to one of the | 80 // The currently displayed instance of PaymentRequest. Points to one of the |
| 74 // elements in |payment_requests_|. Can be null. | 81 // elements in |payment_requests_|. Can be null. |
| 75 PaymentRequest* showing_; | 82 PaymentRequest* showing_; |
| 76 | 83 |
| 77 DISALLOW_COPY_AND_ASSIGN(PaymentRequestWebContentsManager); | 84 DISALLOW_COPY_AND_ASSIGN(PaymentRequestWebContentsManager); |
| 78 }; | 85 }; |
| 79 | 86 |
| 80 } // namespace payments | 87 } // namespace payments |
| 81 | 88 |
| 82 #endif // COMPONENTS_PAYMENTS_PAYMENT_REQUEST_WEB_CONTENTS_MANAGER_H_ | 89 #endif // COMPONENTS_PAYMENTS_PAYMENT_REQUEST_WEB_CONTENTS_MANAGER_H_ |
| OLD | NEW |