Esempio n. 1
0
  /**
   * Generate a fresh meta/global record.
   *
   * @return meta/global record.
   */
  public MetaGlobal generateNewMetaGlobal() {
    final String newSyncID = Utils.generateGuid();
    final String metaURL = this.config.metaURL();

    ExtendedJSONObject engines = new ExtendedJSONObject();
    for (String engineName : enabledEngineNames()) {
      EngineSettings engineSettings = null;
      try {
        GlobalSyncStage globalStage = this.getSyncStageByName(engineName);
        Integer version = globalStage.getStorageVersion();
        if (version == null) {
          continue; // Don't want this stage to be included in meta/global.
        }
        engineSettings = new EngineSettings(Utils.generateGuid(), version.intValue());
      } catch (NoSuchStageException e) {
        // No trouble; Android Sync might not recognize this engine yet.
        // By default, version 0.  Other clients will see the 0 version and reset/wipe accordingly.
        engineSettings = new EngineSettings(Utils.generateGuid(), 0);
      }
      engines.put(engineName, engineSettings.toJSONObject());
    }

    MetaGlobal metaGlobal = new MetaGlobal(metaURL, this.getAuthHeaderProvider());
    metaGlobal.setSyncID(newSyncID);
    metaGlobal.setStorageVersion(STORAGE_VERSION);
    metaGlobal.setEngines(engines);

    return metaGlobal;
  }
Esempio n. 2
0
  @Test
  public void testUploadUpdatedMetaGlobal() throws Exception {
    // Set up session with meta/global.
    final MockGlobalSessionCallback callback = new MockGlobalSessionCallback();
    final GlobalSession session =
        MockPrefsGlobalSession.getSession(
            TEST_USERNAME,
            TEST_PASSWORD,
            new KeyBundle(TEST_USERNAME, TEST_SYNC_KEY),
            callback,
            null,
            null);
    session.config.metaGlobal = session.generateNewMetaGlobal();
    session.enginesToUpdate.clear();

    // Set enabledEngines in meta/global, including a "new engine."
    String[] origEngines =
        new String[] {"bookmarks", "clients", "forms", "history", "tabs", "new-engine"};

    ExtendedJSONObject origEnginesJSONObject = new ExtendedJSONObject();
    for (String engineName : origEngines) {
      EngineSettings mockEngineSettings =
          new EngineSettings(Utils.generateGuid(), Integer.valueOf(0));
      origEnginesJSONObject.put(engineName, mockEngineSettings);
    }
    session.config.metaGlobal.setEngines(origEnginesJSONObject);

    // Engines to remove.
    String[] toRemove = new String[] {"bookmarks", "tabs"};
    for (String name : toRemove) {
      session.removeEngineFromMetaGlobal(name);
    }

    // Engines to add.
    String[] toAdd = new String[] {"passwords"};
    for (String name : toAdd) {
      String syncId = Utils.generateGuid();
      session.recordForMetaGlobalUpdate(name, new EngineSettings(syncId, Integer.valueOf(1)));
    }

    // Update engines.
    session.uploadUpdatedMetaGlobal();

    // Check resulting enabledEngines.
    Set<String> expected = new HashSet<String>();
    for (String name : origEngines) {
      expected.add(name);
    }
    for (String name : toRemove) {
      expected.remove(name);
    }
    for (String name : toAdd) {
      expected.add(name);
    }
    assertEquals(expected, session.config.metaGlobal.getEnabledEngineNames());
  }
 @Override
 public synchronized String getAccountGUID() {
   String accountGUID =
       accountSharedPreferences.getString(SyncConfiguration.PREF_ACCOUNT_GUID, null);
   if (accountGUID == null) {
     Logger.debug(LOG_TAG, "Account GUID was null. Creating a new one.");
     accountGUID = Utils.generateGuid();
     accountSharedPreferences
         .edit()
         .putString(SyncConfiguration.PREF_ACCOUNT_GUID, accountGUID)
         .commit();
   }
   return accountGUID;
 }
