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

Side by Side Diff: chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h

Issue 2968693003: [Android Webapps] Make AddToHomescreenDataFetcher easier to test (Closed)
Patch Set: Merge branch 'master' into homescreen_fetcher_weak_ptr2 Created 3 years, 5 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 unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ 5 #ifndef CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_
6 #define CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ 6 #define CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_
7 7
8 #include <utility>
9
8 #include "base/macros.h" 10 #include "base/macros.h"
9 #include "base/memory/ref_counted.h" 11 #include "base/memory/weak_ptr.h"
10 #include "base/strings/string16.h" 12 #include "base/strings/string16.h"
11 #include "base/task/cancelable_task_tracker.h" 13 #include "base/task/cancelable_task_tracker.h"
12 #include "base/timer/timer.h" 14 #include "base/timer/timer.h"
13 #include "chrome/browser/android/shortcut_info.h" 15 #include "chrome/browser/android/shortcut_info.h"
14 #include "content/public/browser/web_contents_observer.h" 16 #include "content/public/browser/web_contents_observer.h"
15 #include "third_party/skia/include/core/SkBitmap.h" 17 #include "third_party/skia/include/core/SkBitmap.h"
16 18
17 namespace favicon_base { 19 namespace favicon_base {
18 struct FaviconRawBitmapResult; 20 struct FaviconRawBitmapResult;
19 } 21 }
20 22
21 class GURL;
22 class InstallableManager; 23 class InstallableManager;
23 struct InstallableData; 24 struct InstallableData;
24 struct WebApplicationInfo; 25 struct WebApplicationInfo;
25 26
26 // Aysnchronously fetches and processes data needed to create a shortcut for an 27 // Aysnchronously fetches and processes data needed to create a shortcut for an
27 // Android Home screen launcher. 28 // Android Home screen launcher.
28 // 29 class AddToHomescreenDataFetcher : public content::WebContentsObserver {
29 // Because of the various asynchronous calls made by this class, it is
30 // refcounted to prevent the class from being prematurely deleted. If the
31 // |weak_observer_| pointer becomes invalid, the pipeline should kill itself.
32 class AddToHomescreenDataFetcher
33 : public base::RefCounted<AddToHomescreenDataFetcher>,
34 public content::WebContentsObserver {
35 public: 30 public:
36 class Observer { 31 class Observer {
37 public: 32 public:
38 // Called when the installable check is complete. 33 // Called when the installable check is complete.
39 virtual void OnDidDetermineWebApkCompatibility( 34 virtual void OnDidDetermineWebApkCompatibility(
40 bool is_webapk_compatible) = 0; 35 bool is_webapk_compatible) = 0;
41 36
42 // Called when the title of the page is available. Will be called after 37 // Called when the title of the page is available. Will be called after
43 // OnDidDetermineWebApkCompatibility. 38 // OnDidDetermineWebApkCompatibility.
44 virtual void OnUserTitleAvailable(const base::string16& title) = 0; 39 virtual void OnUserTitleAvailable(const base::string16& title) = 0;
45 40
46 // Converts the icon into one that can be used on the Android Home screen.
47 // |is_generated| is an out-param that indicates whether the icon was
48 // generated by Chrome.
49 virtual SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon,
50 const GURL& url,
51 bool* is_generated) = 0;
52
53 // Called when all the data needed to create a shortcut is available. 41 // Called when all the data needed to create a shortcut is available.
54 virtual void OnDataAvailable(const ShortcutInfo& info, 42 virtual void OnDataAvailable(const ShortcutInfo& info,
55 const SkBitmap& primary_icon, 43 const SkBitmap& primary_icon,
56 const SkBitmap& badge_icon) = 0; 44 const SkBitmap& badge_icon) = 0;
57 45
58 protected: 46 protected:
59 virtual ~Observer() {} 47 virtual ~Observer() {}
60 }; 48 };
61 49
62 // Initialize the fetcher by requesting the information about the page from 50 // Initialize the fetcher by requesting the information about the page from
63 // the renderer process. The initialization is asynchronous and 51 // the renderer process. The initialization is asynchronous and
64 // OnDidGetWebApplicationInfo is expected to be called when finished. 52 // OnDidGetWebApplicationInfo is expected to be called when finished.
53 // |observer| must outlive AddToHomescreenDataFetcher.
65 AddToHomescreenDataFetcher(content::WebContents* web_contents, 54 AddToHomescreenDataFetcher(content::WebContents* web_contents,
66 int ideal_icon_size_in_px, 55 int ideal_icon_size_in_px,
67 int minimum_icon_size_in_px, 56 int minimum_icon_size_in_px,
68 int ideal_splash_image_size_in_px, 57 int ideal_splash_image_size_in_px,
69 int minimum_splash_image_size_in_px, 58 int minimum_splash_image_size_in_px,
70 int badge_size_in_px, 59 int badge_size_in_px,
71 int data_timeout_ms, 60 int data_timeout_ms,
72 bool check_webapk_compatible, 61 bool check_webapk_compatible,
73 Observer* observer); 62 Observer* observer);
74 63
64 ~AddToHomescreenDataFetcher() override;
65
75 // IPC message received when the initialization is finished. 66 // IPC message received when the initialization is finished.
76 void OnDidGetWebApplicationInfo(const WebApplicationInfo& web_app_info); 67 void OnDidGetWebApplicationInfo(const WebApplicationInfo& web_app_info);
77 68
78 // Accessors, etc. 69 // Accessors, etc.
79 void set_weak_observer(Observer* observer) { weak_observer_ = observer; }
80 const SkBitmap& badge_icon() const { return badge_icon_; } 70 const SkBitmap& badge_icon() const { return badge_icon_; }
81 const SkBitmap& primary_icon() const { return primary_icon_; } 71 const SkBitmap& primary_icon() const { return primary_icon_; }
82 ShortcutInfo& shortcut_info() { return shortcut_info_; } 72 ShortcutInfo& shortcut_info() { return shortcut_info_; }
83 73
84 private: 74 private:
85 friend class base::RefCounted<AddToHomescreenDataFetcher>;
86
87 ~AddToHomescreenDataFetcher() override;
88
89 // WebContentsObserver: 75 // WebContentsObserver:
90 bool OnMessageReceived(const IPC::Message& message, 76 bool OnMessageReceived(const IPC::Message& message,
91 content::RenderFrameHost* sender) override; 77 content::RenderFrameHost* sender) override;
92 78
93 // Called if either InstallableManager or the favicon fetch takes too long. 79 // Called if either InstallableManager or the favicon fetch takes too long.
94 void OnDataTimedout(); 80 void OnDataTimedout();
95 81
96 // Called when InstallableManager finishes looking for a manifest and icon. 82 // Called when InstallableManager finishes looking for a manifest and icon.
97 void OnDidGetManifestAndIcons(const InstallableData& data); 83 void OnDidGetManifestAndIcons(const InstallableData& data);
98 84
99 // Called when InstallableManager finishes checking for installability. 85 // Called when InstallableManager finishes checking for installability.
100 void OnDidPerformInstallableCheck(const InstallableData& data); 86 void OnDidPerformInstallableCheck(const InstallableData& data);
101 87
102 // Grabs the favicon for the current URL. 88 // Grabs the favicon for the current URL.
103 void FetchFavicon(); 89 void FetchFavicon();
104 void OnFaviconFetched( 90 void OnFaviconFetched(
105 const favicon_base::FaviconRawBitmapResult& bitmap_result); 91 const favicon_base::FaviconRawBitmapResult& bitmap_result);
106 92
107 // Creates the launcher icon from the given bitmap. shortcut_info_.url is
108 // used to generate an icon if there is no bitmap in |bitmap_result| or the
109 // bitmap is not large enough.
110 SkBitmap CreateLauncherIconFromFaviconInBackground(
111 const favicon_base::FaviconRawBitmapResult& bitmap_result);
112
113 // Creates the primary launcher icon from the given |icon|. 93 // Creates the primary launcher icon from the given |icon|.
114 void CreateLauncherIcon(const SkBitmap& icon); 94 void CreateLauncherIcon(const SkBitmap& icon);
115 SkBitmap CreateLauncherIconInBackground(const SkBitmap& icon);
116 95
117 // Notifies the observer that the shortcut data is all available. 96 // Notifies the observer that the shortcut data is all available.
118 void NotifyObserver(const SkBitmap& icon); 97 void NotifyObserver(const std::pair<SkBitmap, bool /*is_generated*/>& icon);
119 98
120 InstallableManager* installable_manager_; 99 InstallableManager* installable_manager_;
121 Observer* weak_observer_; 100 Observer* observer_;
122 101
123 // The icons must only be set on the UI thread for thread safety. 102 // The icons must only be set on the UI thread for thread safety.
124 SkBitmap raw_primary_icon_; 103 SkBitmap raw_primary_icon_;
125 SkBitmap badge_icon_; 104 SkBitmap badge_icon_;
126 SkBitmap primary_icon_; 105 SkBitmap primary_icon_;
127 ShortcutInfo shortcut_info_; 106 ShortcutInfo shortcut_info_;
128 107
129 base::CancelableTaskTracker favicon_task_tracker_; 108 base::CancelableTaskTracker favicon_task_tracker_;
130 base::OneShotTimer data_timeout_timer_; 109 base::OneShotTimer data_timeout_timer_;
131 110
132 const int ideal_icon_size_in_px_; 111 const int ideal_icon_size_in_px_;
133 const int minimum_icon_size_in_px_; 112 const int minimum_icon_size_in_px_;
134 const int ideal_splash_image_size_in_px_; 113 const int ideal_splash_image_size_in_px_;
135 const int minimum_splash_image_size_in_px_; 114 const int minimum_splash_image_size_in_px_;
136 const int badge_size_in_px_; 115 const int badge_size_in_px_;
137 const int data_timeout_ms_; 116 const int data_timeout_ms_;
138 117
139 // Indicates whether to check WebAPK compatibility. 118 // Indicates whether to check WebAPK compatibility.
140 bool check_webapk_compatibility_; 119 bool check_webapk_compatibility_;
141 bool is_waiting_for_web_application_info_; 120 bool is_waiting_for_web_application_info_;
142 bool is_installable_check_complete_; 121
122 base::WeakPtrFactory<AddToHomescreenDataFetcher> weak_ptr_factory_;
143 123
144 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcher); 124 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcher);
145 }; 125 };
146 126
147 #endif // CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ 127 #endif // CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/android/webapps/add_to_homescreen_data_fetcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698