Commit 32f92eca authored by Mike Wasserman's avatar Mike Wasserman Committed by Commit Bot

Actually check for valid ARC app shelf ids before DCHECKing validity.

Bug: 771980
Change-Id: I600e523b71c8c4261c47b12a477cc2d394a222bf
Reviewed-on: https://chromium-review.googlesource.com/944715
Commit-Queue: Michael Wasserman <msw@chromium.org>
Reviewed-by: default avatarYury Khmel <khmel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#540742}
parent 1324ed85
...@@ -394,15 +394,16 @@ bool ShowPackageInfo(const std::string& package_name, ...@@ -394,15 +394,16 @@ bool ShowPackageInfo(const std::string& package_name,
bool IsArcItem(content::BrowserContext* context, const std::string& id) { bool IsArcItem(content::BrowserContext* context, const std::string& id) {
DCHECK(context); DCHECK(context);
// Some unit tests use empty id. // Some unit tests use empty ids, some app ids are not valid ARC app ids.
if (id.empty()) const ArcAppShelfId arc_app_shelf_id = ArcAppShelfId::FromString(id);
if (!arc_app_shelf_id.valid())
return false; return false;
const ArcAppListPrefs* const arc_prefs = ArcAppListPrefs::Get(context); const ArcAppListPrefs* const arc_prefs = ArcAppListPrefs::Get(context);
if (!arc_prefs) if (!arc_prefs)
return false; return false;
return arc_prefs->IsRegistered(ArcAppShelfId::FromString(id).app_id()); return arc_prefs->IsRegistered(arc_app_shelf_id.app_id());
} }
std::string GetLaunchIntent(const std::string& package_name, std::string GetLaunchIntent(const std::string& package_name,
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "chrome/browser/ui/app_list/arc/arc_app_utils.h" #include "chrome/browser/ui/app_list/arc/arc_app_utils.h"
#include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ui/ash/launcher/arc_app_shelf_id.h" #include "chrome/browser/ui/ash/launcher/arc_app_shelf_id.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace { namespace {
...@@ -24,14 +26,7 @@ std::string GetPlayStoreInitialLaunchIntent() { ...@@ -24,14 +26,7 @@ std::string GetPlayStoreInitialLaunchIntent() {
} // namespace } // namespace
class ArcAppUtilsTest : public testing::Test { using ArcAppUtilsTest = testing::Test;
public:
ArcAppUtilsTest() = default;
~ArcAppUtilsTest() override = default;
private:
DISALLOW_COPY_AND_ASSIGN(ArcAppUtilsTest);
};
TEST_F(ArcAppUtilsTest, LaunchIntent) { TEST_F(ArcAppUtilsTest, LaunchIntent) {
const std::string launch_intent = GetPlayStoreInitialLaunchIntent(); const std::string launch_intent = GetPlayStoreInitialLaunchIntent();
...@@ -78,3 +73,12 @@ TEST_F(ArcAppUtilsTest, ShelfGroupId) { ...@@ -78,3 +73,12 @@ TEST_F(ArcAppUtilsTest, ShelfGroupId) {
EXPECT_FALSE(shelf_id2.has_shelf_group_id()); EXPECT_FALSE(shelf_id2.has_shelf_group_id());
EXPECT_EQ(shelf_id2.app_id(), arc::kPlayStoreAppId); EXPECT_EQ(shelf_id2.app_id(), arc::kPlayStoreAppId);
} }
// Tests that IsArcItem does not crash or DCHECK with invalid crx file ids.
TEST_F(ArcAppUtilsTest, IsArcItemDoesNotCrashWithInvalidCrxFileIds) {
// TestingProfile checks CurrentlyOn(cotnent::BrowserThread::UI).
content::TestBrowserThreadBundle thread_bundle;
TestingProfile testing_profile;
EXPECT_FALSE(arc::IsArcItem(&testing_profile, std::string()));
EXPECT_FALSE(arc::IsArcItem(&testing_profile, "ShelfWindowWatcher0"));
}
...@@ -21,6 +21,8 @@ constexpr char kShelfGroupPrefix[] = "shelf_group:"; ...@@ -21,6 +21,8 @@ constexpr char kShelfGroupPrefix[] = "shelf_group:";
} // namespace } // namespace
ArcAppShelfId::ArcAppShelfId() = default;
ArcAppShelfId::ArcAppShelfId(const std::string& shelf_group_id, ArcAppShelfId::ArcAppShelfId(const std::string& shelf_group_id,
const std::string& app_id) const std::string& app_id)
: shelf_group_id_(shelf_group_id), app_id_(app_id) { : shelf_group_id_(shelf_group_id), app_id_(app_id) {
...@@ -33,14 +35,15 @@ ArcAppShelfId::~ArcAppShelfId() = default; ...@@ -33,14 +35,15 @@ ArcAppShelfId::~ArcAppShelfId() = default;
// static // static
ArcAppShelfId ArcAppShelfId::FromString(const std::string& id) { ArcAppShelfId ArcAppShelfId::FromString(const std::string& id) {
if (!base::StartsWith(id, kShelfGroupPrefix, base::CompareCase::SENSITIVE)) if (base::StartsWith(id, kShelfGroupPrefix, base::CompareCase::SENSITIVE)) {
const std::vector<std::string> parts = base::SplitString(
id, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (parts.size() == 3u && crx_file::id_util::IdIsValid(parts[2]))
return ArcAppShelfId(parts[1], parts[2]);
} else if (crx_file::id_util::IdIsValid(id)) {
return ArcAppShelfId(std::string(), id); return ArcAppShelfId(std::string(), id);
}
const std::vector<std::string> parts = base::SplitString( return ArcAppShelfId();
id, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
DCHECK_EQ(3u, parts.size());
return ArcAppShelfId(parts[1], parts[2]);
} }
// static // static
......
...@@ -7,19 +7,21 @@ ...@@ -7,19 +7,21 @@
#include <string> #include <string>
// Represents ARC app shelf id that consists from app id and optional shelf
// group id.
namespace arc { namespace arc {
// An ARC app shelf id consisting of an app id (must be a valid crx file id)
// and an optional shelf group id. Ids with empty app ids are invalid.
class ArcAppShelfId { class ArcAppShelfId {
public: public:
ArcAppShelfId();
ArcAppShelfId(const std::string& shelf_group_id, const std::string& app_id); ArcAppShelfId(const std::string& shelf_group_id, const std::string& app_id);
ArcAppShelfId(const ArcAppShelfId& other); ArcAppShelfId(const ArcAppShelfId& other);
~ArcAppShelfId(); ~ArcAppShelfId();
// Returns id from string representation which has syntax // Returns an id from a string with syntax "shelf_group:group_id:app_id".
// "shelf_group:some_group_id:some_app_id". In case suffix shelf_group is // If the shelf_group prefix is absent then the input is treated as an app id.
// absent then id is treated as app id. // In either case, if the app_id is not a valid crx file id, then the returned
// ArcAppShelfId is empty and considered invalid.
static ArcAppShelfId FromString(const std::string& id); static ArcAppShelfId FromString(const std::string& id);
// Constructs id from app id and optional shelf_group_id encoded into the // Constructs id from app id and optional shelf_group_id encoded into the
...@@ -38,6 +40,8 @@ class ArcAppShelfId { ...@@ -38,6 +40,8 @@ class ArcAppShelfId {
const std::string& app_id() const { return app_id_; } const std::string& app_id() const { return app_id_; }
bool valid() const { return !app_id_.empty(); }
private: private:
const std::string shelf_group_id_; const std::string shelf_group_id_;
const std::string app_id_; const std::string app_id_;
......
...@@ -17,14 +17,7 @@ constexpr char kTestIntentWithShelfGroup[] = ...@@ -17,14 +17,7 @@ constexpr char kTestIntentWithShelfGroup[] =
constexpr char kTestIntentWithoutShelfGroup[] = "#Intent;S.other=tmp;end"; constexpr char kTestIntentWithoutShelfGroup[] = "#Intent;S.other=tmp;end";
} // namespace } // namespace
class ArcAppShelfIdTest : public testing::Test { using ArcAppShelfIdTest = testing::Test;
public:
ArcAppShelfIdTest() = default;
~ArcAppShelfIdTest() override = default;
private:
DISALLOW_COPY_AND_ASSIGN(ArcAppShelfIdTest);
};
TEST_F(ArcAppShelfIdTest, BaseTestAAA) { TEST_F(ArcAppShelfIdTest, BaseTestAAA) {
const arc::ArcAppShelfId shelf_id1(kTestShelfGroupId, kTestAppId); const arc::ArcAppShelfId shelf_id1(kTestShelfGroupId, kTestAppId);
...@@ -62,3 +55,19 @@ TEST_F(ArcAppShelfIdTest, BaseTestAAA) { ...@@ -62,3 +55,19 @@ TEST_F(ArcAppShelfIdTest, BaseTestAAA) {
EXPECT_FALSE(shelf_id7.has_shelf_group_id()); EXPECT_FALSE(shelf_id7.has_shelf_group_id());
EXPECT_EQ(shelf_id7.app_id(), kTestAppId); EXPECT_EQ(shelf_id7.app_id(), kTestAppId);
} }
// Tests valid and invalid input to ArcAppShelfId::FromString.
TEST_F(ArcAppShelfIdTest, FromString) {
// A string containing just a valid crx id string yields a valid id.
EXPECT_TRUE(arc::ArcAppShelfId::FromString(kTestAppId).valid());
// A serialized string with a group id and valid crx id yields a valid id.
const arc::ArcAppShelfId id(kTestShelfGroupId, kTestAppId);
EXPECT_TRUE(arc::ArcAppShelfId::FromString(id.ToString()).valid());
// An empty string yields an invalid id.
EXPECT_FALSE(arc::ArcAppShelfId::FromString(std::string()).valid());
// A string with just a group id yields an invalid id.
EXPECT_FALSE(arc::ArcAppShelfId::FromString(kTestShelfGroupId).valid());
// A string with arbitrary text (possibly a non-crx id) yields an invalid id.
EXPECT_FALSE(arc::ArcAppShelfId::FromString("ShelfWindowWatcher0").valid());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment