diff options
author | droger <droger@chromium.org> | 2014-12-09 05:57:21 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-12-09 13:57:42 +0000 |
commit | 8eabfbc0434d494dbb9f45108bb9c73757e90194 (patch) | |
tree | 3ff7f62d187f44da47a3f2fd3bc5d1452f0c87c2 /ios/web | |
parent | e17e10252cb38c5bc0acf36f1ce67866b757686d (diff) | |
download | chromium_src-8eabfbc0434d494dbb9f45108bb9c73757e90194.zip chromium_src-8eabfbc0434d494dbb9f45108bb9c73757e90194.tar.gz chromium_src-8eabfbc0434d494dbb9f45108bb9c73757e90194.tar.bz2 |
Upstream iOS web code.
This CL upstreams part of the iOS web code.
This code is needed in order to upstream the Translate component.
Review URL: https://codereview.chromium.org/780873002
Cr-Commit-Position: refs/heads/master@{#307463}
Diffstat (limited to 'ios/web')
-rw-r--r-- | ios/web/ios_web.gyp | 6 | ||||
-rw-r--r-- | ios/web/load_committed_details.cc | 13 | ||||
-rw-r--r-- | ios/web/public/load_committed_details.h | 35 | ||||
-rw-r--r-- | ios/web/public/referrer.h | 4 | ||||
-rw-r--r-- | ios/web/public/string_util.h | 22 | ||||
-rw-r--r-- | ios/web/public/web_state/web_state_observer.h | 71 | ||||
-rw-r--r-- | ios/web/string_util.cc | 26 | ||||
-rw-r--r-- | ios/web/string_util_unittest.cc | 45 | ||||
-rw-r--r-- | ios/web/web_state/web_state_observer.cc | 41 |
9 files changed, 262 insertions, 1 deletions
diff --git a/ios/web/ios_web.gyp b/ios/web/ios_web.gyp index cf08644..0b7f927 100644 --- a/ios/web/ios_web.gyp +++ b/ios/web/ios_web.gyp @@ -22,19 +22,25 @@ ], 'sources': [ 'browser_state.cc', + 'load_committed_details.cc', 'navigation/navigation_item_impl.h', 'navigation/navigation_item_impl.mm', 'public/browser_state.h', 'public/favicon_status.cc', 'public/favicon_status.h', + 'public/load_committed_details.h', 'public/navigation_item.h', 'public/referrer.h', 'public/security_style.h', 'public/ssl_status.cc', 'public/ssl_status.h', + 'public/string_util.h', 'public/user_agent.h', 'public/user_agent.mm', + 'public/web_state/web_state_observer.h', 'public/web_thread.h', + 'string_util.cc', + 'web_state/web_state_observer.cc', 'web_thread.cc', ], }, diff --git a/ios/web/load_committed_details.cc b/ios/web/load_committed_details.cc new file mode 100644 index 0000000..b2ac93c --- /dev/null +++ b/ios/web/load_committed_details.cc @@ -0,0 +1,13 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ios/web/public/load_committed_details.h" + +namespace web { + +LoadCommittedDetails::LoadCommittedDetails() + : item(nullptr), previous_item_index(-1), is_in_page(false) { +} + +} // namespace web diff --git a/ios/web/public/load_committed_details.h b/ios/web/public/load_committed_details.h new file mode 100644 index 0000000..bb9a3c1 --- /dev/null +++ b/ios/web/public/load_committed_details.h @@ -0,0 +1,35 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_WEB_PUBLIC_WEB_LOAD_COMMITTED_DETAILS_H_ +#define IOS_WEB_PUBLIC_WEB_LOAD_COMMITTED_DETAILS_H_ + +#include "url/gurl.h" + +namespace web { +class NavigationItem; + +struct LoadCommittedDetails { + // By default, the item will be filled according to a new main frame + // navigation. + LoadCommittedDetails(); + + // The committed item. This will be the active item in the controller. + web::NavigationItem* item; + + // The index of the previously committed navigation item. This will be -1 + // if there are no previous items. + int previous_item_index; + + // The previous URL that the user was on. This may be empty if none. + GURL previous_url; + + // True if the navigation was in-page. This means that the active item's + // URL and the |previous_url| are the same except for reference fragments. + bool is_in_page; +}; + +} // namespace web + +#endif // IOS_WEB_PUBLIC_WEB_LOAD_COMMITTED_DETAILS_H_ diff --git a/ios/web/public/referrer.h b/ios/web/public/referrer.h index 3110c89..d001863 100644 --- a/ios/web/public/referrer.h +++ b/ios/web/public/referrer.h @@ -13,9 +13,11 @@ namespace web { enum ReferrerPolicy { ReferrerPolicyAlways, ReferrerPolicyDefault, + ReferrerPolicyNoReferrerWhenDowngrade, ReferrerPolicyNever, ReferrerPolicyOrigin, - ReferrerPolicyLast = ReferrerPolicyOrigin + ReferrerPolicyOriginWhenCrossOrigin, + ReferrerPolicyLast = ReferrerPolicyOriginWhenCrossOrigin }; // This struct holds a referrer URL, as well as the referrer policy to be diff --git a/ios/web/public/string_util.h b/ios/web/public/string_util.h new file mode 100644 index 0000000..b5b23b3 --- /dev/null +++ b/ios/web/public/string_util.h @@ -0,0 +1,22 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_WEB_PUBLIC_STRING_UTIL_H_ +#define IOS_WEB_PUBLIC_STRING_UTIL_H_ + +#include "base/strings/string16.h" + +namespace web { + +// Truncates |contents| to |length|. +// Returns a string terminated at the last space to ensure no words are +// clipped. +// Note: This function uses spaces as word boundaries and may not handle all +// languages correctly. +base::string16 GetStringByClippingLastWord(const base::string16& contents, + size_t length); + +} // namespace web + +#endif // IOS_WEB_PUBLIC_STRING_UTIL_H_ diff --git a/ios/web/public/web_state/web_state_observer.h b/ios/web/public/web_state/web_state_observer.h new file mode 100644 index 0000000..f63d06e --- /dev/null +++ b/ios/web/public/web_state/web_state_observer.h @@ -0,0 +1,71 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_OBSERVER_H_ +#define IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_OBSERVER_H_ + +#include "base/macros.h" + +namespace web { + +struct LoadCommittedDetails; +class WebState; +class WebStateImpl; + +// An observer API implemented by classes which are interested in various page +// load events from WebState. +class WebStateObserver { + public: + // Returns the web state associated with this observer. + WebState* web_state() const { return web_state_; } + + // This method is invoked when a new non-pending navigation item is created. + // This corresponds to one NavigationManager item being created + // (in the case of new navigations) or renavigated to (for back/forward + // navigations). + virtual void NavigationItemCommitted( + const LoadCommittedDetails& load_details) {}; + + // Called when the current page is loaded. + virtual void PageLoaded() {}; + + // Called on URL hash change events. + virtual void URLHashChanged() {}; + + // Called on history state change events. + virtual void HistoryStateChanged() {}; + + // Invoked when the WebState is being destroyed. Gives subclasses a chance + // to cleanup. + virtual void WebStateDestroyed() {} + + protected: + // Use this constructor when the object is tied to a single WebState for + // its entire lifetime. + explicit WebStateObserver(WebState* web_state); + + // Use this constructor when the object wants to observe a WebState for + // part of its lifetime. It can then call Observe() to start and stop + // observing. + WebStateObserver(); + + virtual ~WebStateObserver(); + + // Start observing a different WebState; used with the default constructor. + void Observe(WebState* web_state); + + private: + friend class WebStateImpl; + + // Stops observing the current web state. + void ResetWebState(); + + WebState* web_state_; + + DISALLOW_COPY_AND_ASSIGN(WebStateObserver); +}; + +} // namespace web + +#endif // IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_OBSERVER_H_ diff --git a/ios/web/string_util.cc b/ios/web/string_util.cc new file mode 100644 index 0000000..a00d023 --- /dev/null +++ b/ios/web/string_util.cc @@ -0,0 +1,26 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ios/web/public/string_util.h" + +#include "base/strings/string_util.h" + +namespace web { + +base::string16 GetStringByClippingLastWord(const base::string16& contents, + size_t length) { + if (contents.size() < length) + return contents; + + base::string16 clipped_contents = contents.substr(0, length); + size_t last_space_index = + clipped_contents.find_last_of(base::kWhitespaceUTF16); + // TODO(droger): Check if we should return the empty string instead. + // See http://crbug.com/324304 + if (last_space_index != base::string16::npos) + clipped_contents.resize(last_space_index); + return clipped_contents; +} + +} // namespace web diff --git a/ios/web/string_util_unittest.cc b/ios/web/string_util_unittest.cc new file mode 100644 index 0000000..4899a92 --- /dev/null +++ b/ios/web/string_util_unittest.cc @@ -0,0 +1,45 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ios/web/public/string_util.h" + +#include "base/strings/utf_string_conversions.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace web { + +// Tests that a regular sentence is clipped correctly. +TEST(StringByClippingLastWordTest, ClipRegularSentence) { + const base::string16 kInput = + base::UTF8ToUTF16("\nSome text here and there."); + EXPECT_EQ(kInput, GetStringByClippingLastWord(kInput, 100)); +} + +// Tests that a long sentence exceeding some length is clipped correctly. +TEST(StringByClippingLastWordTest, ClipLongSentence) { + // An arbitrary length. + const size_t kStringLength = 10; + base::string16 string(kStringLength, 'a'); + string.append(base::UTF8ToUTF16(" b cdefghijklmnopqrstuvwxyz")); + // The string should be cut at the last whitespace, after the 'b' character. + base::string16 result = + GetStringByClippingLastWord(string, kStringLength + 3); + EXPECT_EQ(kStringLength + 2, result.size()); + EXPECT_EQ(0u, string.find_first_of(result)); +} + +// Tests that a block of text with no space is truncated to kLongStringLength. +TEST(StringByClippingLastWordTest, ClipLongTextContentNoSpace) { + // Very long string. + const size_t kLongStringLength = 65536; + // A string slightly longer than |kLongStringLength|. + base::string16 long_string(kLongStringLength + 10, 'a'); + // Block of text with no space should be truncated to kLongStringLength. + base::string16 result = + GetStringByClippingLastWord(long_string, kLongStringLength); + EXPECT_EQ(kLongStringLength, result.size()); + EXPECT_EQ(0u, long_string.find_first_of(result)); +} + +} // namespace web diff --git a/ios/web/web_state/web_state_observer.cc b/ios/web/web_state/web_state_observer.cc new file mode 100644 index 0000000..f6d5053 --- /dev/null +++ b/ios/web/web_state/web_state_observer.cc @@ -0,0 +1,41 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ios/web/public/web_state/web_state_observer.h" + +#include "ios/web/public/load_committed_details.h" +#include "ios/web/public/web_state/web_state.h" + +namespace web { + +WebStateObserver::WebStateObserver(WebState* web_state) : web_state_(nullptr) { + Observe(web_state); +} + +WebStateObserver::WebStateObserver() : web_state_(nullptr) { +} + +WebStateObserver::~WebStateObserver() { + if (web_state_) + web_state_->RemoveObserver(this); +} + +void WebStateObserver::Observe(WebState* web_state) { + if (web_state == web_state_) { + // Early exit to avoid infinite loops if we're in the middle of a callback. + return; + } + if (web_state_) + web_state_->RemoveObserver(this); + web_state_ = web_state; + if (web_state_) + web_state_->AddObserver(this); +} + +void WebStateObserver::ResetWebState() { + web_state_->RemoveObserver(this); + web_state_ = nullptr; +} + +} // namespace web |