extension_util.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_SYNC_GLUE_EXTENSION_UTIL_H_ 6#define CHROME_BROWSER_SYNC_GLUE_EXTENSION_UTIL_H_ 7#pragma once 8 9// This file contains some low-level utility functions used by 10// extensions sync. 11 12#include <set> 13#include <string> 14 15class Extension; 16class ExtensionTypeSet; 17class ExtensionsService; 18 19namespace sync_pb { 20class ExtensionSpecifics; 21} // sync_pb 22 23namespace browser_sync { 24 25enum ExtensionType { 26 THEME, 27 EXTENSION, 28 // A converted user script that does not have an update URL. 29 LOCAL_USER_SCRIPT, 30 // A converted user script that has an update URL. (We don't have a 31 // similar distinction for "regular" extensions as an empty update 32 // URL is interpreted to mean the default update URL [i.e., the 33 // extensions gallery].) 34 UPDATEABLE_USER_SCRIPT, 35 APP, 36}; 37 38typedef std::set<ExtensionType> ExtensionTypeSet; 39 40// Returns the type of the given extension. 41// 42// TODO(akalin): Might be useful to move this into extension.cc. 43ExtensionType GetExtensionType(const Extension& extension); 44 45// Returns whether or not the given extension is one we want to sync. 46bool IsExtensionValid(const Extension& extension); 47 48// Returns whether or not the given extension is one we want to sync. 49bool IsExtensionValidAndSyncable(const Extension& extension, 50 const ExtensionTypeSet& allowed_types); 51 52// Stringifies the given ExtensionSpecifics. 53std::string ExtensionSpecificsToString( 54 const sync_pb::ExtensionSpecifics& specifics); 55 56// Returns whether or not the values of the given specifics are valid, 57// in particular the id, version, and update URL. 58bool IsExtensionSpecificsValid( 59 const sync_pb::ExtensionSpecifics& specifics); 60 61// Equivalent to DCHECK(IsExtensionSpecificsValid(specifics)) << 62// ExtensionSpecificsToString(specifics); 63void DcheckIsExtensionSpecificsValid( 64 const sync_pb::ExtensionSpecifics& specifics); 65 66// Returns true iff two ExtensionSpecifics denote the same extension 67// state. Neither |a| nor |b| need to be valid. 68bool AreExtensionSpecificsEqual(const sync_pb::ExtensionSpecifics& a, 69 const sync_pb::ExtensionSpecifics& b); 70 71// Returns true iff the given ExtensionSpecifics is equal to the empty 72// ExtensionSpecifics object. |specifics| does not have to be valid 73// and indeed, IsExtensionSpecificsValid(specifics) -> 74// !IsExtensionSpecificsUnset(specifics). 75bool IsExtensionSpecificsUnset( 76 const sync_pb::ExtensionSpecifics& specifics); 77 78// Copies the user properties from |specifics| into |dest_specifics|. 79// User properties are properties that are set by the user, i.e. not 80// inherent to the extension. Currently they include |enabled| and 81// |incognito_enabled|. Neither parameter need be valid. 82void CopyUserProperties( 83 const sync_pb::ExtensionSpecifics& specifics, 84 sync_pb::ExtensionSpecifics* dest_specifics); 85 86// Copies everything but non-user properties. Neither parameter need 87// be valid. 88void CopyNonUserProperties( 89 const sync_pb::ExtensionSpecifics& specifics, 90 sync_pb::ExtensionSpecifics* dest_specifics); 91 92// Returns true iff two ExtensionSpecifics have the same user 93// properties. Neither |a| nor |b| need to be valid. 94bool AreExtensionSpecificsUserPropertiesEqual( 95 const sync_pb::ExtensionSpecifics& a, 96 const sync_pb::ExtensionSpecifics& b); 97 98// Returns true iff two ExtensionSpecifics have the same non-user 99// properties. Neither |a| nor |b| need to be valid. 100bool AreExtensionSpecificsNonUserPropertiesEqual( 101 const sync_pb::ExtensionSpecifics& a, 102 const sync_pb::ExtensionSpecifics& b); 103 104// Fills |specifics| with information taken from |extension|, which 105// must be a syncable extension. |specifics| will be valid after this 106// function is called. 107void GetExtensionSpecifics(const Extension& extension, 108 ExtensionsService* extensions_service, 109 sync_pb::ExtensionSpecifics* specifics); 110 111// Exposed only for testing. Pre- and post-conditions are the same as 112// GetExtensionSpecifics(). 113void GetExtensionSpecificsHelper(const Extension& extension, 114 bool enabled, bool incognito_enabled, 115 sync_pb::ExtensionSpecifics* specifics); 116 117// Returns whether or not the extension should be updated according to 118// the specifics. |extension| must be syncable and |specifics| must 119// be valid. 120bool IsExtensionOutdated(const Extension& extension, 121 const sync_pb::ExtensionSpecifics& specifics); 122 123// Sets properties of |extension| according to the information in 124// specifics. |extension| must be syncable and |specifics| must be 125// valid. 126void SetExtensionProperties( 127 const sync_pb::ExtensionSpecifics& specifics, 128 ExtensionsService* extensions_service, Extension* extension); 129 130// Merge |specifics| into |merged_specifics|. Both must be valid and 131// have the same ID. The merge policy is currently to copy the 132// non-user properties of |specifics| into |merged_specifics| (and the 133// user properties if |merge_user_properties| is set) if |specifics| 134// has a more recent or the same version as |merged_specifics|. 135void MergeExtensionSpecifics( 136 const sync_pb::ExtensionSpecifics& specifics, 137 bool merge_user_properties, 138 sync_pb::ExtensionSpecifics* merged_specifics); 139 140} // namespace browser_sync 141 142#endif // CHROME_BROWSER_SYNC_GLUE_EXTENSION_UTIL_H_ 143