Esempio n. 4
0
  protected synchronized EventDispatcherImpl registerNewInstance(String classname, String filename)
      throws ClassNotFoundException, InstantiationException, IllegalAccessException,
          InvocationTargetException, NoSuchMethodException, IOException {
    Log.d(LOGTAG, "Attempting to instantiate " + classname + "from filename " + filename);

    // It's important to maintain the extension, either .dex, .apk, .jar.
    final String extension = getExtension(filename);
    final File dexFile =
        GeckoJarReader.extractStream(
            mApplicationContext, filename, mApplicationContext.getCacheDir(), "." + extension);
    try {
      if (dexFile == null) {
        throw new IOException("Could not find file " + filename);
      }
      final File tmpDir =
          mApplicationContext.getDir("dex", 0); // We'd prefer getCodeCacheDir but it's API 21+.
      final DexClassLoader loader =
          new DexClassLoader(
              dexFile.getAbsolutePath(),
              tmpDir.getAbsolutePath(),
              null,
              mApplicationContext.getClassLoader());
      final Class<?> c = loader.loadClass(classname);
      final Constructor<?> constructor =
          c.getDeclaredConstructor(Context.class, JavaAddonInterfaceV1.EventDispatcher.class);
      final String guid = Utils.generateGuid();
      final EventDispatcherImpl dispatcher = new EventDispatcherImpl(guid, filename);
      final Object instance = constructor.newInstance(mApplicationContext, dispatcher);
      mGUIDToDispatcherMap.put(guid, dispatcher);
      return dispatcher;
    } finally {
      // DexClassLoader writes an optimized version, so we can get rid of our temporary extracted
      // version.
      if (dexFile != null) {
        dexFile.delete();
      }
    }
  }
  // Add a bookmark in the Desktop folder so we can check the folder navigation in the bookmarks
  // page
  private void setUpDesktopBookmarks() {
    blockForGeckoReady();

    // Get the folder id of the StringHelper.DESKTOP_FOLDER_LABEL folder
    Long desktopFolderId = mDatabaseHelper.getFolderIdFromGuid("toolbar");

    // Generate a Guid for the bookmark
    final String generatedGuid = Utils.generateGuid();
    mAsserter.ok(
        (generatedGuid != null),
        "Generating a random Guid for the bookmark",
        "We could not generate a Guid for the bookmark");

    // Insert the bookmark
    ContentResolver resolver = getActivity().getContentResolver();
    Uri bookmarksUri = mDatabaseHelper.buildUri(DatabaseHelper.BrowserDataType.BOOKMARKS);

    long now = System.currentTimeMillis();
    ContentValues values = new ContentValues();
    values.put("title", StringHelper.ROBOCOP_BLANK_PAGE_02_TITLE);
    values.put("url", DESKTOP_BOOKMARK_URL);
    values.put("parent", desktopFolderId);
    values.put("modified", now);
    values.put("type", 1);
    values.put("guid", generatedGuid);
    values.put("position", 10);
    values.put("created", now);

    int updated =
        resolver.update(bookmarksUri, values, "url = ?", new String[] {DESKTOP_BOOKMARK_URL});
    if (updated == 0) {
      Uri uri = resolver.insert(bookmarksUri, values);
      mAsserter.ok(true, "Inserted at: ", uri.toString());
    } else {
      mAsserter.ok(false, "Failed to insert the Desktop bookmark", "Something went wrong");
    }
  }
Esempio n. 6
0
  @Override
  public void execute() throws NoSuchStageException {
    final String name = getEngineName();
    Logger.debug(LOG_TAG, "Starting execute for " + name);

    try {
      if (!this.isEnabled()) {
        Logger.info(LOG_TAG, "Skipping stage " + name + ".");
        session.advance();
        return;
      }
    } catch (MetaGlobalException.MetaGlobalMalformedSyncIDException e) {
      // Bad engine syncID. This should never happen. Wipe the server.
      try {
        session.updateMetaGlobalWith(
            name, new EngineSettings(Utils.generateGuid(), this.getStorageVersion()));
        Logger.info(
            LOG_TAG, "Wiping server because malformed engine sync ID was found in meta/global.");
        wipeServer();
        Logger.info(LOG_TAG, "Wiped server after malformed engine sync ID found in meta/global.");
      } catch (Exception ex) {
        session.abort(
            ex, "Failed to wipe server after malformed engine sync ID found in meta/global.");
      }
    } catch (MetaGlobalException.MetaGlobalMalformedVersionException e) {
      // Bad engine version. This should never happen. Wipe the server.
      try {
        session.updateMetaGlobalWith(
            name, new EngineSettings(Utils.generateGuid(), this.getStorageVersion()));
        Logger.info(
            LOG_TAG, "Wiping server because malformed engine version was found in meta/global.");
        wipeServer();
        Logger.info(LOG_TAG, "Wiped server after malformed engine version found in meta/global.");
      } catch (Exception ex) {
        session.abort(
            ex, "Failed to wipe server after malformed engine version found in meta/global.");
      }
    } catch (MetaGlobalException.MetaGlobalStaleClientSyncIDException e) {
      // Our syncID is wrong. Reset client and take the server syncID.
      Logger.warn(
          LOG_TAG,
          "Remote engine syncID different from local engine syncID:"
              + " resetting local engine and assuming remote engine syncID.");
      this.resetLocal(e.serverSyncID);
    } catch (MetaGlobalException e) {
      session.abort(e, "Inappropriate meta/global; refusing to execute " + name + " stage.");
      return;
    }

    Synchronizer synchronizer;
    try {
      synchronizer = this.getConfiguredSynchronizer(session);
    } catch (NoCollectionKeysSetException e) {
      session.abort(e, "No CollectionKeys.");
      return;
    } catch (URISyntaxException e) {
      session.abort(e, "Invalid URI syntax for server repository.");
      return;
    } catch (NonObjectJSONException e) {
      session.abort(e, "Invalid persisted JSON for config.");
      return;
    } catch (IOException e) {
      session.abort(e, "Invalid persisted JSON for config.");
      return;
    } catch (ParseException e) {
      session.abort(e, "Invalid persisted JSON for config.");
      return;
    }

    Logger.debug(LOG_TAG, "Invoking synchronizer.");
    synchronizer.synchronize(session.getContext(), this);
    Logger.debug(LOG_TAG, "Reached end of execute.");
  }
 public ClientRecord() {
   this(Utils.generateGuid(), COLLECTION_NAME, 0, false);
 }
Esempio n. 8
0
 public HistoryRecord() {
   super(Utils.generateGuid(), COLLECTION_NAME, 0, false);
 }