OLD | NEW |
---|---|
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 #include "chrome/browser/android/banners/app_banner_data_fetcher_android.h" | 5 #include "chrome/browser/android/banners/app_banner_data_fetcher_android.h" |
6 | 6 |
7 #include <vector> | |
8 | |
9 #include "base/strings/string_util.h" | |
7 #include "chrome/browser/android/banners/app_banner_infobar_delegate_android.h" | 10 #include "chrome/browser/android/banners/app_banner_infobar_delegate_android.h" |
11 #include "chrome/browser/banners/app_banner_debug_log.h" | |
8 #include "chrome/browser/infobars/infobar_service.h" | 12 #include "chrome/browser/infobars/infobar_service.h" |
9 #include "chrome/browser/ui/android/infobars/app_banner_infobar_android.h" | 13 #include "chrome/browser/ui/android/infobars/app_banner_infobar_android.h" |
14 #include "chrome/common/chrome_constants.h" | |
15 #include "chrome/common/render_messages.h" | |
10 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
11 | 17 |
18 namespace { | |
19 | |
20 const char kMetaViewportTagName[] = "viewport"; | |
21 const char kMetaViewportWidthDevice[] = "width=device-width"; | |
22 const char kMetaViewportInitialScaleInt[] = "initial-scale=1"; | |
23 const char kMetaViewportInitialScaleFloat[] = "initial-scale=1.0"; | |
24 | |
25 } // anonymous namespace | |
26 | |
12 namespace banners { | 27 namespace banners { |
13 | 28 |
14 AppBannerDataFetcherAndroid::AppBannerDataFetcherAndroid( | 29 AppBannerDataFetcherAndroid::AppBannerDataFetcherAndroid( |
15 content::WebContents* web_contents, | 30 content::WebContents* web_contents, |
16 base::WeakPtr<Delegate> weak_delegate, | 31 base::WeakPtr<Delegate> weak_delegate, |
17 int ideal_icon_size) | 32 int ideal_icon_size) |
18 : AppBannerDataFetcher(web_contents, weak_delegate, ideal_icon_size) { | 33 : AppBannerDataFetcher(web_contents, weak_delegate, ideal_icon_size) { |
19 } | 34 } |
20 | 35 |
21 AppBannerDataFetcherAndroid::~AppBannerDataFetcherAndroid() { | 36 AppBannerDataFetcherAndroid::~AppBannerDataFetcherAndroid() { |
22 } | 37 } |
23 | 38 |
24 std::string AppBannerDataFetcherAndroid::GetBannerType() { | 39 std::string AppBannerDataFetcherAndroid::GetBannerType() { |
25 return native_app_data_.is_null() | 40 return native_app_data_.is_null() |
26 ? AppBannerDataFetcher::GetBannerType() : "android"; | 41 ? AppBannerDataFetcher::GetBannerType() : "android"; |
27 } | 42 } |
28 | 43 |
29 bool AppBannerDataFetcherAndroid::ContinueFetching( | 44 bool AppBannerDataFetcherAndroid::ContinueFetching( |
30 const base::string16& app_title, | 45 const base::string16& app_title, |
31 const std::string& app_package, | 46 const std::string& app_package, |
32 base::android::ScopedJavaLocalRef<jobject> app_data, | 47 base::android::ScopedJavaLocalRef<jobject> app_data, |
33 const GURL& image_url) { | 48 const GURL& image_url) { |
34 set_app_title(app_title); | 49 set_app_title(app_title); |
35 native_app_package_ = app_package; | 50 native_app_package_ = app_package; |
36 native_app_data_.Reset(app_data); | 51 native_app_data_.Reset(app_data); |
37 return FetchIcon(image_url); | 52 return FetchIcon(image_url); |
38 } | 53 } |
39 | 54 |
55 bool AppBannerDataFetcherAndroid::OnMessageReceived( | |
56 const IPC::Message& message) { | |
57 bool handled = true; | |
benwells
2015/06/11 01:54:23
should you call the inherited OnMessageReceived?
dominickn (DO NOT USE)
2015/06/11 03:32:12
It's not inherited from AppBannerDataFetcher (diff
| |
58 IPC_BEGIN_MESSAGE_MAP(AppBannerDataFetcherAndroid, message) | |
59 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidRetrieveMetaTagContent, | |
60 OnDidRetrieveMetaTagContent) | |
61 IPC_MESSAGE_UNHANDLED(handled = false) | |
62 IPC_END_MESSAGE_MAP() | |
63 return handled; | |
64 } | |
65 | |
66 void AppBannerDataFetcherAndroid::HandleWebApp( | |
67 content::WebContents* web_contents) { | |
68 Send(new ChromeViewMsg_RetrieveMetaTagContent(routing_id(), validated_url_, | |
69 kMetaViewportTagName)); | |
70 } | |
71 | |
40 std::string AppBannerDataFetcherAndroid::GetAppIdentifier() { | 72 std::string AppBannerDataFetcherAndroid::GetAppIdentifier() { |
41 return native_app_data_.is_null() | 73 return native_app_data_.is_null() |
42 ? AppBannerDataFetcher::GetAppIdentifier() : native_app_package_; | 74 ? AppBannerDataFetcher::GetAppIdentifier() : native_app_package_; |
43 } | 75 } |
44 | 76 |
45 void AppBannerDataFetcherAndroid::ShowBanner(const SkBitmap* icon, | 77 void AppBannerDataFetcherAndroid::ShowBanner(const SkBitmap* icon, |
46 const base::string16& title) { | 78 const base::string16& title) { |
47 content::WebContents* web_contents = GetWebContents(); | 79 content::WebContents* web_contents = GetWebContents(); |
48 DCHECK(web_contents); | 80 DCHECK(web_contents); |
49 | 81 |
(...skipping 13 matching lines...) Expand all Loading... | |
63 event_request_id(), title, new SkBitmap(*icon), native_app_data_, | 95 event_request_id(), title, new SkBitmap(*icon), native_app_data_, |
64 native_app_package_)); | 96 native_app_package_)); |
65 infobar = new AppBannerInfoBarAndroid(delegate.Pass(), native_app_data_); | 97 infobar = new AppBannerInfoBarAndroid(delegate.Pass(), native_app_data_); |
66 if (infobar) | 98 if (infobar) |
67 RecordDidShowBanner("AppBanner.NativeApp.Shown"); | 99 RecordDidShowBanner("AppBanner.NativeApp.Shown"); |
68 } | 100 } |
69 InfoBarService::FromWebContents(web_contents) | 101 InfoBarService::FromWebContents(web_contents) |
70 ->AddInfoBar(make_scoped_ptr(infobar)); | 102 ->AddInfoBar(make_scoped_ptr(infobar)); |
71 } | 103 } |
72 | 104 |
105 // Ideally, we should be able to get this information more directly. For now, we | |
106 // parse the retrieved meta viewport tag (if any), and look for the necessary | |
107 // strings to determine if the page will scale with the viewport. | |
108 void AppBannerDataFetcherAndroid::OnDidRetrieveMetaTagContent( | |
109 bool success, | |
110 const std::string& tag_name, | |
111 const std::string& tag_content, | |
112 const GURL& expected_url) { | |
113 bool is_responsive = false; | |
114 if (success && tag_name == kMetaViewportTagName && | |
115 validated_url_ == expected_url && | |
116 tag_content.size() <= chrome::kMaxMetaTagAttributeLength) { | |
117 std::string local_tag_content; | |
118 std::vector<std::string> content_tokens; | |
119 | |
120 // Perform the following steps: | |
121 // 1. strip spaces from the viewport content. | |
122 // 2. convert the content to lower case. | |
123 // 3. split the viewport content by commas. | |
124 // 4. search for the required strings. | |
125 base::RemoveChars(tag_content, " ", &local_tag_content); | |
126 base::StringToLowerASCII(&local_tag_content); | |
127 Tokenize(local_tag_content, ",", &content_tokens); | |
128 | |
129 for (auto property : content_tokens) { | |
benwells
2015/06/11 01:54:24
nit: const auto&
| |
130 if (property == kMetaViewportWidthDevice || | |
131 property == kMetaViewportInitialScaleInt || | |
132 property == kMetaViewportInitialScaleFloat) { | |
133 is_responsive = true; | |
134 break; | |
135 } | |
136 } | |
137 } | |
138 | |
139 content::WebContents* web_contents = GetWebContents(); | |
140 if (is_responsive) { | |
benwells
2015/06/11 01:54:24
Nit: this would be cleaner as
if (!responsive) {
dominickn (DO NOT USE)
2015/06/11 03:32:12
Done.
| |
141 if (!CheckFetcherIsStillAlive(web_contents)) { | |
142 OutputDeveloperNotShownMessage(web_contents, | |
143 kUserNavigatedBeforeBannerShown); | |
144 Cancel(); | |
145 return; | |
146 } | |
147 AppBannerDataFetcher::HandleWebApp(web_contents); | |
148 } else { | |
149 OutputDeveloperNotShownMessage(web_contents, kMetaViewportTagError); | |
150 Cancel(); | |
151 return; | |
152 } | |
153 } | |
154 | |
73 } // namespace banners | 155 } // namespace banners |
OLD | NEW |