OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_ARC_ARC_NAVIGATION_THROTTLE_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_ARC_ARC_NAVIGATION_THROTTLE_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/macros.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "components/arc/intent_helper/activity_icon_loader.h" | |
17 #include "content/public/browser/navigation_throttle.h" | |
18 #include "mojo/public/cpp/bindings/array.h" | |
19 #include "ui/gfx/image/image.h" | |
20 | |
21 class GURL; | |
22 | |
23 namespace content { | |
24 class NavigationHandle; | |
25 class WebContents; | |
26 } // namespace content | |
27 | |
28 namespace arc { | |
29 | |
30 // A class that allow us to retrieve ARC app's information and handle URL | |
31 // traffic initiated on Chrome browser, either on Chrome or an ARC's app. | |
32 class ArcNavigationThrottle : public content::NavigationThrottle { | |
33 public: | |
34 // These enums are used to define the buckets for an enumerated UMA histogram | |
35 // and need to be synced with histograms.xml. This enum class should also be | |
36 // treated as append-only. | |
37 enum class CloseReason : int { | |
38 ERROR = 0, | |
39 DIALOG_DEACTIVATED = 1, | |
40 ALWAYS_PRESSED = 2, | |
41 JUST_ONCE_PRESSED = 3, | |
42 PREFERRED_ACTIVITY_FOUND = 4, | |
43 SIZE, | |
44 INVALID = SIZE, | |
45 }; | |
46 | |
47 // Restricts the amount of apps displayed to the user without the need of a | |
48 // ScrollView. | |
49 enum { kMaxAppResults = 3 }; | |
50 | |
51 struct AppInfo { | |
52 AppInfo(gfx::Image img, std::string package, std::string activity) | |
53 : icon(img), package_name(package), activity_name(activity) {} | |
54 gfx::Image icon; | |
55 std::string package_name; | |
56 std::string activity_name; | |
57 }; | |
58 | |
59 using ShowIntentPickerCallback = base::Callback<void( | |
60 content::WebContents* web_contents, | |
61 const std::vector<AppInfo>& app_info, | |
62 const base::Callback<void(const std::string&, CloseReason)>& cb)>; | |
63 ArcNavigationThrottle(content::NavigationHandle* navigation_handle, | |
64 const ShowIntentPickerCallback& show_intent_picker_cb); | |
65 ~ArcNavigationThrottle() override; | |
66 | |
67 static bool ShouldOverrideUrlLoadingForTesting(const GURL& previous_url, | |
68 const GURL& current_url); | |
69 | |
70 // Finds |selected_app_package| from the |handlers| array and returns the | |
71 // index. If the app is not found, returns |handlers.size()|. | |
72 static size_t GetAppIndex( | |
73 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers, | |
74 const std::string& selected_app_package); | |
75 | |
76 static bool IsAppAvailableForTesting( | |
77 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers); | |
78 static size_t FindPreferredAppForTesting( | |
79 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers); | |
80 static bool IsSwapElementsNeededForTesting( | |
81 const mojo::Array<mojom::IntentHandlerInfoPtr>& handlers, | |
82 std::pair<size_t, size_t>* out_indices); | |
83 | |
84 private: | |
85 // content::Navigation implementation: | |
86 NavigationThrottle::ThrottleCheckResult WillStartRequest() override; | |
87 NavigationThrottle::ThrottleCheckResult WillRedirectRequest() override; | |
88 | |
89 NavigationThrottle::ThrottleCheckResult HandleRequest(); | |
90 void OnAppCandidatesReceived( | |
91 mojo::Array<mojom::IntentHandlerInfoPtr> handlers); | |
92 void OnAppIconsReceived( | |
93 mojo::Array<mojom::IntentHandlerInfoPtr> handlers, | |
94 std::unique_ptr<ActivityIconLoader::ActivityToIconsMap> icons); | |
95 void OnIntentPickerClosed(mojo::Array<mojom::IntentHandlerInfoPtr> handlers, | |
96 const std::string& selected_app_package, | |
97 CloseReason close_reason); | |
98 // A callback object that allow us to display an IntentPicker when Run() is | |
99 // executed, it also allow us to report the user's selection back to | |
100 // OnIntentPickerClosed(). | |
101 ShowIntentPickerCallback show_intent_picker_callback_; | |
102 | |
103 // A cache of the action the user took the last time this navigation throttle | |
104 // popped up the intent picker dialog. If the dialog has never been popped up | |
105 // before, this will have a value of CloseReason::INVALID. Used to avoid | |
106 // popping up the dialog multiple times on chains of multiple redirects. | |
107 CloseReason previous_user_action_; | |
108 | |
109 // This has to be the last member of the class. | |
110 base::WeakPtrFactory<ArcNavigationThrottle> weak_ptr_factory_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(ArcNavigationThrottle); | |
113 }; | |
114 | |
115 } // namespace arc | |
116 | |
117 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_NAVIGATION_THROTTLE_H_ | |
OLD | NEW |