Chromium Code Reviews| 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; | |
| 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 |
| 50 infobars::InfoBar* infobar = nullptr; | 82 infobars::InfoBar* infobar = nullptr; |
| 51 if (native_app_data_.is_null()) { | 83 if (native_app_data_.is_null()) { |
| 52 scoped_ptr<AppBannerInfoBarDelegateAndroid> delegate( | 84 scoped_ptr<AppBannerInfoBarDelegateAndroid> delegate( |
| 53 new AppBannerInfoBarDelegateAndroid( | 85 new AppBannerInfoBarDelegateAndroid( |
| 54 event_request_id(), title, new SkBitmap(*icon), web_app_data())); | 86 event_request_id(), title, new SkBitmap(*icon), web_app_data())); |
| 55 | 87 |
| 56 infobar = | 88 infobar = |
| 57 new AppBannerInfoBarAndroid(delegate.Pass(), web_app_data().start_url); | 89 new AppBannerInfoBarAndroid(delegate.Pass(), web_app_data().start_url); |
| 58 if (infobar) | 90 if (infobar) |
| 59 RecordDidShowBanner("AppBanner.WebApp.Shown"); | 91 RecordDidShowBanner("AppBanner.WebApp.Shown"); |
| 60 } else { | 92 } else { |
| 61 scoped_ptr<AppBannerInfoBarDelegateAndroid> delegate( | 93 scoped_ptr<AppBannerInfoBarDelegateAndroid> delegate( |
| 62 new AppBannerInfoBarDelegateAndroid( | 94 new AppBannerInfoBarDelegateAndroid( |
| 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) |
|
gone
2015/06/11 17:37:25
Shouldn't this be checking if the infobar is null?
| |
| 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 (const auto& property : content_tokens) { | |
| 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) { | |
| 141 OutputDeveloperNotShownMessage(web_contents, kMetaViewportTagError); | |
| 142 Cancel(); | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 if (!CheckFetcherIsStillAlive(web_contents)) { | |
| 147 OutputDeveloperNotShownMessage(web_contents, | |
| 148 kUserNavigatedBeforeBannerShown); | |
| 149 Cancel(); | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 AppBannerDataFetcher::HandleWebApp(web_contents); | |
| 154 } | |
| 155 | |
| 73 } // namespace banners | 156 } // namespace banners |
| OLD | NEW |